如何根据链接中包含的字母/单词查找并单击链接

How to find and click link based on the letters/words it contains

本文关键字:链接 单词 查找 单击 何根 包含      更新时间:2024-06-22

我有这段代码直到最近还在运行,不知道为什么,但我认为这是因为有两个链接具有相同的类名。

这就是代码最初的样子——

if (tweet[0].match(theName) == theName) {
      document.getElementsByClassName("twitter-timeline-link")[0].click();
        tFunction = "get outa here";
        theName = " 4444  4 456 7 8 456 7 345 7 345  345 3 1 1 133s "; //buffer to jump out of interval loop
    } 

这就是我的代码现在的样子,它是扫描和刷新函数的一部分,直到变量匹配,这样我就可以点击链接——

if (tweet[0].match(theName) == theName) {
        document.getElementsByClassName("twitter-timeline-link").find("a:contains('swoo.sh')").bind('click', function() {
            window.location.href = this.href;
            return false;
        }).trigger('click');
        tFunction = "get outa here";
        theName = " 4444  4 456 7 8 456 7 345 7 345  345 3 1 1 133s "; //buffer to jump out of interval loop
    } 

问题是通常有2个链接,而错误的链接最终会被点击,所以我正在寻找一种方法来匹配我希望它点击的链接(即swoo)中的一些文本。

我试图使用另一个代码,但它需要我注入jquery库(因为$),我不能/不想这样做,因为它是chrome扩展。

你知道怎么修吗?感谢

您的第二个代码块无论如何都使用jQuery(以防您没有注意到)

关于你的第一个代码块(抛开我不知道tFunctiontheName是干什么的事实),你可以这样修改它:

if (tweet[0].match(theName) == theName) {
    [].slice.call(document.querySelectorAll('a.twitter-timeline-link'))
      .some(function(elem) {
        if (elem.textContent.indexOf('swoo.sh') > -1) {
            elem.click();
            return true;
        }
        return false;
    });
    tFunction = ...;
    theName = ...;
} 

更新:

使用下面的示例扩展,我可以点击我的链接:)它每秒重新加载一次页面,直到找到指定的鞋子条目,然后查找正确的链接(包含"swo.sh")并单击它。

更新2:

事实证明,当网络连接出现滞后时,一些动态DOM操作会导致不一致的行为。我在代码中进行了一些修改,以考虑元素的属性(这些属性不受动态DOM元素创建的影响,并提供了增强的一致性)。事实上,在尝试了十几次之后,我无法再现任何类型的不一致性(这是,而不是以前的方法)。

content.js:

var shoeName = ...;
var tweets = new Array();
function twitterScan() {
    tweets = [].slice.call(document.getElementsByClassName("js-tweet-text"));
    var targetLink = null;
    var foundTweet = tweets.some(function(tweet) {
        if (tweet.innerHTML.indexOf(shoeName) !== -1) {
            var foundLink = [].slice
                    .call(tweet.querySelectorAll('a.twitter-timeline-link'))
                    .some(function(el) {
                if (el.dataset.expandedUrl.indexOf('swoo.sh') !== -1) {
                    targetLink = el;
                    return true;
                }
                return false;
            });
            if (!foundLink) {
                alert('Found ' + shoeName + ', but couldn''t find the link :(');
            }
            return true;
        }
        return false;
    });
    if (targetLink) {
        window.open(targetLink.href);
    } else if (!foundTweet) {
        location.reload(true);
    }
}
setTimeout(twitterScan, 1000);

manifest.json:

{
    "manifest_version": 2,
    "name":    "Test Extension",
    "version": "0.0",
    "offline_enabled": false,
    "content_scripts": [{
        "matches":    ["*://some.domain.com/*"],
        "js":         ["content.js"],
        "run_at":     "document_end",
        "all_frames": false
    }]
}