-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
37 lines (34 loc) · 1.25 KB
/
content.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
//content script, this one interfaces with the page, as opposed to Pouch
//
//content scripts are run in an isolated environment,
//so we can fool around however we want with globals and such
var runtimeOrExtension = chrome.runtime && chrome.runtime.sendMessage ? 'runtime' : 'extension';
var contentport = chrome[runtimeOrExtension].connect({name: "page"});
chrome[runtimeOrExtension].onConnect.addListener(function(port){
port.onMessage.addListener(function(message){
switch (message.command){
case 'getcheckbox':
var name = message.name || '_';
var checkboxes = $(':checkbox');
var arr = $.makeArray(checkboxes);
var vals = $.map(arr, function(elem, index){
return elem.checked;
});
contentport.postMessage({name: name, vals: vals, type: "content"});
break;
case 'setcheckbox':
var name = message.name || '_';
var checkboxes = $(':checkbox');
var data = message.data;
var arr = $.makeArray(checkboxes);
var vals = message.data.vals;
for (var i = 0; i < arr.length; i++){
arr[i].checked = data.vals[i];
}
contentport.postMessage({name: name, vals: vals});
break;
default:
break;
}
});
});