Created
May 12, 2023 09:56
-
-
Save alekc/30e0ac329ebc8fcf6744431b13c24c92 to your computer and use it in GitHub Desktop.
Autotrader.co.uk userscript hiding certain cars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name New Userscript | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match https://www.autotrader.co.uk/car-search* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=autotrader.co.uk | |
// @grant none | |
// ==/UserScript== | |
const storage = getStorage(); | |
(function() { | |
'use strict'; | |
console.log("storage contents", storage); | |
const mainDiv = document.getElementById("root"); | |
let observer = new MutationObserver(callback); | |
observer.observe(mainDiv, { | |
childList: true, | |
attributes: false, | |
characterData: true, | |
subtree: true | |
}); | |
})(); | |
function getStorage(){ | |
const item = localStorage.getItem("hidden-adverts"); | |
if (!item) { | |
return {}; | |
} | |
return JSON.parse(item) | |
} | |
function callback(xx){ | |
//console.log("callback") | |
//delete ads | |
const nodes = getElementsByXpath("//li[./section/span[text()='Ad']]"); | |
for (let i=0; i<nodes.snapshotLength; i++) { | |
let section = nodes.snapshotItem(i); | |
section.style.display = "none"; | |
} | |
appendDelete(); | |
} | |
function appendDelete(){ | |
let nodes = getElementsByXpath("//li/section"); | |
for (let i=0; i<nodes.snapshotLength; i++) { | |
let section = nodes.snapshotItem(i); | |
const sectionID = section.getAttribute("id"); | |
//console.log(storage); | |
//debugger; | |
if (storage[sectionID]){ | |
setElementAsHidden(section); | |
continue; | |
} | |
if (section.querySelector("img.delete")){ | |
console.log("got image already"); | |
continue; | |
} | |
section.style.color = "#f1c40f"; | |
// console.log(section); | |
const deleteButton = document.createElement("img"); | |
deleteButton.classList.add("delete"); | |
deleteButton.setAttribute("src", "https://cdn-icons-png.flaticon.com/24/1828/1828851.png"); | |
deleteButton.style.position = "absolute"; | |
deleteButton.style.top = "5px"; | |
deleteButton.style.right = "5px"; | |
deleteButton.style.zIndex = 1000; | |
deleteButton.setAttribute("data-id",sectionID); | |
deleteButton.addEventListener("click", hideAdvert); | |
section.appendChild(deleteButton); | |
//console.log("Added image", deleteButton); | |
} | |
/* | |
while (section){ | |
const script = document.createElement("img"); | |
script.setAttribute("src", "https://cdn-icons-png.flaticon.com/24/1828/1828851.png"); | |
section.appendChild(script); | |
console.log(section); | |
section = nodes.iterateNext(); | |
}*/ | |
console.log(nodes); | |
} | |
function hideAdvert(obj){ | |
const target = obj.target; | |
const id = obj.target.getAttribute("data-id"); | |
var section = document.getElementById(id); | |
setElementAsHidden(section); | |
storage[id] = true; | |
localStorage.setItem("hidden-adverts", JSON.stringify(storage)); | |
console.log(storage); | |
} | |
function setElementAsHidden(el){ | |
el.classList.add("hidden-advert"); | |
el.style.display = "none"; | |
} | |
function getElementByXpath(path) { | |
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; | |
} | |
function getElementsByXpath(path) { | |
return document.evaluate(path, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment