-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathbackground.js
More file actions
74 lines (70 loc) · 3.05 KB
/
background.js
File metadata and controls
74 lines (70 loc) · 3.05 KB
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
//======================================================================
// Event for extension icon - toggle
//======================================================================
chrome.action.onClicked.addListener((tab) => {
// check origin if its url is matching our rule
let checkOrigin = tab.url.startsWith('https://colab.research.google.com/');
if (checkOrigin) {
console.log('send toggle', checkOrigin, tab.id, tab);
// send toggle action to content
chrome.tabs.sendMessage(tab.id, { type: 'toggle' }).then(function(result) {
// success
console.log('ok', result);
}).catch(function(result) {
// error: if no content script, execute script again
console.log('error', result);
// execute script manually
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
}).then(function(result) {
// toggle again
chrome.tabs.sendMessage(tab.id, { type: 'toggle' });
});
});
} else {
console.log("it's not colab site...");
}
});
//======================================================================
// Event for check tab to disable or enable extension
//======================================================================
// check status on tab activated and check colab exist
chrome.tabs.onActivated.addListener(function() {
chrome.tabs.query({ active: true, lastFocusedWindow: true }, function(tabs) {
if (tabs && tabs.length > 0) {
let tabUrl = tabs[0].url;
let isColabExist = tabUrl.startsWith('https://colab.research.google.com/');
if (isColabExist == true) {
// reset
// chrome.action.setBadgeText({ text: '' });
chrome.action.setPopup({ popup: '' });
} else {
// set badge and popup
// chrome.action.setBadgeBackgroundColor({ color: 'red' });
// chrome.action.setBadgeText({ text: ':(' });
chrome.action.setPopup({ popup: 'popup.html' });
}
}
});
});
// check status on tab updated
chrome.tabs.onUpdated.addListener(function() {
chrome.tabs.query({ active: true, lastFocusedWindow: true }, function(tabs) {
if (tabs && tabs.length > 0) {
let tabUrl = tabs[0].url;
let isColabExist = tabUrl.startsWith('https://colab.research.google.com/');
if (isColabExist == true) {
// reset
// chrome.action.setBadgeText({ text: '' });
chrome.action.setPopup({ popup: '' });
} else {
// set badge and popup
// chrome.action.setBadgeBackgroundColor({ color: 'red' });
// chrome.action.setBadgeText({ text: ':(' });
chrome.action.setPopup({ popup: 'popup.html' });
}
}
});
});
// End of file