-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
144 lines (128 loc) · 4.46 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
myURL=undefined;
chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){ //onUpdated should fire when the selected tab is changed or a link is clicked
chrome.tabs.getSelected(null,function(tab){
myURL=tab.url;
chrome.tabs.query({'active': true,'currentWindow':true},function(tab){
//Be aware 'tab' is an array of tabs even though it only has 1 tab in it
chrome.tabs.sendMessage(tab[0].id,{type: "dom-loaded2", color: "#F00",greeting:"hello"}, function(response){
//response will be the arrayWithAllTheInfoInIt that we sent back
//you can do whatever you want with it here
//I will just output it in console
console.log('response.farewell');
});
});
});
});
/*
chrome.tabs.onActivated.addListener(function(activeInfo) {
// how to fetch tab url using activeInfo.tabid
chrome.tabs.get(activeInfo.tabId, function(tab){
console.log("background");
console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension");
var requestObject = {"backgroundKey": "backgroundValue"};
chrome.tabs.sendRequest(activeInfo.tabId, requestObject,
function(value) { // used below
console.log(value);
});
});
});
/*chrome.browserAction.onClicked.addListener(function(tab) {
chrome.extension.sendMessage({ command: "selected-tab" },function(response) {
console.log(response.farewell);
});
}*/
/*
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.getSelected(null,function(tab) {
chrome.tabs.create( { url: "http://w3patrol.com/search.php?q=" +tab.url } );
});
});
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch(request.type) {
case "dom-loaded2":
//alert(request.data.myProperty);
alert('test');
console.log('Test');
callContent();
break;
}
return true;
});
/*
function responseCallback(value) { // used below
console.log(value);
}
chrome.tabs.onSelectionChanged.addListener(
function handleSelectionChange(tabId, selectInfo) {
var requestObject = {"backgroundKey": "backgroundValue"};
chrome.tabs.sendRequest(tabId, requestObject,
responseCallback);
}
);*/
/*
chrome.runtime.onConnect.addListener(function(port) {
console.assert(port.name == "knockknock");
port.onMessage.addListener(function(msg) {
if (msg.joke == "Knock knock")
port.postMessage({question: "Who's there?"});
else if (msg.answer == "Madame")
port.postMessage({question: "Madame who?"});
else if (msg.answer == "Madame... Bovary")
port.postMessage({question: "I don't get it."});
});
});
*/
// listening for an event / one-time requests
// coming from the popup
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch(request.type) {
case "color-divs":
colorDivs();
break;
case "dom-loaded2":
//alert(request.data.myProperty);
console.log('background.js message received'+request.type);
//callContent();
/*chrome.tabs.query({
active: true, // Select active tabs
lastFocusedWindow: true // In the current window
}, function(array_of_Tabs) {
// Since there can only be one active tab in one active window,
// the array has only one element
var tab = array_of_Tabs[0];
chrome.tabs.sendMessage(tab.id, {type: "dom-loaded2", color: "#F00"}, function(response) {
//console.log(response.farewell);
console.log('sent from background to conetnt');
});
});
/*chrome.tabs.getCurrent(function(tab){
chrome.tabs.sendMessage(tab.id, {type: "dom-loaded2", color: "#F00"}, function(response) {
console.log(response.farewell);
console.log('sent from background to conetnt');
});
});*/
break;
}
return true;
});
/*
var callContent = function() {
chrome.tabs.getSelected(null, function(tab){
chrome.tabs.sendMessage(tab[0].id, {type: "dom-loaded2", color: "#F00"});
// setting a badge
chrome.browserAction.setBadgeText({text: "red!"});
});
};
// send a message to the content script
var colorDivs = function() {
chrome.tabs.getSelected(null, function(tab){
chrome.tabs.sendMessage(tab[0].id, {type: "colors-div", color: "#F00"});
// setting a badge
chrome.browserAction.setBadgeText({text: "red!"});
});
};/
/*chrome.tabs.getAllInWindow(null, function(tabs){
tabs.forEach(function(tab){
alert("Hello world\n" + tab.url);
});
});*/