Javascript 是否将文本替换为指向该文本的链接?

Javascript 是否将文本替换为指向该文本的链接?,javascript,html,greasemonkey,Javascript,Html,Greasemonkey,这是我先前问题的后续。我正在尝试使用Greasemonkey将中的文本更改为包含该文本的链接 因此页面包含 <td class="something"><div style="width: 200px;"> randomtext </div></td> 我还需要做什么才能使这项工作正常进行?忘记jQuery,它只会降低页面速度。 我还没有真正测试过这段代码,但可能需要进行一些调试: // ==UserScript== // ==/UserSc

这是我先前问题的后续。我正在尝试使用Greasemonkey将
中的文本更改为包含该文本的链接

因此页面包含

<td class="something"><div style="width: 200px;">
  randomtext
</div></td>

我还需要做什么才能使这项工作正常进行?

忘记jQuery,它只会降低页面速度。 我还没有真正测试过这段代码,但可能需要进行一些调试:

// ==UserScript==
// ==/UserScript==
(function() { 
    // collect variables
    // you can change this to change which element you replace
    var reference = document.querySelector('td.something>div:first-child');
    var text = reference.innerText;
    var replacement = text.replace(reference, "www.somewhere.com/q?=" + reference);

    // create new anchor tag
    var a = document.createElement('a');
    a.href = replacement;
    a.innerText = text;

    // do the replacement
    reference.innerHTML = ''; // clear the old contents of the reference
    reference.appendChild(a); // append the new anchor tag into the element
})();

这是Greasemonkey脚本的一个相当标准的操作。让它变得容易

您的完整脚本如下所示:

// ==UserScript==
// @name     _Select text (re)linker
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
waitForKeyElements (".something > div", linkifyText);

function linkifyText (jNode) {
    jNode.wrapInner ( function () {
        var newHref = 'http:\/\/www.somewhere.com\/q?='
                    + encodeURIComponent (this.textContent.trim () );

        //-- Note that link text will be filled in automatically.
        var newLink = '<a href="' + newHref + '"></a>';

        return newLink;
    } );
}
/==UserScript==
//@name\u选择文本(重新)链接器
//@包括http://YOUR_SERVER.COM/YOUR_PATH/*
//@需要http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
//@需要https://gist.github.com/raw/2625891/waitForKeyElements.js
//@grant GM_addStyle
//==/UserScript==
/*-需要@grant指令来解决设计变更
在GM 1.0中引入。它会恢复沙箱。
*/
waitForKeyElements(“.something>div”,linkifyText);
函数linkifyText(jNode){
jNode.wrapInner(函数(){
var newHref='http:\/\/www.somewhere.com\/q?='
+encodeURIComponent(this.textContent.trim());
//--请注意,链接文本将自动填充。
var newLink='';
返回newLink;
} );
}

1。您需要使用协议(例如http://2)启动href。getElementsByTagName按标记名返回元素数组。代码中没有标记“something”。只是一个具有类属性“something”的元素。3.如果您尝试getElementsByTagName('td'),您将得到一个包含所有td元素的数组,您可以使用for循环遍历,并检查$(reference[i]).hasClass('something')。
// ==UserScript==
// ==/UserScript==
(function() { 
    // collect variables
    // you can change this to change which element you replace
    var reference = document.querySelector('td.something>div:first-child');
    var text = reference.innerText;
    var replacement = text.replace(reference, "www.somewhere.com/q?=" + reference);

    // create new anchor tag
    var a = document.createElement('a');
    a.href = replacement;
    a.innerText = text;

    // do the replacement
    reference.innerHTML = ''; // clear the old contents of the reference
    reference.appendChild(a); // append the new anchor tag into the element
})();
// ==UserScript==
// @name     _Select text (re)linker
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
waitForKeyElements (".something > div", linkifyText);

function linkifyText (jNode) {
    jNode.wrapInner ( function () {
        var newHref = 'http:\/\/www.somewhere.com\/q?='
                    + encodeURIComponent (this.textContent.trim () );

        //-- Note that link text will be filled in automatically.
        var newLink = '<a href="' + newHref + '"></a>';

        return newLink;
    } );
}