首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建一键复制链接?

如何创建一键复制链接?
EN

Stack Overflow用户
提问于 2014-06-07 08:30:05
回答 2查看 548关注 0票数 1

对于这个linkify plus脚本,我尝试使用GM_Setclipboardhref链接复制到剪贴板上。

如果所讨论的网页只找到并“链接”了一串文本,则该脚本工作得很好。如果它链接了两个字符串,则1-click复制功能在两个链接上都有效,但只复制要“链接”的最后一个字符串。

我甚至不确定我正在尝试做的事情是不可能的。发现了一些使用flash+jQuery+zeroClipboard的类似问题/解决方法。但我不确定是否可以将其实现到Greasemonkey脚本中。

代码语言:javascript
复制
// ==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);
} ) ();
EN

回答 2

Stack Overflow用户

发布于 2020-05-11 12:55:54

使用Anthony Lieuallen脚本的最新版本2.0.2。现在,GM_setClipboard的工作方式非常出色。如下所示:

代码语言:javascript
复制
// ==UserScript==
// @name        Linkify Plus
// @version     2.0.2
// @namespace   http://arantius.com/misc/greasemonkey/
// @description Turn plain text URLs into links.    Supports http, https, ftp, email addresses.
// @grant GM_setClipboard
// ==/UserScript==

/*******************************************************************************
Loosely based on the Linkify script located at:
  http://downloads.mozdev.org/greasemonkey/linkify.user.js

Originally written by Anthony Lieuallen of http://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.

Version history:
 Version 2.0.3:
  - Fix infinite recursion on X(HT)ML pages.
 Version 2.0.2:
  - Limit @include, for greater site/plugin compatibility.
 Version 2.0.1:
  - Fix aberrant 'mailto:' where it does not belong.
 Version 2.0:
  - Apply incrementally, so the browser does not hang on large pages.
  - Continually apply to new content added to the page (i.e. AJAX).
 Version 1.1.4:
  - Basic "don't screw up xml pretty printing" exception case
 Version 1.1.3:
  - Include "+" in the username of email addresses.
 Version 1.1.2:
  - Include "." in the username of email addresses.
 Version 1.1:
  - Fixed a big that caused the first link in a piece of text to
    be skipped (i.e. not linkified).
*******************************************************************************/

var notInTags=[
    'a', 'head', 'noscript', 'option', 'script', 'style', 'title', 'textarea'
];
var textNodeXpath=
    ".//text()[not(ancestor::ns:"+notInTags.join(') and not(ancestor::ns:')+")]";

//Insert Your personal Regex below.  (My example: ip + port in "xxx.xxx.xxx.xxx PORT" format)
var urlRE=/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\s\d{1,5}\b/gi;

var queue=[];

/******************************************************************************/

linkifyContainer(document.body);
document.body.addEventListener('DOMNodeInserted', function(event) {
    linkifyContainer(event.target);
}, false);

/******************************************************************************/

function linkifyContainer(container) {
    var xpathResult=document.evaluate(
        textNodeXpath,
        container,
        { lookupNamespaceURI: function(prefix) { return container.namespaceURI; } },
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null
    ); 

    var i=0;
    function continuation() {
        var node, counter=0;
        while (node=xpathResult.snapshotItem(i++)) {
            linkifyTextNode(node);

            if (++counter>50) {
                return setTimeout(continuation, 0);
            }
        }
    }
    setTimeout(continuation, 0);
}

function linkifyTextNode(node) {
    var i, l, m;
    var txt=node.textContent;
    var span=null;
    var 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.appendChild(document.createTextNode(l));
        //if (l.match(/^www/i)) {
        //  l='http://'+l;
        //} else if (-1==l.indexOf('://')) {
        //  l='mailto:'+l;
        //}
        a.setAttribute('href', l);
        a.onclick = function() { GM_setClipboard(l, 'text'); return false};
        span.appendChild(a);
        //track insertion point
        p=m.index+m[0].length;
    }
    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
        try {
            node.parentNode.replaceChild(span, node);
        } catch (e) {
            console.error(e);
            console.log(node);
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2014-06-07 09:10:56

http://www.htmlgoodies.com/beyond/javascript/article.php/3458851

看看这是否有帮助。我现在正在打电话,所以不能探索太多。一旦我进入系统,我会试一试。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24092461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档