This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbackground.js
115 lines (101 loc) · 3.78 KB
/
background.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
108
109
110
111
112
113
114
115
/*
This Chrome extension has two components running on separate threads:
1) background.js runs in the extension background. background.js does not have access to the page's content but it has access to the chrome api.
2) content_script.js has access to the content of the page, which is necessary to hide the relevant tweets, but not to the chrome api.
background.js and content_script.js communicate with each other via a message api linked to the browser *tab*, and the specific tab must be specified by number (here denoted 'tabId').
From the chrome api, the extension needs to know:
1) when there is a navigation ('webNavigation') so that it can hide the relevant tweets.
2) since Twitter loads information after webNavigation fires, we also need to fire an 'alarm' periodically.
3) Those two require user permissions, specificed in the manifest.json
4) 'pageAction' and 'tabs' are chrome apis also used in this extension.
*/
const ALARM_PREFIX = "twitterLikeHiderAlarm";
let totalRemoved = 0;
let options;
const debugLog = (...data) => {
const isdebug = options && options.debug === true;
if (isdebug) console.info.apply(null, data);
};
/** onResponse receives and handles messages from content_script.js
**/
const onResponse = (tabId) => (response) => {
debugLog("TwitterLikesHider:background:onResponse", tabId, response);
if (response)
switch (response.type) {
case "isOff":
totalRemoved = 0;
chrome.pageAction.setTitle({
title: `Showing all likes`,
tabId: tabId,
});
chrome.pageAction.setIcon({
path: {
16: "icon-off-16.png",
24: "icon-off-24.png",
32: "icon-off-32.png",
},
tabId: tabId,
});
chrome.alarms.clearAll();
break;
case "isOn":
totalRemoved = response.value;
chrome.pageAction.setTitle({
title: `Hiding ${totalRemoved} ${
totalRemoved === 1 ? "tweet" : "tweets"
}`,
tabId: tabId,
});
chrome.pageAction.setIcon({
path: {
16: "icon-16.png",
24: "icon-24.png",
32: "icon-32.png",
},
tabId: tabId,
});
createOrContinueAlarm(tabId);
break;
case "optionChange":
// pass the options to the content page
options = response;
chrome.tabs.sendMessage(tabId, response);
break;
default:
console.error(`TwitterLikesHider: unknown response`, response);
break;
}
};
const createOrContinueAlarm = (tabId) => {
const alarmName = `${ALARM_PREFIX}-${tabId}`;
const onAlarmDetails = (alarm) => {
if (alarm === undefined)
// If the user does not scroll, give Twitter 7.5 seconds to load
// and then trigger a removal of tweets:
chrome.alarms.create(alarmName, { when: 7500, periodInMinutes: 1 });
};
chrome.alarms.get(alarmName, onAlarmDetails);
};
/** The user clicking the entension icon turns the extension on
* and off */
const onIconClicked = (e) => {
const tabId = e.id;
chrome.tabs.sendMessage(tabId, { type: "toggle" }, onResponse(tabId));
};
const onAlarm = (alarm) => {
const alarmName = alarm.name;
if (alarmName.indexOf("-") < 0) return;
const tabId = parseInt(alarmName.split("-")[1]);
chrome.tabs.sendMessage(tabId, { type: "alarm" }, onResponse(tabId));
};
const onNavToTwitter = (e) => {
const tabId = e.tabId;
chrome.pageAction.show(tabId);
chrome.tabs.sendMessage(tabId, { type: "webNavigation" }, onResponse(tabId));
chrome.runtime.onMessage.addListener(onResponse(tabId));
};
chrome.webNavigation.onCompleted.addListener(onNavToTwitter, {
url: [{ hostContains: "twitter.com" }],
});
chrome.pageAction.onClicked.addListener(onIconClicked);
chrome.alarms.onAlarm.addListener(onAlarm);