изменить все исходящие ссылки - PullRequest
0 голосов
/ 25 января 2020

Я хотел бы изменить все исходящие ссылки на веб-странице. Я просто хочу добавить динамическую c строку в конце каждого URL. Я использую fetch, чтобы получить эту строку. Пока я могу выбрать и изменить URL, но не могу добавить выбранную строку.

Мой код

<!DOCTYPE html>
<html>
<head>
  <title>My Website</title>
<script>
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? 
            anchors[i].href = p + (p.indexOf('?') != -1 ? "&" : "?") + 'FetchedText'; 
        }
    }
} </script>
  </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>

Пожалуйста, помогите мне добавить полученная строка для каждого исходящего URL. Моя главная задача - эта JavaScript строка: якоря [i] .href = p + (p.indexOf ('?')! = -1? "&": "?") + ' FetchedText Как мне добавить его сюда? ';

1 Ответ

0 голосов
/ 25 января 2020

Я думаю, что вы ищете что-то вроде этого, я заметил, что вы удалили старую ссылку из API, но я положил ее обратно, если вам нужно, чтобы она работала с кодом.

document.addEventListener( "DOMContentLoaded", modify_outbound_links);

function modify_outbound_links(){

  fetch('https://api.exchangeratesapi.io/latest?symbols=USD,GBP')
  .then((response) => {
    return response.json();
  })
  .then((json) => {

     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? 
            anchors[i].href = p + (p.indexOf('?') != -1 ? "&" : "?") + 'FetchedText&' + 'USD='+json['rates']['USD'] + '&GBP=' + json['rates']['GBP']; 
        }
    }
    document.getElementById("MyFetchedString").innerHTML = text.slice(0, 10);
  });
}
<!DOCTYPE html>
<html>
<head>
  <title>My Website</title>
<script>
 </script>
  </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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...