forked from JeffreyATW/mbfc_icon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventPage.js
107 lines (100 loc) · 2.67 KB
/
eventPage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var getFile = function (type) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
var obj = {}
obj[type] = JSON.parse(xhr.responseText)
browser.storage.local.set(obj);
}
};
xhr.open('GET', 'https://jeffreyatw.com/static/mbfc/' + type + '.json', true);
xhr.send();
}
var update = function () {
browser.storage.local.get(['biases', 'lastUpdated', 'sources'], function (items) {
if (
items.lastUpdated === undefined ||
Date.now() - items.lastUpdated > 86400000 ||
items.biases === undefined ||
items.sources === undefined
) {
getFile('sources');
getFile('biases');
browser.storage.local.set({
lastUpdated: Date.now()
});
}
});
}
browser.storage.local.get(['biases', 'sources'], function (items) {
if (items.sources === undefined || items.biases === undefined) {
update();
}
});
browser.alarms.create('updater', {
periodInMinutes: 1
});
browser.alarms.onAlarm.addListener(function (alarm) {
if (alarm.name === 'updater') {
update();
}
});
var tabListener = function (tab) {
if (tab.url !== undefined) {
getTabSource(tab.url, function (source, bias) {
var path = '/icon.png';
if (source === undefined || bias === undefined) {
browser.pageAction.hide(tab.id);
} else {
path = '/icons/';
switch (source.bias) {
case 'left':
path += 'left';
break;
case 'leftcenter':
case 'left-center':
path += 'left-center';
break;
case 'center':
path += 'center';
break;
case 'rightcenter':
case 'right-center':
path += 'right-center';
break;
case 'right':
path += 'right';
break;
case 'pro-science':
path += 'pro-science';
break;
case 'conspiracy':
path += 'conspiracy-pseudoscience';
break;
case 'satire':
path += 'satire';
break;
case 'fake-news':
path += 'fake-news';
break;
}
path += '.png';
browser.pageAction.show(tab.id);
browser.pageAction.setTitle({
title: bias.name,
tabId: tab.id
});
}
browser.pageAction.setIcon({
path: path,
tabId: tab.id
});
});
}
}
browser.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
tabListener(tab);
});
browser.tabs.onActivated.addListener(function (ids) {
browser.tabs.get(ids.tabId, tabListener);
});