【问题标题】:How can I make a userscript that gets the href attribute contents of every element in a specified class?如何制作一个获取指定类中每个元素的 href 属性内容的用户脚本?
【发布时间】:2014-02-11 23:53:48
【问题描述】:

这是网页的html格式:

<ul id="all_items_container" >
<li class="first item" id="">
    <a class="item title_link" href="#942" title="blah blah" id="" >
        <span class="title" >blah blah</span>
    </a>
</li>
<li class="item " id="">
    <a class="item title_link" href="#846" title="blah blah blah" id="" >
        <span class="title" >blah blah blah</span>
    </a>
</li>
<li class="item " id="">
    <a class="item title_link" href="#745" title="blah blah blah blah" id="" >
        <span class="title" >blah blah blah blah</span>
    </a>
</li>

我需要能够获取“item title_link”类中元素的所有href属性,然后去掉#符号,然后将其转换为url。例如,“item title_link”类中的第一个元素的href值为“#942”,我需要能够将该字符串转换为类似“http://www.domainname.com/index/942/get”的内容。

我需要能够为该类中的所有元素创建一个脚本,然后导航到每个生成的链接。这是我到目前为止不起作用的脚本:

var itemLinks = document.getElementsByClassName("item title_link");
var itemLinksHRefs = itemLinks.href;
var actualItemHRefs = itemLinksHRefs.replace("#","");
var actualItemLinks = "http://www.domainname.com/index/"+actualItemHRefs+"/get";

window.location.href = actualItemLinks;

我知道我的脚本可能偏离了正确的方法,但我对创建用户脚本还很陌生,因此我们将不胜感激。

编辑:我需要一种方法让脚本等待启动 6 秒,然后获取“item title_link”类中所有元素的 hrefs 值并将它们转换为 url 列表,然后要么该脚本在新选项卡中打开所有这些链接,或者能够以这种格式将这些链接导出到文本文件:

http://www.domainname.com/index/942/get
http://www.domainname.com/index/846/get
http://www.domainname.com/index/745/get

【问题讨论】:

  • 你只能“导航”到第一个链接,然后页面的JS停止运行...
  • a) 您只能将一个类传递给该方法。如果两者都需要,请使用document.querySelectorAll('.item.title_link')。 b) 您需要迭代集合,或在访问属性之前提取第一个元素。

标签: javascript url href userscripts getelementsbyclassname


【解决方案1】:

document.getElementsByClassName

返回一个包含任何给定元素的所有子元素的数组 类名。当在文档对象上调用时,完整的文档 被搜索,包括根节点。您也可以致电 getElementsByClassName() 在任何元素上;它只会返回元素 它们是具有给定的指定根元素的后代 类名。

还有,

window.location.href 是一个属性,它会告诉 浏览器的当前 URL 位置。将属性设置为 不同的东西会重定向页面

window.location.href = actualItemLinks; // will redirect your page immediately when you assign some url to it

可能你正在寻找的是,

var elements = document.getElementsByClassName("item title_link"); // Get all elements that have both the 'item' and 'title_link' classes.It will return node list of elements.

var itemLinksHRefs = elements[0].href; // pick first hyperlinks href attr.

var actualItemHRefs = itemLinksHRefs.replace("#","");

var actualItemLinks = "http://www.domainname.com/index/" + actualItemHRefs + "/get";

window.location.href = actualItemLinks;

【讨论】:

    【解决方案2】:

    您正在尝试将位置设置为多个链接... 而不是.href,而是.getAttribute('href');

    var a,b,c,d, len;
    
    a = document.getElementsByClassName("item title_link");
    len = a.length;
    
    for (var i = 0; i <= len - 1; i++) {
        b = a[i].getAttribute('href');
        c = b.replace("#", "");
        d = "http://www.domainname.com/index/" + c + "/get";
        console.log(d); //just to check them out
    }
    
    //window.location.href = d;
    //or whatever you wanted to do with that...
    

    【讨论】:

    • 我忘了提到脚本将要导航的链接必须能够下载文件,当我使用该脚本时,它确实导航到正确的 url,但它没有开始下载来自每个 url 的文件。
    • 对每个链接使用第一个答案中的函数:stackoverflow.com/questions/3749231/…
    • 你能给我一个例子,说明我如何使用你发布的链接中的功能。就像我之前说的,我对 javascript 还不是很熟悉。
    • 只要把函数放到你的文件里,加一行downloadURL(d);正下方 d = ...
    • 当网页加载时,“title title_link”类元素需要几秒钟才能加载。有什么办法可以让循环延迟 6 秒?我需要能够延迟整个循环的启动,因为加载元素大约需要 4 秒。
    猜你喜欢
    • 1970-01-01
    • 2016-01-24
    • 2011-08-15
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    相关资源
    最近更新 更多