Skip to content

Commit c714623

Browse files
committed
Add browser-action, disabling feature, and redirect/block counts
1 parent 2517fa1 commit c714623

File tree

6 files changed

+110
-2
lines changed

6 files changed

+110
-2
lines changed

icon.png

7.23 KB
Loading

manifest.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
"name": "Backloader",
55
"version": "0.1.0",
6-
"description": "A Chrome extension to redirect CDN requests to another CDN or local resource",
6+
"description": "A Chrome extension to redirect CDN requests to another CDN",
77

88
"permissions": [
99
"storage",
@@ -13,6 +13,18 @@
1313
],
1414

1515
"background": {
16-
"scripts": ["scripts/background.js", "scripts/list.js"]
16+
"scripts": ["scripts/list.js", "scripts/background.js"],
17+
"css": ["popup.css"]
18+
},
19+
20+
"browser_action": {
21+
"default_icon": "icon.png",
22+
"default_popup": "popup.html",
23+
"default_title": "Backloader"
24+
},
25+
26+
"icons": {
27+
"128": "icon.png"
1728
}
29+
1830
}

popup.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
body {
2+
background-color: #ffd
3+
}
4+
5+
.info {
6+
white-space: nowrap;
7+
padding: 3pt;
8+
}

popup.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<link rel="stylesheet" href="popup.css"></style>
6+
<script src="scripts/popup.js" ></script>
7+
</head>
8+
<body>
9+
<div class="info">
10+
<input type="checkbox" checked="true" id="enabled">
11+
<label for="enabled">Enable redirects and blocking</label>
12+
</div>
13+
14+
<hr>
15+
16+
<div class="info">URLs redirected: <span id="redirect-count"></span>
17+
</div>
18+
<div class="info">URLs blocked: <span id="block-count"></span>
19+
</div>
20+
</body>
21+
<html>

scripts/background.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
1+
//set "enabled" to true on first run
2+
chrome.runtime.onInstalled.addListener(function(details){
3+
if(details.reason === "install"){
4+
chrome.storage.local.set({enabled: true,
5+
redirected: 0,
6+
blocked: 0});
7+
}
8+
});
9+
10+
//add event listener for updates to local storage
11+
chrome.storage.onChanged.addListener(updateEnabled);
12+
13+
//update backloader.enabled when it get's changed in local storage
14+
function updateEnabled(changes, namespace) {
15+
for (key in changes) {
16+
var change = changes[key];
17+
//only do something if it's "enabled" that gets updated
18+
if(key == "enabled" && namespace == "local"){
19+
backloader.enabled = change.newValue;
20+
console.log("Updated local-storage-enable-status from " + change.oldValue + " to " + change.newValue);
21+
}
22+
}
23+
}
24+
25+
var backloader = {
26+
redirected: 0,
27+
blocked: 0,
28+
enabled: true,
29+
30+
/**
31+
* Increments the value indicated by key (may be "redirected"
32+
* or "blocked").
33+
* @param {String} key - The variable to increment.
34+
*/
35+
increment: function(key){
36+
var obj = {};
37+
obj[key] = ++this[key];
38+
chrome.storage.local.set(obj);
39+
console.log("updated " + key + " to " + this[key]);
40+
}
41+
};
42+
143
//register an event listener for all web requests
244
chrome.webRequest.onBeforeRequest.addListener(manageRequest, {urls: ["<all_urls>"]}, ["blocking"]);
345

@@ -7,6 +49,9 @@ chrome.webRequest.onBeforeRequest.addListener(manageRequest, {urls: ["<all_urls>
749
* @param request - The webrequest.
850
*/
951
function manageRequest(request){
52+
//only run if filtering is enabled
53+
if(!backloader.enabled) return;
54+
1055
var list = completeFilterList();
1156
for(var i in list){
1257
if(match(list[i].source, request.url)){
@@ -17,12 +62,14 @@ function manageRequest(request){
1762

1863
if(redirect){
1964
console.log("Redirecting "+request.url+" to " + redirect);
65+
backloader.increment("redirected");
2066
return {redirectUrl: redirect};
2167
}
2268

2369
else {
2470
//block the request
2571
console.log("Blocking " + request.url);
72+
backloader.increment("blocked");
2673
return {cancel: true};
2774
}
2875

scripts/popup.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
window.onload = function(){
2+
chrome.storage.local.get(
3+
["enabled", "redirected", "blocked"], initPopup);
4+
};
5+
6+
function initPopup(items){
7+
//add event listener to checkbox
8+
document.getElementById("enabled").onclick = toggle;
9+
//set whether the checkbox is checked
10+
document.getElementById("enabled").checked = items.enabled;
11+
//set the number redirected
12+
document.getElementById("redirect-count").innerText= items.redirected;
13+
//set the number blocked
14+
document.getElementById("block-count").innerText = items.blocked;
15+
}
16+
17+
function toggle(checkbox){
18+
console.log("Changing enable-status to " + this.checked);
19+
chrome.storage.local.set({enabled: this.checked});
20+
}

0 commit comments

Comments
 (0)