Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automaticly report all Promoted Tweets with "illegal service".
// ==UserScript==
// @name Twitter Vanced
// @version 3.4
// @description try to take over the world!
// @include https://twitter.com/*
// @author github.com/catSIXe (cat6e)
// ==/UserScript==
(function() {
'use strict';
//var processedLists = new WeakMap();
const toProcessList = []
let currentStep = 0, currentAd = null
/*
step 0 = finding ads
step 1.. = processing steps for the handling of adverts
*/
const processedAds = {}
var terminateAdvertisements = function () {
const tweets = document.getElementsByTagName("article")
let adIndex = 0
for (var i = 0; i < tweets.length; i++) { try {
const article = tweets[i]
if (article.childElementCount < 1 || article.innerText.indexOf('Promoted') === -1) continue
//if (!article.querySelector('svg[aria-label="Verified account"]')) continue
const identifier = article.outerText
if (!!processedAds[identifier]) return
processedAds[identifier] = true
toProcessList.push(article)
console.log('queued Advert', article, identifier)
} catch(e){ console.error(e) }}
};
var ticker = () => {
switch(currentStep) {
case 0:
if (toProcessList.length > 0) {
currentAd = toProcessList.shift()
console.log('terminating Advert', currentAd)
currentStep = 1
}
break;
case 1:
console.log('[twitter vanced] > Extending Advert Menu')
currentAd.querySelector('[aria-label="More"]').click()
currentStep = 2
break;
case 2:
console.log('[twitter vanced] > Report Ad')
if (!document.querySelector('[role="menu"]')) {
console.log('[twitter vanced] > menu is not open, going back to step 0')
currentStep = 0;
break
}
document.querySelector('div[data-testid="report"]>div:last-child > div > span').click()
currentStep = 3
break;
case 3:
console.log('[twitter vanced] > Waiting for finished Report')
let f1 = document.querySelector('div[aria-labelledby="modal-header"] > div > div > div')
if (!!f1 && !!f1.querySelector('[aria-label="Close"]')) {
let f2 = document.querySelector('div[aria-labelledby="modal-header"]')
if (!!f2 && !!f2.querySelector('[aria-label="Close"]')) {
try {
f2.querySelector('[aria-label="Close"]').click()
} catch (e) {
console.error(e)
} finally {
currentStep = 4
}
}
}
case 4:
console.log('[twitter vanced] > Testing if Modal is open')
if (!document.querySelector('div[aria-labelledby="modal-header"] > div > div > div')) {
console.log('[twitter vanced] > test passed')
currentStep = 5
} else {
console.log('[twitter vanced] > test failed, going back to step 3')
currentStep = 3
}
break;
case 5:
console.log('[twitter vanced] > going to next advert')
currentStep = 0;
break;
}
}
setInterval(ticker, 500)
const amIinTheReportWindow = window.location.href.indexOf('https://twitter.com/i/safety/report_story') >= 0
if (amIinTheReportWindow === false) {
var DOMObserverTimer = false
var DOMObserverConfig = {
attributes: true,
childList: true,
subtree: true
}
var DOMObserver = new MutationObserver(function () {
if (DOMObserverTimer !== 'false') {
clearTimeout(DOMObserverTimer);
}
DOMObserverTimer = setTimeout(function () {
DOMObserver.disconnect();
terminateAdvertisements();
DOMObserver.observe(document.body, DOMObserverConfig);
}, 100);
});
DOMObserver.observe(document.body, DOMObserverConfig);
terminateAdvertisements();
} else {
setInterval(() => {
document.querySelector('#illegal-btn').click()
}, 500)
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment