-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
91 lines (86 loc) · 2.77 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
// chrome.action.onClicked.addListener(() => {
// //onclicking the extension icon event is triggered
// chrome.tabs.create({
// url: "https://sanketkarki.com.np",
// active: true,
// });
// });
// chrome.action.onClicked.addListener((tab) => {
// //execute this function to listen to the toolbar of the extension being clicked
// chrome.scripting.executeScript({
// //execute this script on this tab
// target: { tabId: tab.id }, //on the tab that is clicked
// files: ["content-script.js"],
// });
// });
// chrome.runtime.onInstalled.addListener(() => {
// console.log("Service worker is installed and running.");
// });
// chrome.action.onClicked.addListener((tab) => {
// console.log("Extension icon clicked.");
// chrome.scripting.executeScript({
// target: { tabId: tab.id },
// files: ["content-script.js"],
// });
// });
chrome.runtime.onInstalled.addListener(({ reason }) => {
if (reason === "install") {
chrome.tabs.create({
url: "onboarding.html",
});
chrome.storage.local.set({ show: "quote" });
}
});
let urlData;
chrome.runtime.onStartup.addListener(() => {
// Perform any necessary operations on startup(restart of chrome)
chrome.storage.local.get({ url: [] }).then((data) => {
urlData = data.url;
});
});
chrome.storage.onChanged.addListener((changes, areaName) => {
chrome.storage.local.get({ url: [] }).then((data) => {
urlData = data.url;
console.log("urlData", urlData);
});
});
//retrieve the stored urls from local storage
// async function getCurrentTab() {
// let queryOptions = { active: true, lastFocusedWindow: true };
// // `tab` will either be a `tabs.Tab` instance or `undefined`.
// let [tab] = await chrome.tabs.query(queryOptions);
// return tab;
// }
// chrome.tabs.query({ active: true, lastFocusedWindow: true }, (tabs) => {
// let currentUrl = tabs[0]?.url;
// if (currentUrl.match(urlData)) {
// console.log("Matched");
// chrome.scripting.executeScript({
// //execute this script on this tab
// target: { tabId: currentUrl.id }, //on the tab that is clicked
// files: ["content-script.js"],
// });
// }
// });
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
console.log("tabId and tab", tabId, tab);
console.log("tab.url", tab.url);
if (!urlData) {
return;
}
console.log("urlData", urlData);
urlData.forEach((url) => {
if (tab.url.match(url)) {
console.log("Matched");
chrome.scripting.executeScript({
//execute this script on this tab
target: { tabId: tabId }, //on the tab that is clicked
files: ["content-script.js"],
});
}
});
// // read changeInfo data and do something with it (like read the url)
// if (changeInfo.url) {
// // do something here
// }
});