By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,717 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,717 IT Pros & Developers. It's quick & easy.

Script not picking up the url and adding tags

100+
P: 290
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
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
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

100+
P: 290
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
Expert Mod 15k+
P: 16,027
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

Post your reply

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