-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
89 lines (77 loc) · 2.92 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
// // Function to execute a script on a specific tab
// function executeScriptOnTab(tabId, script) {
// chrome.tabs.executeScript(tabId, { code: script });
// }
// // Function to update the tabs and execute a script on update
// function updateTabsAndExecuteScript() {
// // Get all tabs
// chrome.tabs.query({}, function (tabs) {
// // Loop through each tab
// tabs.forEach(function (tab) {
// // Execute the script on the tab
// executeScriptOnTab(tab.id, 'console.log("Script executed on tab: " + tab.url);');
// });
// });
// }
// try {
// // Listen for tab update event
// chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
// // Check if the tab has finished loading
// if (changeInfo.status === 'complete') {
// // Execute the script on the updated tab
// chrome.scripting.executeScript({
// target: { tabId: tab.id },
// files: ['scripts/contentScript.js', 'css/contentScript.css']
// });
// chrome.scripting.insertCSS({
// files: ["css/contentScript.css"],
// target: { tabId: tab.id },
// });
// // executeScriptOnTab(tabId, 'console.log("Script executed on updated tab: " + tab.url);');
// }
// });
// } catch (e) {
// console.log(e);
// }
alert("hi");
console.log('Background script is running');
chrome.runtime.onInstalled.addListener(() => {
console.log('Extension installed or updated');
});
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
let url = tabs[0].url;
// use `url` here inside the callback because it's asynchronous!
console.log(url);
});
});
// Listener for tab activation
// chrome.tabs.onActivated.addListener(activeInfo => {
// chrome.tabs.get(activeInfo.tabId, function(tab) {
// if (tab.url) {
// console.log('Visited URL:', tab.url);
// }
// });
// startTime = Date.now(); // Start timing for the new active tab
// });
console.log("going to activate?");
chrome.tabs.onActivated.addListener(activeInfo => {
// console.log('Tab activated:', activeInfo.tabId);
// chrome.tabs.get(activeInfo.tabId, function(tab) {
// if (tab.url) {
// console.log('Visited URL:', tab.url);
// }
// });
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
console.log(tabs);
let url = tabs[0].url;
// use `url` here inside the callback because it's asynchronous!
console.log(url);
});
});
chrome.storage.onChanged.addListener(function(changes, namespace) {
for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
console.log(`Storage key "${key}" in namespace "${namespace}" changed.`);
console.log(`Old value was "${oldValue}", new value is "${newValue}".`);
}
});