Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ahy4/2805e1e0938f604240d5f1a6c5d39e3b to your computer and use it in GitHub Desktop.
Save ahy4/2805e1e0938f604240d5f1a6c5d39e3b to your computer and use it in GitHub Desktop.
// @ts-check
// 広告ブロック
(async () => {
const SLEEP_TIME = 0;
const LONG_SLEEP_TIME = 3000;
/**
* @param {number} msec
* @returns {Promise<void>}
*/
const sleep = (msec) => new Promise((resolve) => setTimeout(resolve, msec));
/**
* Find an ancestor element with a specified attribute.
* @param {HTMLElement} startElement
* @param {string} attributeName
* @param {string} attributeValue
* @returns {HTMLElement | null}
*/
function findAncestorWithAttribute(
startElement,
attributeName,
attributeValue
) {
let currentElement = startElement;
while (currentElement) {
if (currentElement.getAttribute(attributeName) === attributeValue) {
return currentElement;
}
currentElement = currentElement.parentElement;
}
return null;
}
/**
* Clicks an element if it exists.
* @param {Element | null} element
* @returns {Promise<boolean>}
*/
async function clickIfExist(element) {
if (element instanceof HTMLElement) {
element.click();
await sleep(SLEEP_TIME);
return true;
}
return false;
}
while (true) {
await sleep(LONG_SLEEP_TIME);
const spans = document.querySelectorAll("span");
for (const span of spans) {
if (!span.textContent.includes("プロモーション")) continue;
const ancestor = findAncestorWithAttribute(
span,
"data-testid",
"cellInnerDiv"
);
if (!ancestor) continue;
const screenName = Array.from(
ancestor.querySelectorAll('div[data-testid="User-Name"] span')
).find((span) => span.textContent.startsWith("@"));
const isGoldVerified = ancestor.querySelector('g[fill-rule="evenodd"]');
if (isGoldVerified) {
console.log(`スキップ: twitter.com/${screenName?.textContent}`);
continue;
}
const dots = ancestor.querySelector(
'[data-testid="caret"][aria-label="もっと見る"]'
);
if (!(await clickIfExist(dots))) continue;
const dropdown = document.querySelector('[data-testid="Dropdown"]');
const aboutThisAd = dropdown?.querySelector('a[data-testid="whyThisAd"]');
if (!aboutThisAd) continue;
const block = dropdown.querySelector('[data-testid="block"]');
if (!(await clickIfExist(block))) continue;
const blockDialog = document.querySelector(
'[data-testid="confirmationSheetConfirm"]'
);
await clickIfExist(blockDialog);
console.log(`ブロック完了: twitter.com/${screenName?.textContent}`);
await sleep(200)
}
}
})();
// @ts-check
setInterval(() => {
if (location.href !== 'https://x.com/i/verified-get-verified') {
return
}
const butttonAll = document.querySelectorAll('button')
const targetButton = Array.from(butttonAll).find(b=>b.innerText.includes('後で試す'))
if (targetButton) {
targetButton.click()
}
}, 15)
@ahy4
Copy link
Author

ahy4 commented Oct 25, 2023

script auto runner で動かすようお

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment