How can you replace the target of a link in Greasemonkey?

I am trying to write a script in Greasemonkey that will replace the target of the link with something else, but with my limited knowledge of Javascript I really don't know how to do this.

Basically, I try to find all links containing a certain string of characters (ex: // a [contains (@href, 'xx')]) and either replace them with another link or add something to them (replacing "abc123." com "at" zyx987.com "or" abc123.com "at" abc123.com/folder)).

If you could point me on the right path, I would really appreciate it.

edit: This is working code if someone has the same question in the future:

  var links, thisLink;
 links = document.evaluate ("// a [contains (@href, 'roarrr')]",
     document
     null
     XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
     null);
 for (var i = 0; i <links.snapshotLength; i ++) {
     var thisLink = links.snapshotItem (i);
     thisLink.href + = 'test.html';
 }
+4
source share
2 answers
var links = document.evaluate("//a[contains(@href, 'roarrr')]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); for (var i=0; i < links.snapshotLength; i++) { var thisLink = links.snapshotItem(i); thisLink.href += 'test.html'; } 
+2
source

You get the desired a element and set them src as follows:

 elem.src = 'http://example.com'; 

you can also use the previous src value:

 elem.src += 'index.html'; 
+1
source

Source: https://habr.com/ru/post/1307491/


All Articles