我的用户如何根据链接的文本获取链接?

时间:2012-09-11 17:34:43

标签: javascript css-selectors greasemonkey contains

在目标网页上给出此HTML:

<dd class="ddTit">
<a href="http://abc.xxx.com/54781.html" target="_blank">special text words</a>
</dd>

如何根据“特殊文字”获取网址,并点击Greasemonkey脚本?

我试过了:

var textlink = document.querySelector ("dd.ddTit a[textContent*='special']");
var clickEvent      = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
textlink.dispatchEvent (clickEvent);

没有运气。

如何根据textContent进行选择,例如textContent包含特殊这个词?

3 个答案:

答案 0 :(得分:3)

唉,你不能用querySelector()来做CSS selectors do not (yet) offer a content/text selector

以下是3种方法:

  1. 循环选择有针对性的节点:

    var ddTitLinks  = document.querySelectorAll ("dd.ddTit a");
    
    for (var J = ddTitLinks.length - 1;  J >= 0;  --J) {
        var ddTitLink   = ddTitLinks[J];
        //--- Case-insensitive search.
        if (/special text words/i.test (ddTitLink.textContent) ) {
            var clickEvent      = document.createEvent ('MouseEvents');
            clickEvent.initEvent ('click', true, true);
            ddTitLink.dispatchEvent (clickEvent);
            break;
        }
    }
    


  2. 使用jQuery(一个功能强大的图书馆,可以为您节省大量的时间):

    // ==UserScript==
    // @name     _Click Special link(s)
    // @include  http://YOUR_SERVER.COM/YOUR_PATH/*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
    // ==/UserScript==
    
    //--- Note that contains() is CASE SENSITIVE.
    var specLink    = $("dd.ddTit a:contains('special text words')");
    if (specLink.length) {
        var clickEvent  = document.createEvent ('MouseEvents');
        clickEvent.initEvent ('click', true, true);
        specLink[0].dispatchEvent (clickEvent);
    }
    


  3. 使用XPath:

    //--- Note that contains() is CASE SENSITIVE.
    var specLinks   = document.evaluate (
        "//dd[contains(@class, 'ddTit')]/a[contains(text(), 'special text words')]",
        document,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null
    );
    
    if (specLinks.snapshotLength) {
        var specLink    = specLinks.snapshotItem (0);
        var clickEvent  = document.createEvent ('MouseEvents');
        clickEvent.initEvent ('click', true, true);
        specLink.dispatchEvent (clickEvent);
    }
    

答案 1 :(得分:1)

这是一个使用jquery的greasemonkey脚本,它可以执行你想要的任务:

// ==UserScript==
// @name       your script
// @namespace  http://foobarfoobar.com/
// @version    0.1
// @description  Trigger click with jquery
// @require    http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// @match      http://*/*
// ==/UserScript==

(function () {
    $(document).ready(function(){
        $('dd.ddTit').find("a:contains('special text words')").bind('click', function() {
            window.location.href = this.href;
            return false;
        }).trigger('click');

    });
})();

如果您将部分代码复制到脚本中,请确保包含以下行:

// @require    http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js

所以jquery代码运行良好,也将@match规则调整到你想要应用脚本的特定网站。

答案 2 :(得分:0)

如果你真的只有内容来识别链接,你可能需要遍历所有链接以找到你想要的链接:

var links = document.getElementsByTagName( 'a' );

for ( var i = 0; i < links.length; i++ ) {
    var link = links[i];
    if ( /special/.test( link.textContent ) ) {
        // do something with link
    }
}