-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.js
More file actions
122 lines (96 loc) · 3.2 KB
/
menu.js
File metadata and controls
122 lines (96 loc) · 3.2 KB
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
/**
* @author Jepp3
*
**/
/**
* retrives form data from the html page
* @returns {Object}
**/
function retriveFormData() {
var formData = {};
formData.urlsToTriggerOn = document.getElementById('urlsToTriggerOn').value;
formData.urlReplacePattern = document.getElementById('urlReplacePattern').value;
formData.urlReplaceContent = document.getElementById('urlReplaceContent').value;
return formData;
}
/**
* Will talk to the background page and tell the singletonInstance to
* infect the http trafic, based on the form data given by the user.
* @returns void
**/
function registerListners() {
console.log("register");
var otherWindows = chrome.extension.getBackgroundPage();
var singletonInstance = otherWindows.infectorSingletonInstance;
singletonInstance.infectHttpTrafic(retriveFormData());
}
/**
* Will talk to the background page and tell the singleton instance to stop
* infecting the trafic
* @returns void
**/
function unRegisterListner() {
console.log("unregister");
var otherWindows = chrome.extension.getBackgroundPage();
var singletonInstance = otherWindows.infectorSingletonInstance;
singletonInstance.stopInfectingTrafic();
}
/**
* Saves the form data to local storage . ( obs async )
*
**/
function saveTheFormData() {
chrome.storage.sync.set(retriveFormData(), function() {
// silently ignore this at the moment
});
}
/**
* populate the form with data fetch from the local storage
*
**/
function populateFormWithFormData() {
chrome.storage.sync.get(null, function(formData) {
document.getElementById('urlsToTriggerOn').value = formData.urlsToTriggerOn || "";
document.getElementById('urlReplacePattern').value = formData.urlReplacePattern || "";
document.getElementById('urlReplaceContent').value = formData.urlReplaceContent || "";
});
}
/**
* This methods sets the current state.
* if the infector is active, then "active" will be printed.
* if the infector is in-active then "in-active" will be printed.
**/
function setState() {
// get the infector
var otherWindows = chrome.extension.getBackgroundPage();
var singletonInstance = otherWindows.infectorSingletonInstance;
var stateInHumanReadableFormat = "The extension is disabled";
// ask the infector the current state
if(singletonInstance.isActive()) {
stateInHumanReadableFormat = "The extension is enabled";
}
// set the state in the widget
document.getElementById('stateIndicator').innerHTML = stateInHumanReadableFormat;
}
/**
* On page load
*
* Here we register events that will be triggered by the user.
* The methods here will be a "routing" to more heavier methods defined above.
*
**/
document.addEventListener('DOMContentLoaded', function() {
populateFormWithFormData();
setState();
// when the user clicks on the submit button
document.getElementById("submit-url-values").addEventListener("click", function() {
registerListners.call(this);
saveTheFormData.call(this);
setState.call(this);
}.bind(this));
// when the user clicks on the disable button
document.getElementById("disable").addEventListener("click", function() {
unRegisterListner.call(this);
setState.call(this);
}.bind(this));
});