-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
167 lines (121 loc) · 4.05 KB
/
background.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
chrome.runtime.onInstalled.addListener(function() {
// Get the current tab
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
var tab = tabs[0];
var tabId = tab.id;
chrome.tabs.get(tabId, function (tab) {
// Ignorar urls que inicien con chrome://
var jsSettings = chrome.contentSettings.javascript;
// Obtener permisos para la url actual
jsSettings.get({"primaryUrl": tab.url}, function (details) {
if (details.setting == "allow") {
// Show in tab
chrome.pageAction.setIcon({
"tabId": tabId,
"path": "/images/switch_on.png"
});
chrome.pageAction.show(tabId);
} else if (details.setting == "block") {
// Hide in tab
chrome.pageAction.setIcon({
"tabId": tabId,
"path": "/images/switch_off.png"
});
chrome.pageAction.show(tabId);
}
});
});
});
});
/*
* TODO:
* - Opción para aplicar regla solo en dominio (sin subdominio)
*
*/
chrome.pageAction.onClicked.addListener(function () {
// Get the current tab
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
var tab = tabs[0];
// Wait for the tab to load
if (tab.status != "complete") return;
// Solo permitir urls que inicien con http
if (!tab.url.startsWith("http")) return;
var jsSettings = chrome.contentSettings.javascript;
// Obtener permisos para la url actual
jsSettings.get({"primaryUrl": tab.url}, function (details) {
if (details.setting == "allow") {
// Disallow
chrome.pageAction.setIcon({
"tabId": tab.id,
"path": "/images/switch_off.png"
});
// Create pattern
var url = new URL(tab.url);
var topLevel = url.hostname.match(/[^.]*\.[^.]{2,3}(?:\.[^.]{2,3})?$/mg)[0];
// Set the port
var port;
if (!url.port) {
if (url.protocol == "http:") port = 80;
if (url.protocol == "https:") port = 443;
} else {
port = url.port;
}
// Pattern
var pattern = url.protocol + "//*." + topLevel + ":" + port + "/*";
// Set the permissions and reload.
jsSettings.set({"primaryPattern": pattern, "setting": "block"}, function () {
chrome.tabs.reload(tab.id);
});
} else if (details.setting == "block") {
// Allow
chrome.pageAction.setIcon({
"tabId": tab.id,
"path": "/images/switch_on.png"
});
// Create pattern
var url = new URL(tab.url);
var topLevel = url.hostname.match(/[^.]*\.[^.]{2,3}(?:\.[^.]{2,3})?$/mg)[0];
// Set the port
var port;
if (!url.port) {
if (url.protocol == "http:") port = 80;
if (url.protocol == "https:") port = 443;
} else {
port = url.port;
}
// Pattern
var pattern = url.protocol + "//*." + topLevel + ":" + port + "/*";
// Set the permissions and reload.
jsSettings.set({"primaryPattern": pattern, "setting": "allow"}, function () {
chrome.tabs.reload(tab.id);
});
}
});
});
});
// Evento en tab actualizada
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
if (changeInfo.status != "loading") return;
chrome.tabs.get(tabId, function (tab) {
// Ignorar urls que inicien con chrome://
var jsSettings = chrome.contentSettings.javascript;
// Obtener permisos para la url actual
jsSettings.get({"primaryUrl": tab.url}, function (details) {
if (details.setting == "allow") {
// Show in tab
chrome.pageAction.setIcon({
"tabId": tabId,
"path": "/images/switch_on.png"
});
chrome.pageAction.show(tabId);
} else if (details.setting == "block") {
// Hide in tab
chrome.pageAction.setIcon({
"tabId": tabId,
"path": "/images/switch_off.png"
});
chrome.pageAction.show(tabId);
}
});
});
});