简体   繁体   English

如何使用 JavaScript 从 href 中查找特定链接?

[英]How to find a specific link from an href using JavaScript?

This is my first experience with JS, so please forgive the noob question.这是我第一次接触JS,所以请原谅菜鸟问题。 I'm trying to make a userscript for a phpBB forum that'll allow me to automatically bookmark every topic I create.我正在尝试为 phpBB 论坛制作一个用户脚本,它允许我自动为我创建的每个主题添加书签。

My approach is to add an onclick listener to the submit button.我的方法是在提交按钮上添加一个 onclick 监听器。 I'll use the code found in another question:我将使用在另一个问题中找到的代码:

    var submit = document.getElementsByTagName('input')[0];
    submit.onclick = function() {
        ;
    }

Before that though I want to find a link to bookmarking the topic in one of the hrefs on the page and store it as a variable.在此之前,虽然我想在页面上的一个 href 中找到一个链接来为主题添加书签并将其存储为变量。

I know that it will always take the form of我知道它总是会采取以下形式

<a href="./viewtopic.php?f=FORUM_NUMBER&amp;t=TOPIC_NUMBER&amp;bookmark=1&amp;hash=HASH"

The final code should look something like (hopefully it's the correct form)最终代码应该看起来像(希望它是正确的形式)

var link = THE LINK EXTRACTED FROM THE MATCHED HREF
var submit = document.getElementsByTagName('input')[0];
submit.onclick = function() {
    setTimeout(function(){ window.location.href = 'link'; }, 1000);
}

My issue is I don't know how to approach locating the href that I need and getting the link from it.我的问题是我不知道如何找到我需要的 href 并从中获取链接。 Haven't found any similar questions about this.没有发现任何类似的问题。

Thanks in advance for any help提前感谢您的帮助

You can try document.getElementsByTagName("a") ;你可以试试document.getElementsByTagName("a") ; which returns a collection of all the <a></a> loaded in the dom.它返回 dom 中加载的所有<a></a>的集合。 Then you can find it in the list and and use.href to get it's href attribute.然后您可以在列表中找到它并使用.href 来获取它的 href 属性。

Maybe something like this?也许是这样的?

var anchors = document.getElementsByTagName('a'); // get all <a> tags
var link = '';
if (anchors) {
  // getAttribute(attributeName) gets the value of attributeName (in your case, the value of 'href' attribute
  // .map(), .find() and .filter() are available methods for arrays in JS
  // .startsWith() is an available method for matching strings in JS.
  // You can even experiment with other regex-based string matching methods like .match()

  // Use one of the following lines, based on what you require:

  // to get the first matching href value
  link = anchors.map(anchor => anchor.getAttribute('href')).find(url => url.startsWith('./viewtopic.php')); // or provide a better regex pattern using .match()

  // to get all matching href values as an array
  link = anchors.map(anchor => anchor.getAttribute('href')).filter(url => url.startsWith('./viewtopic.php')); // or provide a better regex pattern using .match()
}

Since you're not new to coding, check out this documentation if you're new to JS:)由于您对编码并不陌生,因此如果您是 JS 新手,请查看此文档:)

Happy coding!快乐编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM