-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
126 lines (111 loc) · 4.36 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
116
117
118
119
120
121
122
123
124
125
126
// call runtime.onInstalled
chrome.runtime.onInstalled.addListener(() => {
console.log("this always runs once when the extension is installed.");
});
/*
Need to update code when chrome88 is release on main chrome channel.
Manifest v2 && chromeVerson < 88 => chrome.browserAction
(Manifest V2 | Manifest v3) && chromeVerson > 88 => chrome.action
*/
chrome.browserAction.onClicked.addListener((tab) => {
console.log("icon was clicked");
globalThis.popupEnabled = false;
enableExtension(tab, tab.id);
});
const checkLocationChange = () => {
// could make a promise later if necessary
console.log("sending check location message");
if (globalThis.popupEnabled)
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const port = chrome.tabs.sendMessage(tabs[0].id, { type: "checkLocation" }, (reload) => {
if (reload) {
// if true, io client should connect to the new room and disconnect from the old room
// reload the extension to be able to inject the content script again.
// console.log(`disable exentension on tab ${tabs[0].id}`);
// chrome.browserAction.disable(tabs[0].id);
globalThis.popupEnabled = false;
}
});
console.log("port:", port);
});
globalThis.locationTested = true;
setTimeout(() => (globalThis.locationTested = false), 8000); // check for the path change once every 8 seconds (bad to use global variables for this, but ohwell)
};
function toggleChat(tabId) {
console.log("adding chat.");
chrome.tabs.executeScript(
tabId,
{
file: "contentScript.js",
},
() => {
if (chrome.runtime.lastError) return console.log("error:", chrome.runtime.lastError);
console.log("content-script injected");
}
);
}
function isLiveEventPage(urlString) {
const { hostname, pathname } = new URL(urlString);
if (pathname.includes("ArticleId")) return false;
const home = pathname.match(/\/home\//);
if (!home) return false;
const eventId = pathname.match(/^.*\/([a-z\-0-9]+)\??.*/)[1];
console.log("eventId length:", eventId.length);
return eventId && eventId.length > 20;
}
/* enable/disable popup / extension
browserAction => browserAction.enable && browserAction.disable
action => action.enable() && action.disable
*/
// check the page and then enable popup
function enableExtension(tab, tabId) {
//if live event enable dazn chat toggle
if (!isLiveEventPage(tab.url)) {
console.log(`not a live event.
popup => watch.html`);
return chrome.browserAction.setPopup({ popup: "popup/wrongPage.html", tabId });
}
console.log(`tab ${tabId} - is Live event`);
chrome.browserAction.setPopup({ popup: "popup/index.html", tabId });
console.log(`popup => popup.html`);
toggleChat(tabId);
// chrome.storage.sync.set({ chatEnabled: true }, () => console.log("icon enabled"));
globalThis.popupEnabled = true;
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
console.log(globalThis.locationTested);
if (!/.dazn./.test(tab.url)) return chrome.browserAction.disable(tabId);
else chrome.browserAction.enable(tabId, () => console.log("extension enabled on tab:", tabId));
if (!globalThis.locationTested) checkLocationChange();
console.log(`Tab ${tabId} ${tab.status}`);
if (tab.status !== "complete") return;
if (isLiveEventPage) {
chrome.browserAction.setBadgeText({ tabId, text: "live" });
chrome.browserAction.setBadgeBackgroundColor({ tabId, color: "green" });
} else {
chrome.browserAction.setBadgeText({ tabId, text: "" });
chrome.browserAction.setBadgeBackgroundColor({ tabId, color: "" });
}
// if (globalThis.popupEnabled)
// return console.log("daznChat already Enabled", globalThis.popupEnabled);
// enablePopup(tab, tabId);
});
// Message listener
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("sender:", sender);
console.log("request", request);
// console.log("sender.id matches location.host:", sender?.id === location?.host);
const { type, event } = request;
// check types
if (type === "eventDetails") {
const eventId = sender.tab.url.match(/^.*\/([a-z\-0-9]+)\??.*/)[1];
const title = sender.tab.title;
console.log(title);
sendResponse({ eventId, title });
}
if (type === "unloaded") {
console.log("window was unloaded");
console.log("event", event);
}
// sendResponse("good Evening");
});