-
-
Save rtfpessoa/7fdd3d4121ee4acafe56e8425154888a to your computer and use it in GitHub Desktop.
// ID of the config, e.g. A1234BCD. | |
const configID = "A1234BCD"; | |
// API key, found at the bottom of your account page in https://my.nextdns.io/account | |
const APIKey = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
// Mark true or false. If true, failed links will be retried 3 times at progressively increasing intervals. | |
// If false, failed links will not be retried. | |
const retryFailedLinks = true; | |
// Time delay between requests in milliseconds. | |
// 800 seems to not give any errors but is rather slow while anything faster will give errors (in limited testing). | |
// If you want to go lower, it is recommended that "retryFailedLinks" is true | |
const timeDelay = 200; | |
const ignoreDomainsSet = new Set([ | |
"clients.l.google.com", | |
"clients1.google.com", | |
"clients2.google.com", | |
"clients3.google.com", | |
"clients4.google.com", | |
"clients5.google.com", | |
"clients6.google.com", | |
"akamaiedge.net", | |
]); | |
const youtubeAdDomainsSet = new Set(); | |
const existingLinkSet = new Set(); | |
const fetchEwprattenDomains = async () => { | |
const response = await fetch( | |
"https://raw.githubusercontent.com/Ewpratten/youtube_ad_blocklist/master/blocklist.txt" | |
); | |
const text = await response.text(); | |
text.split("\n").forEach((line) => youtubeAdDomainsSet.add(line)); | |
return; | |
}; | |
const fetchkboghdadyDomains = async () => { | |
const response = await fetch( | |
"https://raw.githubusercontent.com/kboghdady/youTube_ads_4_pi-hole/master/youtubelist.txt" | |
); | |
const text = await response.text(); | |
text.split("\n").forEach((line) => youtubeAdDomainsSet.add(line)); | |
return; | |
}; | |
const fetchGoodbyeAdsDomains = async () => { | |
const response = await fetch( | |
"https://raw.githubusercontent.com/jerryn70/GoodbyeAds/master/Formats/GoodbyeAds-YouTube-AdBlock-Filter.txt" | |
); | |
const text = await response.text(); | |
text.split("\n").forEach((line) => { | |
if (line.startsWith("||") && line.endsWith("^")) { | |
const domain = line.substring(2, line.length - 1); | |
youtubeAdDomainsSet.add(domain); | |
} | |
}); | |
return; | |
}; | |
const fetchExistingLinks = async () => { | |
const response = await fetch( | |
`https://api.nextdns.io/profiles/${configID}/denylist`, | |
{ | |
headers: { | |
accept: "application/json, text/plain, */*", | |
"content-type": "application/json", | |
"X-Api-Key": JSON.stringify(APIKey), | |
}, | |
method: "GET", | |
mode: "cors", | |
credentials: "include", | |
} | |
); | |
const domains = await response.json(); | |
domains.data.map((domain) => { | |
existingLinkSet.add(domain.id); | |
}); | |
return; | |
}; | |
function sleep(milliseconds) { | |
return new Promise((resolve) => setTimeout(resolve, milliseconds)); | |
} | |
const blockDomain = async (domain) => | |
fetch(`https://api.nextdns.io/profiles/${configID}/denylist`, { | |
headers: { | |
accept: "application/json, text/plain, */*", | |
"content-type": "application/json", | |
"X-Api-Key": JSON.stringify(APIKey), | |
}, | |
body: JSON.stringify({ id: domain, active: true }), | |
method: "POST", | |
mode: "cors", | |
credentials: "include", | |
}).then((response) => { | |
if (response.ok) { | |
return response.json; | |
} | |
throw "Failed to block domain"; | |
}); | |
const blockDomains = async (domains, delay = timeDelay) => { | |
console.log(`Preparing to block ${domains.length} domains`); | |
const failedLinksSet = new Set(); | |
for (let idx = 0; idx < domains.length; idx++) { | |
const domain = domains[idx]; | |
if (ignoreDomainsSet.has(domain)) { | |
console.log(`Ignoring ${domain} ${idx + 1}/${domains.length}`); | |
continue; | |
} | |
if (existingLinkSet.has(domain)) { | |
console.log(`Skipping ${domain} ${idx + 1}/${domains.length}`); | |
continue; | |
} | |
try { | |
console.log(`Blocking ${domain} ${idx + 1}/${domains.length}`); | |
await blockDomain(domain); | |
} catch (error) { | |
console.error(error); | |
failedLinksSet.add(domain); | |
} | |
await sleep(delay); | |
} | |
return Array.from(failedLinksSet); | |
}; | |
const stringToHex = (str) => | |
str | |
.split("") | |
.map((c) => c.charCodeAt(0).toString(16).padStart(2, "0")) | |
.join(""); | |
const unblockDomain = async (domain) => | |
fetch( | |
`https://api.nextdns.io/profiles/${configID}/denylist/hex:${stringToHex( | |
domain | |
)}`, | |
{ | |
headers: { | |
accept: "application/json, text/plain, */*", | |
"content-type": "application/json", | |
"X-Api-Key": JSON.stringify(APIKey), | |
}, | |
body: null, | |
method: "DELETE", | |
mode: "cors", | |
credentials: "include", | |
} | |
); | |
const unblockDomains = async (domains, delay = timeDelay) => { | |
console.log(`Preparing to unblock ${domains.length} domains`); | |
for (let idx = 0; idx < domains.length; idx++) { | |
try { | |
console.log(`Unblocking ${domains[idx]} ${idx}/${domains.length}`); | |
await unblockDomain(domains[idx]); | |
} catch (error) { | |
console.error(error); | |
} | |
await sleep(delay); | |
} | |
}; | |
const retry = async (fn, initialInput, retries, delayMs) => { | |
let attempt = 0; | |
let delay = timeDelay; | |
let nextInput = initialInput; | |
while (true) { | |
nextInput = await fn(nextInput, delay); | |
if (nextInput.size == 0) { | |
return; | |
} | |
console.log(`Retry ${attempt}/${retries} failed. Retrying again...`); | |
if (attempt++ > retries) { | |
console.error("Failed domains", nextInput); | |
throw "Retries exceeded"; | |
} | |
delay += delayMs * attempt; | |
} | |
}; | |
const run = async () => { | |
console.log(`Downloading domains to block ...`); | |
await fetchEwprattenDomains(); | |
await fetchkboghdadyDomains(); | |
await fetchGoodbyeAdsDomains(); | |
await fetchExistingLinks(); | |
const youtubeAdDomains = Array.from(youtubeAdDomainsSet); | |
const retries = retryFailedLinks ? 3 : 0; | |
await retry(blockDomains, youtubeAdDomains, retries, 100); | |
// await unblockDomains(Array.from(existingLinkSet)); | |
console.log("Have fun!"); | |
}; | |
run(); |
Thanks ducktapeonmydesk. That updated code did the trick. For anyone else reading, I changed:
for (let idx = 0; idx < 1300; idx++) {
to:
for (let idx = 0; idx < youtubeAdDomains.length; idx++) {
so it adds all the domains.
Whoops, it should have been that! I was doing some testing with the retries and didn't want to wait for the entire thing. Glad it worked for you. But it looks to have been updated and written better by someone anyways :)
Is this actually working for anyone?
I have run the script and the addresses are added to the denylist in NextDNS - but they don't block anything. Youtube shows just as many ads as before.
same problem i only stuck denylist and browser crashing when im trying to enter on that page. also there is no bulk remove so its pain in the ass to remove few thousands links manualy pffff
same problem i only stuck denylist and browser crashing when im trying to enter on that page. also there is no bulk remove so its pain in the ass to remove few thousands links manualy pffff
To remove, comment out line 203, uncomment line 205.
ut line 203, uncomment line 205.
tnx man
same problem i only stuck denylist and browser crashing when im trying to enter on that page. also there is no bulk remove so its pain in the ass to remove few thousands links manualy pffff
To remove, comment out line 203, uncomment line 205.
Thank you! glad to know there is an easy way to undo this :)
by the way, why no one creates an official list and add it to NextDNS that is only and specifically for blocking YouTube ads? why do we have to do it like this?
This is a spiffy way to 'subscribe' to lists on demand. I had been hoping I could chain decloudus (DC) to nextdns (ND) for pattern matching on DC and subscriptions on ND. DC profile allows specifying an upstream resolver in ipv4 or ipv6. ND profile provides unique ipv6, but DC rejects as invalid.
Could the browser be crashing because the whole list is grabbed instead of chunks? or all lists into ram?
Are we using the posted gist or the ducktapeonmydesk + MarkDarwin mod?
This is a spiffy way to 'subscribe' to lists on demand. I had been hoping I could chain decloudus (DC) to nextdns (ND) for pattern matching on DC and subscriptions on ND. DC profile allows specifying an upstream resolver in ipv4 or ipv6. ND profile provides unique ipv6, but DC rejects as invalid.
Could the browser be crashing because the whole list is grabbed instead of chunks? or all lists into ram?
Are we using the posted gist or the ducktapeonmydesk + MarkDarwin mod?
The posted gist. Each list is being grabbed, combined into one "master" list, and then each URL is added to the deny list in NextDNS. Lists remain true, no manipulation.
The only "mod" I had was commenting out certain lists so they wouldn't be run.
For testing purposes, because I didn't want to wait for ~100k lines to be added to the deny list, I had it set to "finish" at 1300 lines. I forgot to change the code back to run the entire list before posting. MarkDarwin caught my mistake and fixed it.
As far as browser crashing: Each list is grabbed in it's entirety before being combined. The amount of data could be causing your browser to crash. If you want to test it with only one of the lists, you can comment out any combination of lines 195 through 197.
Sadly, doesn’t seem to work for the iOS, iPadOS and tvOS apps.
Sadly, doesn’t seem to work for the iOS, iPadOS and tvOS apps.
no need to use those awful OSes. use Windows or Android only.
Sadly, doesn’t seem to work for the iOS, iPadOS and tvOS apps.
no need to use those awful OSes. use Windows or Android only.
Thank you for your unwanted opinion :)
Updated