Introducing search engine modules
Additional changes: Improved IPC by multiple actions - the initial one is "test" now
This commit is contained in:
parent
dd95ae2fe7
commit
a33462fd66
|
@ -53,12 +53,25 @@ function is_cloudfucked(address) {
|
|||
}
|
||||
browser.runtime.onConnect.addListener(function(port) {
|
||||
port.onMessage.addListener(async function(data) {
|
||||
console.log(`testing ${data.hostname}`);
|
||||
let result = await browser.dns.resolve(data.hostname, ['disable_ipv6']);
|
||||
let cloudfucked = result.addresses.some(ip => is_cloudfucked(ip));
|
||||
port.postMessage({
|
||||
"hostname": data.hostname,
|
||||
"cloudfucked": cloudfucked
|
||||
});
|
||||
switch (data.action) {
|
||||
case 'test':
|
||||
console.log(`testing ${data.hostname}`);
|
||||
let result = await browser.dns.resolve(data.hostname, ['disable_ipv6']);
|
||||
let cloudfucked = result.addresses.some(ip => is_cloudfucked(ip));
|
||||
port.postMessage({
|
||||
"hostname": data.hostname,
|
||||
"cloudfucked": cloudfucked
|
||||
});
|
||||
break;
|
||||
case 'prefs-update':
|
||||
console.log('attempting to update preferences', data.prefs);
|
||||
break;
|
||||
case 'prefs-fetch':
|
||||
console.log('attempting to fetch preferences');
|
||||
break;
|
||||
default:
|
||||
console.log(`unknown action ${data.action}`);
|
||||
break;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,115 +1,128 @@
|
|||
const resources = {
|
||||
"cf_red": browser.runtime.getURL('images/pure-evil.png')
|
||||
};
|
||||
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 => {
|
||||
const modules = {
|
||||
"searx": {
|
||||
"check": function() {
|
||||
return document.body.classList.contains('results_endpoint');
|
||||
},
|
||||
"trigger": function(link, cloudfucked) {
|
||||
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.setAttribute('data-test', cloudfucked);
|
||||
if (cloudfucked) {
|
||||
market.alt = 'cloudflare';
|
||||
market.src = browser.runtime.getURL('images/pure-evil.png');
|
||||
market.src = resources.cf_red;
|
||||
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
|
||||
},
|
||||
"init": function(hosts, port) {
|
||||
function getPages() {
|
||||
return document.querySelectorAll('#main_results hr').length + 1;
|
||||
}
|
||||
let pages = getPages();
|
||||
this.parse(hosts, port);
|
||||
addEventListener('scroll', () => {
|
||||
if (getPages() > pages) {
|
||||
modules.searx.parse(hosts, port);
|
||||
pages = getPages();
|
||||
}
|
||||
});
|
||||
},
|
||||
"parse": function(hosts, port) {
|
||||
Array.from(document.querySelectorAll('.result-default'))
|
||||
.forEach((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);
|
||||
}
|
||||
});
|
||||
makeTest(hosts, port);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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 => {
|
||||
},
|
||||
"duckduckgo": {
|
||||
"check": function() {
|
||||
return document.body.classList.contains('body--html');
|
||||
},
|
||||
"trigger": function(link, cloudfucked) {
|
||||
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.setAttribute('data-test', cloudfucked);
|
||||
if (cloudfucked) {
|
||||
market.alt = 'cloudflare';
|
||||
market.src = browser.runtime.getURL('images/pure-evil.png');
|
||||
market.src = resources.cf_red;
|
||||
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);
|
||||
},
|
||||
"init": function(hosts, port) {
|
||||
this.parse(hosts, port);
|
||||
},
|
||||
"parse": function(hosts, port) {
|
||||
Array.from(document.querySelectorAll('.result__a'))
|
||||
.forEach((link) => {
|
||||
let hostname = new URL(link.href).hostname;
|
||||
if (!(hostname in hosts)) {
|
||||
hosts[hostname] = {
|
||||
"links": [link],
|
||||
"cloudfucked": null
|
||||
};
|
||||
} else {
|
||||
hosts[hostname].links.push(link);
|
||||
}
|
||||
});
|
||||
makeTest(hosts, port);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
function loadModule(name) {
|
||||
if (!(name in modules)) {
|
||||
throw new Error(`unknown module ${name}`);
|
||||
}
|
||||
const module = modules[name];
|
||||
if (module.check()) {
|
||||
const port = browser.runtime.connect();
|
||||
let hosts = [];
|
||||
port.onMessage.addListener((data) => {
|
||||
hosts[data.hostname].cloudfucked = data.cloudfucked;
|
||||
hosts[data.hostname].links.forEach((link) => {
|
||||
module.trigger(link, data.cloudfucked);
|
||||
});
|
||||
});
|
||||
// TODO: fix this crap
|
||||
module.init(hosts, port);
|
||||
}
|
||||
}
|
||||
function makeTest(hosts, port) {
|
||||
for (hostname in hosts) {
|
||||
let host = hosts[hostname];
|
||||
if (host.cloudfucked === null) {
|
||||
port.postMessage({
|
||||
"hostname": hostname
|
||||
"action": "test",
|
||||
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;
|
||||
let engine = gatekeepers[location.hostname];
|
||||
if (typeof engine !== 'undefined') {
|
||||
loadModule(engine);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue