Discussions » Development

JS help for beginner - Remove amazon sponsored results

§
Posted: 03.08.2017

JS help for beginner - Remove amazon sponsored results

Hi,
the sponsored results in amazon search are only identifyable at a low level, so it is not possible to remove them with CSS / adblocker.

I'm a JS noob but still tried to write a script to remove the ads but I don't get a console output:

// ==UserScript==
// @name        Amazon sponsored
// @namespace   *
// @description Removes sponsored search results
// @include     https://www.amazon.de/s?*
// @version     1
// @grant       none
// ==/UserScript==

function remSponsored() {
  var sponsored = document.getElementsByClassName("s-sponsored-list-header").parentNode.parentNode.parentNode.parentNode.parentNode;
  // following for loop on HTMLcollection should work in ES6 according to stackoverflow
  for (var item of sponsored) {
    item.id.setAttribute("style", "display:none;");
    // yes, I want to overwrite and don't know if the "style" attribute is already present. Not using .style.display
  }
  console.log("Advertising search results removed.");
}

remSponsored();

I'd appreciate if anyone could help.
Thanks,
Graphen

Deleted user 20822
§
Posted: 03.08.2017

You should try your code with the debug console (Ctrl-Alt-K). sponsored is not a collection.

PS. Those elements can be blocked by "uBlock Origin", with the rule amazon.*###s-results-list-atf > .s-result-item:has(:scope > .s-item-container h5.s-sponsored-list-header) in the built-in list "uBlock filters".

§
Posted: 04.08.2017

Thanks for the hint, I could now make it work like this:

function remSponsored() {
  var sponsored = document.getElementsByClassName("s-sponsored-list-header");
  
  for (var i = 0; i < sponsored.length; i++) {
    sponsored[i].parentNode.parentNode.parentNode.parentNode.parentNode.setAttribute("style", "display:none;");
    // yes, I want to overwrite and don't know if the "style" attribute is already present. Not using .style.display
  }
  console.log("GM Userscript: Advertising search results removed.");
}

remSponsored();

Although that uBlock rule might be another push for me to finally switch from ABP to uBO.

[SOLVED]

Post reply

Sign in to post a reply.