eBay - Block sponsored listings

Blocks ebay sponsored listings by checking whether the 'Sponsored' text is in the bounds of the 'Sponsored' svg image.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name             eBay - Block sponsored listings
// @description      Blocks ebay sponsored listings by checking whether the 'Sponsored' text is in the bounds of the 'Sponsored' svg image.
// @license          MIT
// @match            https://www.ebay.com/sch/*
// @match            https://www.ebay.at/sch/*
// @match            https://www.ebay.ca/sch/*
// @match            https://www.ebay.ch/sch/*
// @match            https://www.ebay.com.au/sch/*
// @match            https://www.ebay.com.hk/sch/*
// @match            https://www.ebay.com.my/sch/*
// @match            https://www.ebay.com.sg/sch/*
// @match            https://www.ebay.co.uk/sch/*
// @match            https://www.ebay.de/sch/*
// @match            https://www.ebay.es/sch/*
// @match            https://www.ebay.fr/sch/*
// @match            https://www.ebay.ie/sch/*
// @match            https://www.ebay.it/sch/*
// @match            https://www.ebay.nl/sch/*
// @match            https://www.ebay.pl/sch/*
// @version          1.0
// @namespace https://greasyfork.org/users/1508855
// ==/UserScript==

function isSponsored(listing) {

  var element = listing.querySelector('[style*="data:image/svg+xml"]');

  if (!element) {
    return null;
  }

  var style = element.style.backgroundImage;
  var base64 = style.split('base64,')[1].split('"')[0];

  var svgXml = atob(base64);

  var parser = new DOMParser();
  var svgDoc = parser.parseFromString(svgXml, 'image/svg+xml');
  var textElement = svgDoc.querySelector('text');

  var x = parseFloat(textElement.getAttribute('x'));
  x = x + parseFloat(textElement.getAttribute('dx'));

  var y = parseFloat(textElement.getAttribute('y'));
  y = y + parseFloat(textElement.getAttribute('dy'));

  var transform = textElement.getAttribute('transform');
  if (transform && transform.startsWith('translate')) {
      var matches = transform.match(/translate\(([-\d.]+) ([-\d.]+)\)/);
      if (matches) {
          x = x + parseFloat(matches[1]);
          y = y + parseFloat(matches[2]);
      }
  }

  return (x < 100 && x > -100 && y < 100 && y > -100);
}

var selectors = ['.s-item', '.s-card'];
var listings = [];
for (const selector of selectors) {
  listings.push(...document.querySelectorAll(selector));
}

var sponsored = 0;
listings.forEach(item => {
  if (isSponsored(item)) {
    sponsored = sponsored + 1;
    //item.style.backgroundColor = 'red';
    item.style.display = 'none';
  }
});

console.log("Hid", sponsored, "/", listings.length, " elements that matched the 'sponsored listings' test.");