Midora/extension/background.js

435 lines
13 KiB
JavaScript
Raw Normal View History

2018-09-16 12:27:42 +02:00
var browser = browser || chrome;
2019-01-14 23:11:06 +01:00
var PENDING_SUBMISSIONS = ':PENDING_SUBMISSIONS'
var MIGRATION = ':MIGRATION'
2019-01-18 19:56:54 +01:00
// If a user labels one of these URLs, they're making a mistake. Ignore the label.
// This list includes:
// * Social networks that are not supported
// * System pages of supported social networks
// * Archival and link shortening sites.
2019-04-18 16:53:57 +02:00
// * Reddit bots.
2019-01-14 23:11:06 +01:00
var badIdentifiersArray = [
'archive.is',
'archive.org',
'assets.tumblr.com',
2019-03-01 00:04:29 +01:00
'bing.com',
2019-01-14 23:11:06 +01:00
'bit.ly',
'blogspot.com',
2019-02-08 20:42:25 +01:00
'change.org',
2019-02-18 21:27:01 +01:00
'curiouscat.me',
2019-02-18 23:16:03 +01:00
'deviantart.com',
'discord-store.com',
'discordapp.com',
2019-04-08 16:27:41 +02:00
'disqus.com',
2019-03-01 00:04:29 +01:00
'duckduckgo.com',
2019-04-08 16:27:41 +02:00
'en.wikipedia.org',
2019-02-18 23:16:03 +01:00
'en.wikiquote.org',
2019-02-08 20:42:25 +01:00
'etsy.com',
2019-04-08 16:27:41 +02:00
'facebook.com',
2019-01-14 23:11:06 +01:00
'facebook.com/a',
'facebook.com/ad_campaign',
'facebook.com/ads',
'facebook.com/ajax',
'facebook.com/bookmarks',
'facebook.com/buddylist.php',
'facebook.com/bugnub',
'facebook.com/comment',
'facebook.com/composer',
2019-04-30 21:46:56 +02:00
'facebook.com/donate',
2019-01-14 23:11:06 +01:00
'facebook.com/events',
'facebook.com/findfriends',
'facebook.com/friends',
'facebook.com/fundraisers',
'facebook.com/games',
'facebook.com/groups',
2019-02-25 23:44:18 +01:00
'facebook.com/hashtag',
2019-01-14 23:11:06 +01:00
'facebook.com/help',
'facebook.com/home.php',
'facebook.com/intl',
'facebook.com/jobs',
'facebook.com/l.php',
'facebook.com/language.php',
'facebook.com/legal',
'facebook.com/like.php',
'facebook.com/local_surface',
'facebook.com/logout.php',
'facebook.com/mbasic',
2019-04-30 21:55:40 +02:00
'facebook.com/media',
2019-01-14 23:11:06 +01:00
'facebook.com/menu',
'facebook.com/messages',
'facebook.com/nfx',
'facebook.com/notes',
'facebook.com/notifications.php',
'facebook.com/notifications',
'facebook.com/nt',
'facebook.com/page',
'facebook.com/pages',
'facebook.com/people',
'facebook.com/permalink.php',
'facebook.com/pg',
2019-05-12 16:07:02 +02:00
'facebook.com/docs',
2019-01-14 23:11:06 +01:00
'facebook.com/photo.php',
'facebook.com/policies',
'facebook.com/privacy',
'facebook.com/profile.php',
'facebook.com/rapid_report',
'facebook.com/reactions',
'facebook.com/salegroups',
'facebook.com/search',
'facebook.com/settings',
2019-02-25 23:44:18 +01:00
'facebook.com/sharer.php',
2019-01-14 23:11:06 +01:00
'facebook.com/shares',
'facebook.com/story.php',
'facebook.com/ufi',
2019-01-14 23:14:36 +01:00
'google.com',
'googleusercontent.com',
2019-02-18 21:27:01 +01:00
'i.imgur.com',
'i.reddituploads.com',
2019-02-18 23:16:03 +01:00
'imdb.com',
2019-02-18 21:27:01 +01:00
'imgur.com',
2019-01-18 18:58:18 +01:00
'instagram.com',
2019-01-18 19:56:54 +01:00
'mail.google.com',
2019-01-14 23:11:06 +01:00
'media.tumblr.com',
'medium.com',
2019-01-18 18:58:18 +01:00
'patreon.com',
'paypal.com',
'paypal.me',
2019-02-18 21:27:01 +01:00
'play.google.com',
2019-01-18 19:56:54 +01:00
'plus.google.com',
2019-04-08 16:27:41 +02:00
'rationalwiki.org',
2019-01-14 23:11:06 +01:00
'reddit.com',
'reddit.com/r/all',
'reddit.com/r/popular',
'reddit.com/user/_youtubot_',
'reddit.com/user/animalfactsbot',
'reddit.com/user/anti-gif-bot',
'reddit.com/user/areyoudeaf',
2019-04-30 21:46:56 +02:00
'reddit.com/user/auto-xkcd37',
'reddit.com/user/automoderator',
'reddit.com/user/autotldr',
'reddit.com/user/biglebowskibot',
'reddit.com/user/bots_rise_up',
'reddit.com/user/cheer_up_bot',
'reddit.com/user/cheer-bot',
'reddit.com/user/clickablelinkbot',
'reddit.com/user/colorizethis',
'reddit.com/user/darnit_bot',
'reddit.com/user/darthplagueisbot',
'reddit.com/user/deepfrybot',
'reddit.com/user/dreamprocessor',
'reddit.com/user/drunkanimalfactbot',
'reddit.com/user/election_info_bot',
'reddit.com/user/eyebleachbot',
'reddit.com/user/factorial-bot',
'reddit.com/user/friendly-bot',
'reddit.com/user/garlicbot',
'reddit.com/user/gfycat_details_fixer',
'reddit.com/user/gifv-bot',
'reddit.com/user/good_good_gb_bb',
'reddit.com/user/goodbot_badbot',
'reddit.com/user/goodmod_badmod',
'reddit.com/user/gyazo_bot',
'reddit.com/user/haiku-detector',
2019-04-30 21:46:56 +02:00
'reddit.com/user/haikubot-1911',
'reddit.com/user/helperbot_',
'reddit.com/user/hug-bot',
'reddit.com/user/i_am_a_haiku_bot',
'reddit.com/user/ilinknsfwsubreddits',
'reddit.com/user/image_linker_bot',
'reddit.com/user/imdb_preview',
'reddit.com/user/imguralbumbot',
'reddit.com/user/jacksfilmsbot',
'reddit.com/user/jiffierbot',
'reddit.com/user/livetwitchclips',
'reddit.com/user/lyrics-matcher-bot',
'reddit.com/user/mailmygovnnbot',
'reddit.com/user/massdropbot',
'reddit.com/user/mentioned_videos',
'reddit.com/user/metric_units',
'reddit.com/user/mlbvideoconverterbot',
'reddit.com/user/morejpeg_auto',
'reddit.com/user/movieguide',
'reddit.com/user/multiusebot',
'reddit.com/user/news-summary',
'reddit.com/user/nflvideoconverterbot',
'reddit.com/user/octopusfunfacts',
'reddit.com/user/octupusfunfacts',
'reddit.com/user/opfeels',
'reddit.com/user/payrespects-bot',
'reddit.com/user/perrycohen',
'reddit.com/user/phonebatterylevelbot',
'reddit.com/user/picdescriptionbot',
'reddit.com/user/portmanteau-bot',
'reddit.com/user/quoteme-bot',
'reddit.com/user/redditsilverbot',
'reddit.com/user/redditstreamable',
'reddit.com/user/remindmebot',
'reddit.com/user/riskyclickerbot',
'reddit.com/user/rosey-the-bot',
'reddit.com/user/seriouslywhenishl3',
'reddit.com/user/shhbot',
'reddit.com/user/smallsubbot',
'reddit.com/user/snapshillbot',
'reddit.com/user/sneakpeekbot',
'reddit.com/user/stabbot_crop',
2019-04-30 21:46:56 +02:00
'reddit.com/user/stabbot',
'reddit.com/user/steamnewsbot',
'reddit.com/user/subjunctive__bot',
'reddit.com/user/table_it_bot',
'reddit.com/user/the-paranoid-android',
2019-04-30 21:46:56 +02:00
'reddit.com/user/thehelperdroid',
'reddit.com/user/thiscatmightcheeryou',
'reddit.com/user/timestamp_bot',
'reddit.com/user/timezone_bot',
'reddit.com/user/tiny_smile_bot',
'reddit.com/user/tipjarbot',
'reddit.com/user/tippr',
'reddit.com/user/totes_meta_bot',
'reddit.com/user/totesmessenger',
'reddit.com/user/tumblrdirect',
'reddit.com/user/tweetsincommentsbot',
'reddit.com/user/twitterlinkbot',
'reddit.com/user/twittertostreamable',
'reddit.com/user/video_descriptionbot',
'reddit.com/user/videodirectlinkbot',
'reddit.com/user/vredditmirrorbot',
'reddit.com/user/whodidthisbot',
'reddit.com/user/wikitextbot',
'reddit.com/user/xkcd_transcriber',
'reddit.com/user/youtubefactsbot',
2019-02-18 21:27:01 +01:00
'reddituploads.com',
2019-01-14 23:11:06 +01:00
'removeddit.com',
't.co',
't.umblr.com',
2019-02-18 23:16:03 +01:00
'tapatalk.com',
2019-04-30 21:46:56 +02:00
'tmblr.co',
2019-01-14 23:11:06 +01:00
'tumblr.com',
2019-02-18 21:27:01 +01:00
'twitch.tv',
2019-01-14 23:11:06 +01:00
'twitter.com',
'twitter.com/hashtag',
'twitter.com/i',
'twitter.com/search',
'twitter.com/settings',
2019-02-25 23:03:20 +01:00
'twitter.com/threader_app',
2019-01-14 23:11:06 +01:00
'twitter.com/threadreaderapp',
'twitter.com/who_to_follow',
2019-01-18 18:58:18 +01:00
'vk.com',
2019-04-08 16:27:41 +02:00
'wikipedia.org',
2019-01-14 23:11:06 +01:00
'wordpress.com',
'www.tumblr.com',
'youtu.be',
'youtube.com',
2019-03-03 21:26:59 +01:00
'youtube.com/playlist',
2019-01-14 23:11:06 +01:00
'youtube.com/redirect',
'youtube.com/watch',
];
var badIdentifiers = {};
badIdentifiersArray.forEach(x => badIdentifiers[x] = true);
2018-09-16 12:27:42 +02:00
var overrides = null;
var accepted = false;
var installationId = null;
2019-01-14 23:11:06 +01:00
2018-09-16 12:27:42 +02:00
browser.storage.local.get(['overrides', 'accepted', 'installationId'], v => {
accepted = v.accepted
overrides = v.overrides || {}
2019-01-14 23:11:06 +01:00
var migration = overrides[MIGRATION] || 0;
2019-04-30 21:46:56 +02:00
var CURRENT_VERSION = 11;
2019-01-14 23:11:06 +01:00
if(migration < CURRENT_VERSION){
for(var key of Object.getOwnPropertyNames(overrides)){
if(key.startsWith(':')) continue;
if(key.startsWith('facebook.com/a.')){
delete overrides[key];
continue;
}
2019-01-14 23:11:06 +01:00
if(key != key.toLowerCase()){
var v = overrides[key];
delete overrides[key];
overrides[key.toLowerCase()] = v;
}
}
badIdentifiersArray.forEach(x => delete overrides[x]);
overrides[MIGRATION] = CURRENT_VERSION;
browser.storage.local.set({ overrides: overrides });
}
2018-12-16 17:36:19 +01:00
if (!v.installationId) {
installationId = (Math.random() + '.' + Math.random() + '.' + Math.random()).replace(/\./g, '');
browser.storage.local.set({ installationId: installationId });
} else {
2018-10-27 17:00:38 +02:00
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);
2019-02-25 23:03:38 +01:00
var b = new BloomFilter(array, 20);
2018-09-16 12:27:42 +02:00
b.name = name;
bloomFilters.push(b);
});
});
}
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
2018-12-16 17:36:19 +01:00
if (message.acceptClicked !== undefined) {
2018-09-16 12:27:42 +02:00
accepted = message.acceptClicked;
2018-12-16 17:36:19 +01:00
browser.storage.local.set({ accepted: accepted });
2018-09-16 12:27:42 +02:00
browser.tabs.remove(sender.tab.id);
2018-12-16 17:36:19 +01:00
if (accepted && uncommittedResponse)
2018-09-16 12:27:42 +02:00
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;
2018-12-16 17:36:19 +01:00
for (var i = 0; i < id.length; i++) {
2018-09-16 12:27:42 +02:00
sum += id.charCodeAt(i);
}
2018-12-16 17:36:19 +01:00
if (sum % 8 != 0) continue;
2018-09-16 12:27:42 +02:00
}
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/*",
2018-11-16 21:51:31 +01:00
"*://*.twitter.com/*",
"*://medium.com/*",
2018-12-16 11:32:37 +01:00
"*://disqus.com/*",
2019-01-13 17:16:37 +01:00
"*://*.tumblr.com/*",
"*://*.wikipedia.org/*",
"*://*.rationalwiki.org/*",
"*://*.google.com/*",
2019-03-01 00:04:29 +01:00
"*://*.bing.com/*",
"*://duckduckgo.com/*",
2018-09-16 12:27:42 +02:00
]
});
}
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-12-16 17:36:19 +01:00
function submitPendingRatings() {
2018-10-27 17:02:15 +02:00
var submitted = overrides[PENDING_SUBMISSIONS].map(x => x);
2018-12-16 17:36:19 +01:00
var requestBody = {
installationId: installationId,
entries: submitted
2018-10-27 17:02:15 +02:00
}
console.log('Sending request');
2019-02-18 21:36:19 +01:00
fetch('https://k5kk18774h.execute-api.us-east-1.amazonaws.com/default/shinigamiEyesSubmission', {
2018-10-27 17:02:15 +02:00
body: JSON.stringify(requestBody),
method: 'POST',
2018-12-16 17:36:19 +01:00
credentials: 'omit',
2018-10-27 17:02:15 +02:00
}).then(response => {
response.text().then(result => {
console.log('Response: ' + result);
2018-12-16 17:36:19 +01:00
if (result == 'SUCCESS') {
2018-10-27 17:02:15 +02:00
overrides[PENDING_SUBMISSIONS] = overrides[PENDING_SUBMISSIONS].filter(x => submitted.indexOf(x) == -1);
2018-12-16 17:36:19 +01:00
browser.storage.local.set({ overrides: overrides });
2018-10-27 17:02:15 +02:00
}
})
2018-12-16 17:36:19 +01:00
2018-10-27 17:02:15 +02:00
});
}
2018-12-16 17:36:19 +01:00
function saveLabel(response) {
if (accepted) {
if (!overrides[PENDING_SUBMISSIONS]) {
2018-10-27 17:02:15 +02:00
overrides[PENDING_SUBMISSIONS] = Object.getOwnPropertyNames(overrides)
2018-12-16 17:36:19 +01:00
.map(x => { return { identifier: x, label: overrides[x] } });
2018-10-27 17:02:15 +02:00
}
2018-09-16 12:27:42 +02:00
overrides[response.identifier] = response.mark;
if (response.secondaryIdentifier)
overrides[response.secondaryIdentifier] = response.mark;
2018-12-16 17:36:19 +01:00
browser.storage.local.set({ overrides: overrides });
2018-10-27 17:02:15 +02:00
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();
}
2018-12-16 17:36:19 +01:00
function openHelp() {
2018-09-16 12:27:42 +02:00
browser.tabs.create({
url: browser.extension.getURL('help.html')
})
}
2018-09-16 12:27:42 +02:00
browser.contextMenus.onClicked.addListener(function (info, tab) {
2018-12-16 17:36:19 +01:00
if (info.menuItemId == 'help') {
2018-09-16 12:27:42 +02:00
openHelp();
return;
}
var label = info.menuItemId.substring('mark-'.length);
2018-12-16 17:36:19 +01:00
if (label == 'none') label = '';
2018-09-16 12:27:42 +02:00
browser.tabs.sendMessage(tab.id, {
mark: label,
url: info.linkUrl,
// elementId: info.targetElementId,
debug: overrides.debug
2018-09-16 12:27:42 +02:00
}, null, response => {
if (!response.identifier) return;
if (response.mark){
if (badIdentifiers[response.identifier]) return;
if (response.secondaryIdentifier && badIdentifiers[response.secondaryIdentifier])
response.secondaryIdentifier = null;
}
if (response.debug && /^facebook\.com\/[a-zA-Z]/.test(response.identifier))
alert('Note: could not find numeric id for ' + response.identifier);
2018-09-16 12:27:42 +02:00
response.tabId = tab.id;
saveLabel(response);
})
});