gpt4 book ai didi

javascript - 添加一个?另一个字符串指向所有出站链接

转载 作者:行者123 更新时间:2023-12-01 00:13:42 24 4
gpt4 key购买 nike

在此代码中,我将修改所有传出链接。我已经添加 ”?”成功地。我还想添加一个从 URL 获取的动态字符串,但失败了。这是我的代码:

fetch('https://httpbin.org/encoding/utf8')
.then((response) => {
return response.text();
})
.then((text) => {
document.getElementById("MyFetchedString").innerHTML = text.slice(0, 10);
});
document.addEventListener( "DOMContentLoaded", modify_outbound_links);

function modify_outbound_links(){
anchors = document.getElementsByTagName('a');
for (let i = 0; i < anchors.length; i++) {
let p = anchors[i].href;
if (p.indexOf('example.com') === -1) {
//How do i append the fetchedText here?
anchors[i].href = p + (p.indexOf('?') != -1 ? "&" : "?") + 'FetchedText';
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>

<body>

<h2>My First Web Page</h2>
<p>My First Paragraph.</p>

Modify all outbound links.<br>

<p><a href="https://google.com/">Google.com</a></p>
<p><a href="https://yahoo.com/">Yahoo.com</a></p>
<p><a href="https://example.com/">mydomain.com</a></p>

<p id="MyFetchedString"></p>

</body>

如何在此处附加 fetchedText:

anchors[i].href = p + (p.indexOf('?') != -1 ? "&" : "?") + '**I WANT THE FETCHED TEXT HERE**';

最佳答案

通过这 4 个步骤即可获得所需的功能 -

  1. 删除文档加载事件,因为在包含 fetch 时不需要该事件。

document.addEventListener( "DOMContentLoaded", modify_outbound_links);

This event listener interferes with fetch. It can be removed.

  • 将提取的文本提取到'提取请求'中名为fetchedText的变量中
  • fetchedText作为参数传递给modify_outbound_links函数
  • fetchedText附加到modify_outbound_links函数内的出站链接
  • 工作代码:

    <!DOCTYPE html>
    <html>
    <head>
    <title>My Website</title>
    </head>
    <body>
    <h2>My First Web Page</h2>
    <p>My First Paragraph.</p>
    Modify all outbound links.
    <br>
    <p><a href="https://google.com/">Google.com</a></p>
    <p><a href="https://yahoo.com/">Yahoo.com</a></p>
    <p><a href="https://example.com/">mydomain.com</a></p>
    <p id="MyFetchedString"></p>
    <script>
    fetch('https://httpbin.org/encoding/utf8')
    .then((response) => {
    return response.text();
    })
    .then((text) => {
    console.log("Response from httpbin = " + text)
    var fetchedText = text.slice(4, 16)
    document.getElementById("MyFetchedString").innerHTML = fetchedText
    modify_outbound_links(fetchedText)
    })

    function modify_outbound_links(fetchedText) {
    anchors = document.getElementsByTagName('a')
    for (let i = 0; i < anchors.length; i++) {
    let p = anchors[i].href
    if (p.indexOf('example.com') === -1) {
    //How do i append the fetchedText here?
    anchors[i].href = p + (p.indexOf('?') != -1 ? "&" : "?") + fetchedText;
    }
    }
    }
    </script>
    </body>
    </html>

    输出

    My First Web Page My First Paragraph.

    Modify all outbound links.

    Google.com (link = https://google.com/?Unicode%20Demo)

    Yahoo.com (link = https://yahoo.com/?Unicode%20Demo)

    mydomain.com (link = https://example.com/)

    Unicode Demo

    关于javascript - 添加一个?另一个字符串指向所有出站链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59913032/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com