-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
94 lines (79 loc) · 2.6 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
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
//set default values
var _hostname = "localhost"
var _active = false;
var bReady = false;
//find browser type
if (typeof chrome !== "undefined")
if (typeof browser !== "undefined")
browserAgent = "Firefox";
else browserAgent = "Chrome";
else
browserAgent = "Edge";
//override console.log to write on background elements console
console.log = function(txt){
port.postMessage({active:_active, host:_hostname, debug:txt});
}
//create pipe to communicate between popup and extension backend
if (browserAgent == "Chrome")
var port = chrome.extension.connect({
name: "Variable Highway for Polluter"
});
else //for firefox, idk about IE
var port = browser.runtime.connect({
name: "Variable Highway for Polluter"
});
//when there is a response from backend
port.onMessage.addListener(function(msg){
_active = msg.active;
_hostname = msg.host;
if(_active==true) _extEnable();
else _extDisable()
if(bReady) document.getElementById('host-field').value = _hostname; //ask what host we were using to background.
})
//set host for the backend
function setHost(){
_hostname = document.getElementById('host-field').value;
port.postMessage({active:_active, host:_hostname});
window.close();
}
//set active/deactive for backend
function toggleExtension(){
if(_active==false){
_extEnable();
}else{
_extDisable();
}
port.postMessage({active:_active, host:_hostname});
}
//manage variables and visuals for activation
function _extEnable(){
console.log("Enabling extension")
_active=true;
document.getElementById("ext-btn").classList.add("ext-enabled");
document.getElementById("ext-btn").classList.remove("ext-disabled");
document.getElementById("ext-btn").innerHTML = "Enabled";
}
//manage variables and visuals for deactivation
function _extDisable(){
console.log("Disabling extension")
_active=false;
document.getElementById("ext-btn").classList.remove("ext-enabled");
document.getElementById("ext-btn").classList.add("ext-disabled");
document.getElementById("ext-btn").innerHTML = "Disabled";
}
//do when extension button is clicked
function initializePopup(state){
if(state==0) console.log("Dom Loaded")
console.log("Beep Boop Hostname popup online")
document.getElementById('ext-btn').onclick = function(){toggleExtension()}
document.getElementById('ok-btn').onclick = function(){setHost()}
bReady = true;
}
//initialize
console.log("Dom state: " + document.readyState)
//dom may sometimes load before event listener is registered. This eliminates that misfortune.
if(document.readyState === 'loading') {
document.addEventListener("DOMContentLoaded", function(){initializePopup(0)}, false);
}else{
initializePopup(1)
}