-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommunication.js
68 lines (59 loc) · 2.1 KB
/
communication.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
// Get information about the tabs
var gettingActive = browser.tabs.query({
currentWindow: true, active: true
});
gettingActive.then(checkCheckbox, onError);
// Check the status of checkbox
function checkCheckbox(tabs) {
let checkbox = document.getElementById("check");
let filteringText = document.getElementById("filteringText");
let blockedContent = document.getElementById("blockedContent");
let url = document.getElementById("url");
browser.tabs.sendMessage(
tabs[0].id,
{greeting: "Checkbox status"}
).then(response => {
checkbox.checked = response.ischecked;
blockedContent.innerText = response.blocked;
url.innerText = response.url;
console.log(url.innerText);
if(response.ischecked == true){
filteringText.innerText = "Toggle to Stop Filtering";
}
else{
filteringText.innerText = "Toggle to Start Filtering";
}
}).catch(onError);
}
// Start ot stop filtering based on a click on the checkbox
function startOrStopFiltering(tabs) {
let checkbox = document.getElementById("check");
let ischecked = checkbox.checked;
if(ischecked == true){
filteringText.innerText = "Toggle to Stop Filtering";
}
else{
filteringText.innerText = "Toggle to Start Filtering";
}
browser.tabs.sendMessage(
tabs[0].id,
{greeting: "Hi from background script", "isFiltering": ischecked}
).then(response => {
console.log("Message from the content script:");
console.log(response.response);
}).catch(onError);
}
// Error handling
function onError(error) {
console.log('Error: ${error}');
let filteringText = document.getElementById("filteringText");
let checkbox = document.getElementById("check");
filteringText.innerText = "Filtering not supported for this site";
checkbox.checked = false;
}
// On a click on the checkbox, communicate with the content script
function sendmessage(){
gettingActive.then(startOrStopFiltering, onError);
}
let checkbox = document.getElementById("check");
checkbox.addEventListener("click", sendmessage);