473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Script not picking up the url and adding tags

290 100+
Hi,

I am using this script which is nearly working correctly, but not quite!

When a user selects some text from the web-page and copies it,
the script is supposed to pick up the current web-page url and create a
resource box or credit line that is added to the selected text when the
data is pasted.

You will see that the meta keywords are used (randomly) as the anchor text
and this should be wrapped with the href= tags in order to create a link. Thats the bit that is not workink, but I can not see why.

Here is the complete script.

Expand|Select|Wrap|Line Numbers
  1. function init(){
  2.  
  3. // Options:
  4. var useMetaKeyword = true; // Otherwise, page title
  5. var minLength = 40; // Min selection chars
  6. var useMetaAuthor = true; // Otherwise use domain
  7. var addLinks = true; // Otherwise, just cite at end
  8. var skip = new Array("home","link","click here"); // Don't link these (lowercase!)
  9.  
  10. function D(b,a,c){
  11.  if(b.addEventListener)b.addEventListener(a,c,false);
  12.  else b.attachEvent&&b.attachEvent("on"+a,c)}
  13.  
  14. function o(b,a){
  15.  if(typeof b=="undefined"||b==null||!RegExp)return false;
  16.    a=new RegExp("(^|\\s)"+a+"(\\s|$)");
  17.  if(typeof b=="string")return a.test(b);
  18.   else if(typeof b=="object"&&b.className)return a.test(b.className);return false}
  19.  
  20. function E(b,a){
  21.  var c=false,j;
  22.  for(j=b.parentNode;j!=undefined;){
  23.   if(b.parentNode==e.body)break;
  24.     else if(b.parentNode==a){c=true;break}j=j.parentNode}return c}
  25.  
  26. function F(b){
  27.   return b.replace(/^\s*/,"")
  28.     }
  29.  
  30. function G(b){
  31.   return b.replace(/\s*$/,"")
  32.     }
  33.  
  34. function H(b){
  35.   return G(F(b))
  36.     }
  37.  
  38. var I=new Array("home","link","click here"),
  39. e=document,
  40. x=window,
  41. t=e.getElementsByTagName("body")[0],
  42. p=e.getElementsByName("author"),
  43. i=e.getElementsByName("keywords"),
  44. q=x.location.toString(),
  45. u=e.title.toString(),
  46. d;if(!Array.indexOf)Array.prototype.indexOf=function(b,a){
  47.   var c=-1;
  48.     for(a=a|0;a<this.length||a==-1;a++)
  49.     if(this[a]==b)c=a;return c};
  50.     if(i.length>0&&useMetaKeyword){
  51.       i=e.getElementsByName("keywords")[0].getAttribute("content").split(",");
  52.     u=Math.floor(Math.random()*i.length);
  53.         i=i[u].replace(/^\s*|\s*$/,"")}
  54.     else 
  55.       i=u;
  56.         p=(p.length>0&&useMetaAuthor)?p[0].getAttribute("content"):e.domain;
  57.  
  58. var y="<p id='credit'><br/>Read more about <a href='"+q+"'>"+i+"</a> by <a href='http://"+e.domain+"' />"+p+"</a></p>";
  59.  
  60. if(/MSIE/g.test(navigator.userAgent))var v="msie";
  61. else if(/Safari/g.test(navigator.userAgent))v="safChrome";
  62. q=e.createElement("span");q.setAttribute("id","sasText");
  63. t.appendChild(q);
  64. d=e.getElementById("sasText");
  65.  
  66. posType=document.all&&!window.opera&&!window.XMLHttpRequest?"absolute":"fixed";
  67. d.style.position=posType;
  68. d.style.top="0px";
  69. d.style.left="-9999px";
  70. D(t,"copy",function(){d.innerHTML=y;
  71. if(v=="msie"){
  72.   for(var b=e.selection.createRange(),
  73.    a=b.parentElement();
  74.      a.nodeName!="BODY"&&!o(a,"lbExclude");)a=a.parentNode;
  75.      if(o(a,"lbExclude"))return true;
  76.      a=e.body.createTextRange();
  77.      a.moveToElementText(d);
  78.      var c=b.duplicate();
  79.      c=c.htmlText;
  80.  
  81. if(c.length>minLength){
  82.   d.id="tempSasText";
  83.     d.innerHTML=c+y;
  84.     (c=e.getElementById("sasText"))&&c.parentNode.removeChild(c);
  85.     d.id="sasText";a.select()}}
  86.  
  87. else{b=x.getSelection();
  88.   for(a=b.anchorNode;a.nodeName!="BODY"&&!o(a,"lbExclude");)a=a.parentNode;
  89.      if(o(a,"lbExclude"))return false;
  90.      if(b==""&&v=="safChrome"){
  91.        d.innerHTML=t.innerHTML;
  92.          a=document.createRange();
  93.          b.removeAllRanges();
  94.          a.selectNodeContents(d);b.addRange(a)}
  95.  
  96. else if(b.toString().length>minLength){
  97.  var j=e.getElementById("credit");
  98.  a=b.getRangeAt(0);
  99.  c=a.cloneContents();
  100.  d.id="tempSasText";
  101.  d.insertBefore(c,j);
  102.  (c=e.getElementById("sasText"))&&c.parentNode.removeChild(c);
  103.  d.id="sasText";
  104.  b.removeAllRanges();
  105.  a.selectNode(d);
  106.  b.addRange(a)}}
  107.  
  108.  var w=[];a=d.getElementsByTagName("a");for(b=0;b<a.length;b++)w.push(a[b].href);
  109.  
  110. if(addLinks){a=e.getElementsByTagName("a");
  111. for(b=0;b<a.length;b++){var r=a[b].href;
  112.  
  113. if(w.indexOf(r)==-1)if(E(a[b],d)==false){
  114. var f=H(a[b].innerHTML).toLowerCase();
  115.  
  116. if(skip.indexOf(f)==-1)
  117. if((new RegExp(e.domain,"g")).test(r)){
  118. var z=[];
  119.  
  120. function n(g,k,l){
  121.  for(var A=g.childNodes.length;A-- >0;){
  122.  var h=g.childNodes[A];
  123.  if(h.nodeType===1)h.tagName.toLowerCase()!=="a"&&n(h,k,l);
  124.  
  125. else if(h.nodeType===3)
  126.  for(var m=h.data.length;1;){
  127.  m=h.data.lastIndexOf(k,m);
  128.  if(m===-1||z.indexOf(k.toLowerCase())!==-1)break;
  129.  var B=/\w/;
  130.  if(h.nodeValue.charAt(m-1).match(B)||h.nodeValue.charAt(m+f.length).match(B))break;
  131.  l.call(window,h,m)}}}
  132.  
  133. function s(g,k){g.splitText(k+f.length);
  134.  var l=e.createElement("a");
  135.  l.href=r;l.appendChild(g.splitText(k));
  136.  g.parentNode.insertBefore(l,g.nextSibling);
  137.  z.push(f.toLowerCase());
  138.  w.push(r)}n(d,f,s);f=f.charAt(0).toUpperCase()+f.slice(1);n(d,f,s);
  139.  f=f.toUpperCase();
  140.  n(d,f,s);f=f.replace(/\w\S*/g,function(g){
  141.  return g.charAt(0).toUpperCase()+g.substr(1).toLowerCase()});n(d,f,s)}}}}})}
  142.  window.onload=init;
  143.  
I have put the script on this page:
test page

To test it, just go to that page, highlight some text
(at least 40 characters) copy it, and then paste it
into your editor or notepad.

You can see that it is picking up some info and
writing the attribute line but it doesn't create the link

If it helps get it working, I am happy to slim the code
down by excluding some features.

Any ideas ?
Dec 11 '09 #1
3 2114
acoder
16,027 Expert Mod 8TB
I've not gone through your code, but I tested your page, and this was added:
Read more about consumer complaints by www.Support-Focus.com
I see line 58 has something similar, but the links have been stripped out. What do you actually do (or attempt to) with this line afterwards (to save me trying to decipher it)?
Dec 12 '09 #2
jeddiki
290 100+
Thanks for looking :)

The bit that you saw added was the "credit line".

As you noted, the links were not present.

The script is supposed to create that credit line with working html hyperlinks
but it doesn't.

To answer - what I am trying to do, well the idea is that when anyone
highlights and copies a paragragh or two from my webpage, then this script will
add the credit line so it is there ready if the copier wants to use it.

(of course there is nothing to stop them from deleting it ! )

Line 58 is where this credit line is created - but I don't know why the hyperlink
doesn't work

Any suggestions ?
Dec 14 '09 #3
acoder
16,027 Expert Mod 8TB
It's not my line of expertise as it's not something I've really looked into or investigated, but it does seem that all non-text is ignored. For example, the bold in the header is removed and is copied as plain text. I don't know if there's a solution, but hope that helps in some way.
Dec 21 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Maxim Vexler | last post by:
Hello to everyone, Assuming i have this simple script : <?PHP //Opening tag =' $html_header=' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"...
30
by: Toni Mcintyre | last post by:
i'm having 2 problems with the http://validator.w3.org 1. if i have: <meta http-equiv="Content-Script-Type" content="text/javascript"> then why do i need <script type=text/javascript>...
11
by: rajarao | last post by:
hi I want to remove the content embedded in <script> and </script> tags submitted via text box. My java script should remove the content embedded between <script> and </script> tag. my current...
38
by: | last post by:
I have a script... ----- <SCRIPT language="JavaScript" type="text/javascript"> <!-- function makeArray() { for (i = 0; i<makeArray.arguments.length; i++) this = makeArray.arguments; } ...
8
by: Jakej | last post by:
I've been using a javascript in an html file for a banner slider, and it works as desired. But I'd like to use it on more than one page and it would be great if I could transfer the code to a .js...
12
by: tshad | last post by:
I am not sure why I am getting this error: I have the following code I want to run from another include file that holds all my functions. functions.inc...
4
by: petermichaux | last post by:
Hi, I'm hoping for a reason I'm wrong or an alternate solution... I'd like to be able to dynamically include some javascript files. This is like scriptaculous.js library but their solution is...
3
by: Tarik Monem | last post by:
Hey everyone, I'm trying to add javascript tags into a specific DIV so that I can use a variable to change out the script tags source and use it as a type of "javascript-include" I found a...
11
by: moltendorf | last post by:
Hey everyone, as I have been by far pleased with some of the top helpers on this forum, I have decided to send another one your way. Even though I am personally having minimal issues with this...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.