2014-06-07 38 views
1

對於此鏈接加腳本,我試圖使href鏈接一鍵複製到剪貼板,使用GM_Setclipboard如何創建一次點擊複製鏈接?

如果有問題的網頁只找到並「鏈接」一個文本字符串,該腳本就可以正常工作。如果它鏈接兩個字符串,一鍵複製功能對兩個鏈接起作用,但只複製最後一個字符串以「鏈接」。

我甚至不確定我想要做什麼是可能的。發現了一些使用flash + jQuery + zeroClipboard的類似問題/解決方法。但不知道我是否可以將它實現爲Greasemonkey腳本。

// ==UserScript== 
// @name  1Click_COPY 
// @include  http*://www.w3schools.com/* 
// $Revision: #2 $ 
// ==/UserScript== 
// Originally written by Anthony Lieuallen of http://www.arantius.com/ 
// Licensed for unlimited modification and redistribution as long as this notice is kept intact. 
// 
// If possible, please contact me regarding new features, bugfixes 
// or changes that I could integrate into the existing code instead of 
// creating a different script. Thank you 

(function(){ 
    function linkify() { 
     try { 
      var notInTags=['a', 'head', 'noscript', 'option', 'script', 'style', 'title', 'textarea']; 
      var res = document.evaluate("//text()[not(ancestor::"+notInTags.join(') and not(ancestor::')+")]", 
       document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 
      var i, el, l, m, p, span, txt, urlRE, linky; 

      //The string you want to find using reg ex. This finds http to create links. 
      urlRE=/\b(https?:\/\/[^\s+\"\<\>]+)/ig. 

      for (i=0; el=res.snapshotItem(i); i++) { 
       //grab the text of this element and be sure it has a URL in it 
       txt=el.textContent; 
       span=null; 
       p=0; 
       while (m=urlRE.exec(txt)) { 
        if (null==span) { 
         //create a span to hold the new text with links in it 
         span=document.createElement('span'); 
        } 

        //get the link without trailing dots 
        l=m[0].replace(/\.*$/, ''); 
        //put in text up to the link 
        span.appendChild(document.createTextNode(txt.substring(p, m.index))); 
        //create a link and put it in the span 
        a=document.createElement('a'); 
        a.className='linkifyplus'; 
        a.appendChild(document.createTextNode(l)); 

        a.setAttribute('href', l); 

        //a.setAttribute('onclick', "return false"); 
        //linky=a.getAttritube('href'); 
        //a.setAttritube("onclick", function() { GM_setClipboard(l, 'text') 
}); 
        //copy text to clipboard 

        a.onclick = function() { GM_setClipboard(l, 'text'); return false}; 

        span.appendChild(a); 

        p=m.index+m[0].length; 
       } 

       // This removes the non linked text 
       if (span) { 
        //take the text after the last link 
        span.appendChild(document.createTextNode(txt.substring(p, txt.length))); 
        //replace the original text with the new span 
        el.parentNode.replaceChild(span, el); 
       } 
      } 
     } 
     catch(e) {dump('Linkify Plus Error ('+e.lineNumber+'): '+e+'\n');} 
    } 

    window.addEventListener("load", linkify, false); 
})(); 

回答

相關問題