Midora/extension/background.js

164 lines
4.7 KiB
JavaScript
Raw Normal View History

2018-09-16 12:27:42 +02:00
var browser = browser || chrome;
var overrides = null;
var accepted = false;
var installationId = null;
browser.storage.local.get(['overrides', 'accepted', 'installationId'], v => {
accepted = v.accepted
overrides = v.overrides || {}
if(!v.installationId){
installationId = (Math.random()+ '.' +Math.random() + '.' +Math.random()).replace(/\./g, '');
browser.storage.local.set({installationId: installationId});
2018-10-27 17:00:38 +02:00
}else{
installationId = v.installationId;
2018-09-16 12:27:42 +02:00
}
})
var bloomFilters = [];
function loadBloomFilter(name) {
var url = browser.extension.getURL('data/' + name + '.dat');
fetch(url).then(response => {
response.arrayBuffer().then(arrayBuffer => {
var array = new Uint32Array(arrayBuffer);
var b = new BloomFilter(array, 17);
b.name = name;
bloomFilters.push(b);
});
});
}
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if(message.acceptClicked !== undefined) {
accepted = message.acceptClicked;
browser.storage.local.set({accepted: accepted});
browser.tabs.remove(sender.tab.id);
if(accepted && uncommittedResponse)
saveLabel(uncommittedResponse)
uncommittedResponse = null;
return;
}
var response = {};
var transphobic = message.myself && bloomFilters.filter(x => x.name == 'transphobic')[0].test(message.myself);
for (var id of message.ids) {
2018-10-27 11:42:49 +02:00
if (overrides[id] !== undefined) {
response[id] = overrides[id];
continue;
}
if (transphobic) {
if (id == message.myself) continue;
2018-09-16 12:27:42 +02:00
var sum = 0;
for(var i = 0; i < id.length; i++){
sum += id.charCodeAt(i);
}
if(sum % 8 != 0) continue;
}
2018-10-27 11:42:49 +02:00
for (var bloomFilter of bloomFilters) {
if (bloomFilter.test(id)) response[id] = bloomFilter.name;
2018-09-16 12:27:42 +02:00
}
}
sendResponse(response);
});
loadBloomFilter('transphobic');
loadBloomFilter('t-friendly');
function createContextMenu(text, id) {
browser.contextMenus.create({
id: id,
title: text,
contexts: ["link"],
targetUrlPatterns: [
"*://*.facebook.com/*",
"*://*.youtube.com/*",
"*://*.reddit.com/*",
"*://*.twitter.com/*"
]
});
}
createContextMenu('Mark as anti-trans', 'mark-transphobic');
createContextMenu('Mark as t-friendly', 'mark-t-friendly');
createContextMenu('Clear', 'mark-none');
createContextMenu('Help', 'help');
var uncommittedResponse = null;
2018-10-27 17:02:15 +02:00
function submitPendingRatings(){
var submitted = overrides[PENDING_SUBMISSIONS].map(x => x);
var requestBody ={
installationId: installationId,
entries: submitted
}
console.log('Sending request');
fetch('https://shinigami-eyes.azurewebsites.net/api/submit', {
body: JSON.stringify(requestBody),
method: 'POST',
credentials: 'omit',
}).then(response => {
response.text().then(result => {
console.log('Response: ' + result);
if(result == 'SUCCESS'){
overrides[PENDING_SUBMISSIONS] = overrides[PENDING_SUBMISSIONS].filter(x => submitted.indexOf(x) == -1);
browser.storage.local.set({overrides: overrides});
}
})
});
}
var PENDING_SUBMISSIONS = ':PENDING_SUBMISSIONS'
2018-09-16 12:27:42 +02:00
function saveLabel(response){
if(accepted){
2018-10-27 17:02:15 +02:00
if(!overrides[PENDING_SUBMISSIONS]){
overrides[PENDING_SUBMISSIONS] = Object.getOwnPropertyNames(overrides)
.map(x => { return { identifier: x, label: overrides[x] }});
}
2018-09-16 12:27:42 +02:00
overrides[response.identifier] = response.mark;
2018-10-27 17:02:15 +02:00
browser.storage.local.set({overrides: overrides});
overrides[PENDING_SUBMISSIONS].push(response);
submitPendingRatings();
2018-09-16 12:27:42 +02:00
//console.log(response);
browser.tabs.sendMessage(response.tabId, { updateAllLabels: true });
//browser.tabs.executeScript(response.tabId, {code: 'updateAllLabels()'});
return;
}
uncommittedResponse = response;
openHelp();
}
function openHelp(){
browser.tabs.create({
url: browser.extension.getURL('help.html')
})
}
browser.contextMenus.onClicked.addListener(function (info, tab) {
if(info.menuItemId == 'help'){
openHelp();
return;
}
var label = info.menuItemId.substring('mark-'.length);
if(label == 'none') label = '';
browser.tabs.sendMessage(tab.id, {
mark: label,
url: info.linkUrl,
elementId: info.targetElementId
}, null, response => {
if (!response.identifier) return;
response.tabId = tab.id;
saveLabel(response);
})
});