116 lines
3.7 KiB
JavaScript
116 lines
3.7 KiB
JavaScript
const gatekeepers = {
|
|
// "here-goes-hostname-of-your-local-searx-instance": "searx",
|
|
"html.duckduckgo.com": "duckduckgo"
|
|
};
|
|
function prepare_searx(hosts, port) {
|
|
port.onMessage.addListener(function(data) {
|
|
hosts[data.hostname].cloudfucked = data.cloudfucked;
|
|
hosts[data.hostname].links.forEach(link => {
|
|
if (!link.querySelector('.cloudfuck_market')) {
|
|
let market = document.createElement('img');
|
|
market.classList.add('cloudfuck_market');
|
|
market.setAttribute('data-test', data.cloudfucked);
|
|
if (data.cloudfucked) {
|
|
market.alt = 'cloudflare';
|
|
market.src = browser.runtime.getURL('images/pure-evil.png');
|
|
market.style.height = '1em';
|
|
market.style.marginRight = '0.5em';
|
|
}
|
|
link.prepend(market);
|
|
}
|
|
});
|
|
});
|
|
function getPages() {
|
|
return document.querySelectorAll('#main_results hr').length + 1;
|
|
}
|
|
let pages = getPages();
|
|
parse_searx(hosts, port);
|
|
addEventListener('scroll', function() {
|
|
if (getPages() > pages) {
|
|
parse_searx(hosts, port);
|
|
pages = getPages();
|
|
}
|
|
});
|
|
}
|
|
function parse_searx(hosts, port) {
|
|
Array.from(document.querySelectorAll('.result-default'))
|
|
.forEach(function(el) {
|
|
let link = el.querySelector('a');
|
|
let hostname = new URL(link.href).hostname;
|
|
if (!(hostname in hosts)) {
|
|
hosts[hostname] = {
|
|
"links": [link],
|
|
"cloudfucked": null
|
|
};
|
|
} else {
|
|
hosts[hostname].links.push(link);
|
|
}
|
|
});
|
|
for (hostname in hosts) {
|
|
let host = hosts[hostname];
|
|
if (host.cloudfucked === null) {
|
|
port.postMessage({
|
|
"hostname": hostname
|
|
});
|
|
}
|
|
}
|
|
}
|
|
// TODO: optimize code for search engines modules
|
|
function prepare_duckduckgo(hosts, port) {
|
|
port.onMessage.addListener(function(data) {
|
|
hosts[data.hostname].cloudfucked = data.cloudfucked;
|
|
hosts[data.hostname].links.forEach(link => {
|
|
if (!link.querySelector('.cloudfuck_market')) {
|
|
let market = document.createElement('img');
|
|
market.classList.add('cloudfuck_market');
|
|
market.setAttribute('data-test', data.cloudfucked);
|
|
if (data.cloudfucked) {
|
|
market.alt = 'cloudflare';
|
|
market.src = browser.runtime.getURL('images/pure-evil.png');
|
|
market.style.height = '1em';
|
|
market.style.marginRight = '0.5em';
|
|
}
|
|
link.prepend(market);
|
|
}
|
|
});
|
|
});
|
|
parse_duckduckgo(hosts, port);
|
|
}
|
|
function parse_duckduckgo(hosts, port) {
|
|
Array.from(document.querySelectorAll('.result__a'))
|
|
.forEach(function(link) {
|
|
let hostname = new URL(link.href).hostname;
|
|
if (!(hostname in hosts)) {
|
|
hosts[hostname] = {
|
|
"links": [link],
|
|
"cloudfucked": null
|
|
};
|
|
} else {
|
|
hosts[hostname].links.push(link);
|
|
}
|
|
});
|
|
for (hostname in hosts) {
|
|
let host = hosts[hostname];
|
|
if (host.cloudfucked === null) {
|
|
port.postMessage({
|
|
"hostname": hostname
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
let hosts = [];
|
|
let port = browser.runtime.connect();
|
|
switch (gatekeepers[location.hostname]) {
|
|
case 'duckduckgo':
|
|
if (document.body.classList.contains('body--html')) {
|
|
prepare_duckduckgo(hosts, port);
|
|
}
|
|
break;
|
|
case 'searx':
|
|
if (document.body.classList.contains('results_endpoint')) {
|
|
prepare_searx(hosts, port);
|
|
}
|
|
break;
|
|
}
|