-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
57 lines (50 loc) · 1.59 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
/**
* Background script
*
* It has both the listeners, one will request content scripts and
* another will respond to content scripts
*
* @author: Madhankumar<[email protected]>
*/
(function() {
'use strict';
/**
* Listener for content scripts
*
*/
chrome.runtime.onMessage.addListener(function(request, sender, senderResponse) {
let chromeExtensionMessageHandler = {
/**
* returns all the opened tabs in the chrome
* by excluding the currently opened tab
*/
getAllTabs: function() {
chrome.tabs.query({}, tabs => {
senderResponse(tabs);
});
return true;
},
/**
* switch to the selected tab at the particular window
* @param {tab} tab
*/
switchTab: function (tab) {
chrome.tabs.update(parseInt(tab.tabId), {active: true}, function() {
chrome.windows.update(parseInt(tab.windowId), {focused: true});
});
return true;
},
}
return chromeExtensionMessageHandler[request.type](request.selectedTab);
});
/**
* Listener for background scripts
*/
chrome.commands.onCommand.addListener(function(command) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, command, function(response) {
console.info(response);
});
})
});
})();