-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
151 lines (101 loc) · 3.77 KB
/
popup.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
145
window.addEventListener("load", init, false);
///---------Include list_tools.js------------------------------
function Link(url) {
this.url = url;
this.hits = 0;
}
function findMatchingLink(url, list) {
var target = findMatchingLinkIndex(url, list);
if(target != -1) { return list[target]; }
else return null;
}
function findMatchingLinkIndex(url, list) {
if(list == null) { return -1; }
for (var i = 0; i < list.length; i++) {
if (url.indexOf(list[i].url) != -1) {
return i; }
}
return -1;
}
function removeMatchingLink(url, list) {
var target = findMatchingLinkIndex(url, list);
if(target != -1) { list.splice(target, 1); }
}
///---------------------------------------------------------------
function Button(id, offText, onText, sublistName) {
this.id = id;
this.onText = onText;
this.offText = offText;
this.sublistName = sublistName;
this.sublist = null;
this.element = null;
}
var blamButton = new Button("blam", "BLAM IT!", "UNBLAM IT", "black_list");
var highlightButton = new Button("highlight", "Highlight Site", "Unhighlight Site", "a_list");
// var black_list;
// var blam;
var current_site
function init() {
chrome.tabs.query({active: true, currentWindow: true}, function(arrayOfTabs) {
current_site = arrayOfTabs[0].url;
chrome.storage.local.get('black_list', function(result){
if(result.black_list != null) { blamButton.sublist = result.black_list; }
else { blamButton.sublist = []; }
initButton(blamButton);
});
chrome.storage.local.get('a_list', function(result){
if(result.a_list != null) { highlightButton.sublist = result.a_list; }
else { highlightButton.sublist = []; }
initButton(highlightButton);
});
});
var ln = document.getElementById("title_bar");
var location = ln.href;
ln.onclick = function () {
chrome.tabs.create({active: true, url: location}); };
}
function initButton(button) {
button.element = document.getElementById(button.id);
updateButton(button, false);
button.element.onclick = function() {
updateButton(button, true); }
}
function updateButton(button, withFlip) {
var linkIndex = findMatchingLinkIndex(current_site, button.sublist);
console.log(linkIndex);
var buttonOn = (linkIndex != -1);
if(withFlip == true) {
if(buttonOn == false) {
var link = new Link(getSiteName(current_site));
button.sublist.push(link);
}
else { button.sublist.splice(linkIndex, 1); }
buttonOn = !buttonOn;
if(button.sublistName == "black_list") {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {updateBlam: buttonOn}, function(response) {});
});
chrome.storage.local.set({ 'black_list' : button.sublist }, function() {}); }
else {
chrome.storage.local.set({ 'a_list' : button.sublist }, function() {}); }
}
if(buttonOn == true) {
button.element.className = "engaged_button";
button.element.innerHTML = button.onText; }
else {
button.element.className = "";
button.element.innerHTML = button.offText; }
}
var tlds = [".com", ".net", ".org", ".biz", ".name", ".info", ".edu", ".gov", ".ag"];
function getSiteName(url) {
console.log(url);
for (var i = 0; i < tlds.length; i++) {
var target = url.indexOf(tlds[i]);
if(target != -1) {
var output = url.substring(0, target+tlds[i].length);
console.log("lalal");
return output;
}
}
return url;
}