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) {
|
browser.runtime.onConnect.addListener(function(port) {
|
||||||
port.onMessage.addListener(async function(data) {
|
port.onMessage.addListener(async function(data) {
|
||||||
console.log(`testing ${data.hostname}`);
|
switch (data.action) {
|
||||||
let result = await browser.dns.resolve(data.hostname, ['disable_ipv6']);
|
case 'test':
|
||||||
let cloudfucked = result.addresses.some(ip => is_cloudfucked(ip));
|
console.log(`testing ${data.hostname}`);
|
||||||
port.postMessage({
|
let result = await browser.dns.resolve(data.hostname, ['disable_ipv6']);
|
||||||
"hostname": data.hostname,
|
let cloudfucked = result.addresses.some(ip => is_cloudfucked(ip));
|
||||||
"cloudfucked": cloudfucked
|
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 = {
|
const gatekeepers = {
|
||||||
// "here-goes-hostname-of-your-local-searx-instance": "searx",
|
// "here-goes-hostname-of-your-local-searx-instance": "searx",
|
||||||
"html.duckduckgo.com": "duckduckgo"
|
"html.duckduckgo.com": "duckduckgo"
|
||||||
};
|
};
|
||||||
function prepare_searx(hosts, port) {
|
const modules = {
|
||||||
port.onMessage.addListener(function(data) {
|
"searx": {
|
||||||
hosts[data.hostname].cloudfucked = data.cloudfucked;
|
"check": function() {
|
||||||
hosts[data.hostname].links.forEach(link => {
|
return document.body.classList.contains('results_endpoint');
|
||||||
|
},
|
||||||
|
"trigger": function(link, cloudfucked) {
|
||||||
if (!link.querySelector('.cloudfuck_market')) {
|
if (!link.querySelector('.cloudfuck_market')) {
|
||||||
let market = document.createElement('img');
|
let market = document.createElement('img');
|
||||||
market.classList.add('cloudfuck_market');
|
market.classList.add('cloudfuck_market');
|
||||||
market.setAttribute('data-test', data.cloudfucked);
|
market.setAttribute('data-test', cloudfucked);
|
||||||
if (data.cloudfucked) {
|
if (cloudfucked) {
|
||||||
market.alt = 'cloudflare';
|
market.alt = 'cloudflare';
|
||||||
market.src = browser.runtime.getURL('images/pure-evil.png');
|
market.src = resources.cf_red;
|
||||||
market.style.height = '1em';
|
market.style.height = '1em';
|
||||||
market.style.marginRight = '0.5em';
|
market.style.marginRight = '0.5em';
|
||||||
}
|
}
|
||||||
link.prepend(market);
|
link.prepend(market);
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
});
|
"init": function(hosts, port) {
|
||||||
function getPages() {
|
function getPages() {
|
||||||
return document.querySelectorAll('#main_results hr').length + 1;
|
return document.querySelectorAll('#main_results hr').length + 1;
|
||||||
}
|
}
|
||||||
let pages = getPages();
|
let pages = getPages();
|
||||||
parse_searx(hosts, port);
|
this.parse(hosts, port);
|
||||||
addEventListener('scroll', function() {
|
addEventListener('scroll', () => {
|
||||||
if (getPages() > pages) {
|
if (getPages() > pages) {
|
||||||
parse_searx(hosts, port);
|
modules.searx.parse(hosts, port);
|
||||||
pages = getPages();
|
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
|
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
"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);
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
"duckduckgo": {
|
||||||
// TODO: optimize code for search engines modules
|
"check": function() {
|
||||||
function prepare_duckduckgo(hosts, port) {
|
return document.body.classList.contains('body--html');
|
||||||
port.onMessage.addListener(function(data) {
|
},
|
||||||
hosts[data.hostname].cloudfucked = data.cloudfucked;
|
"trigger": function(link, cloudfucked) {
|
||||||
hosts[data.hostname].links.forEach(link => {
|
|
||||||
if (!link.querySelector('.cloudfuck_market')) {
|
if (!link.querySelector('.cloudfuck_market')) {
|
||||||
let market = document.createElement('img');
|
let market = document.createElement('img');
|
||||||
market.classList.add('cloudfuck_market');
|
market.classList.add('cloudfuck_market');
|
||||||
market.setAttribute('data-test', data.cloudfucked);
|
market.setAttribute('data-test', cloudfucked);
|
||||||
if (data.cloudfucked) {
|
if (cloudfucked) {
|
||||||
market.alt = 'cloudflare';
|
market.alt = 'cloudflare';
|
||||||
market.src = browser.runtime.getURL('images/pure-evil.png');
|
market.src = resources.cf_red;
|
||||||
market.style.height = '1em';
|
market.style.height = '1em';
|
||||||
market.style.marginRight = '0.5em';
|
market.style.marginRight = '0.5em';
|
||||||
}
|
}
|
||||||
link.prepend(market);
|
link.prepend(market);
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
});
|
"init": function(hosts, port) {
|
||||||
parse_duckduckgo(hosts, port);
|
this.parse(hosts, port);
|
||||||
}
|
},
|
||||||
function parse_duckduckgo(hosts, port) {
|
"parse": function(hosts, port) {
|
||||||
Array.from(document.querySelectorAll('.result__a'))
|
Array.from(document.querySelectorAll('.result__a'))
|
||||||
.forEach(function(link) {
|
.forEach((link) => {
|
||||||
let hostname = new URL(link.href).hostname;
|
let hostname = new URL(link.href).hostname;
|
||||||
if (!(hostname in hosts)) {
|
if (!(hostname in hosts)) {
|
||||||
hosts[hostname] = {
|
hosts[hostname] = {
|
||||||
"links": [link],
|
"links": [link],
|
||||||
"cloudfucked": null
|
"cloudfucked": null
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
hosts[hostname].links.push(link);
|
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) {
|
for (hostname in hosts) {
|
||||||
let host = hosts[hostname];
|
let host = hosts[hostname];
|
||||||
if (host.cloudfucked === null) {
|
if (host.cloudfucked === null) {
|
||||||
port.postMessage({
|
port.postMessage({
|
||||||
"hostname": hostname
|
"action": "test",
|
||||||
|
hostname
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let hosts = [];
|
let engine = gatekeepers[location.hostname];
|
||||||
let port = browser.runtime.connect();
|
if (typeof engine !== 'undefined') {
|
||||||
switch (gatekeepers[location.hostname]) {
|
loadModule(engine);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue