Search code examples
javascriptgreasemonkey

Script to search inside links in a webpage


Is it possible to write a script in greasemonkey that will find specific/mentioned keyword on the list of given links for example a page has 10 links and am looking for word "ramp" and on those 10links can it go and highlight the links that has word "ramp" ?


Solution

  • I can't comment as I don't have enough reputation. As the other say, your question should not be asked this way. I assume that you don't know Javascript, so here is a quick and dirty code that solves your issue.

    "use strict";
    var links = document.getElementsByTagName("a");
    for (let i=0; i<links.length; i++) {
        let link = links[i];
        link.innerHTML = link.innerHTML.replace("ramp", "<span style='background-color: yellow;'>ramp</span>");
    }
    

    Now that you have the code, I advice you to spend some time on the following links: https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML https://developer.mozilla.org/en-US/docs/Web/HTML/Element/span