简体   繁体   中英

Change `href`s links via `onmousedown`,`click` events

Basically, when I do a search in Yandex, I get something like this:

PIC .

However, when I right-click and copy the link I don't get the real link, but the junky one (the one which works as intermediate with the function of tracking my click before redirecting to the link I want).

I'm trying to code a script which removes those junky links. This is what I've tried:

function decodeURL(str) {
   return decodeURIComponent((str+'').replace(/\+/g, '%20'));
}

function getRealLinks() {
    var classes = document.getElementsByClassName('organic typo typo_text_m typo_line_s'); // gets any search result

    var cleanLinks = new Array(); // we'll store any clean link to this array

    for (i = 0; i < classes.length; i++) {
        var html = classes[i].innerHTML; // we get part of the inner html of the selected search result
        var pattern = /(url=(.*?))(?=&amp;\w+=)/; // with this regex we'll match the url
        var match = pattern.exec(html); // attempt matching pattern
        cleanLinks.push(decodeURL(match[2])); // decode the url, then push the match to the array
    }

    console.log(cleanLinks);
}

function bindEventListeners() {
    var lis = document.getElementsByTagName('li'); // search results have `li` selector

    for (var i = 0; i < lis.length; i++) { // iterate every element
      (function(i) {
        lis[i].addEventListener('click', function() { // add a listener to the current element
          alert(this.dataset.cid); // each result has an id (by default between 0-9)
        }, false);
      })(i);
    }
}

bindEventListeners();

getRealLinks() gives me the correct links, but I don't know now how to "progress" to replace the href s of the search results with the clean links.

bindEventListeners() as it's called, binds listeners that will be triggered when a click is realized on the search result body portion. It will be useful to me to get the id of the clicked search result, just to let deviate the links to the one I need. For example, the user clicks the search result with index 3 , I'll do var search_3 = getRealLinks()[3] .

How can I replace those links you can see with the clean ones in JavaScript?

Attach a mousedown listener on window , the topmost DOM object, and specify true for the useCapture parameter of addEventListener to intercept the event at the very start of the capturing phase from window to the click target. That is, our code will run before the target's onmousedown .

// ==UserScript==
// @name     Defluff yandex links
// @include  *://yandex.*/*
// ==/UserScript==

window.addEventListener('mousedown', event => {
    const a = event.target.closest('a');
    if (a) {
        a.onmousedown = null;
    }
}, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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