Skip to content

Commit

Permalink
Add browser-action, disabling feature, and redirect/block counts
Browse files Browse the repository at this point in the history
  • Loading branch information
Nateowami committed Jan 12, 2015
1 parent 2517fa1 commit c714623
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 14 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "Backloader",
"version": "0.1.0",
"description": "A Chrome extension to redirect CDN requests to another CDN or local resource",
"description": "A Chrome extension to redirect CDN requests to another CDN",

"permissions": [
"storage",
Expand All @@ -13,6 +13,18 @@
],

"background": {
"scripts": ["scripts/background.js", "scripts/list.js"]
"scripts": ["scripts/list.js", "scripts/background.js"],
"css": ["popup.css"]
},

"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html",
"default_title": "Backloader"
},

"icons": {
"128": "icon.png"
}

}
8 changes: 8 additions & 0 deletions popup.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body {
background-color: #ffd
}

.info {
white-space: nowrap;
padding: 3pt;
}
21 changes: 21 additions & 0 deletions popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="popup.css"></style>
<script src="scripts/popup.js" ></script>
</head>
<body>
<div class="info">
<input type="checkbox" checked="true" id="enabled">
<label for="enabled">Enable redirects and blocking</label>
</div>

<hr>

<div class="info">URLs redirected: <span id="redirect-count"></span>
</div>
<div class="info">URLs blocked: <span id="block-count"></span>
</div>
</body>
<html>
47 changes: 47 additions & 0 deletions scripts/background.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
//set "enabled" to true on first run
chrome.runtime.onInstalled.addListener(function(details){
if(details.reason === "install"){
chrome.storage.local.set({enabled: true,
redirected: 0,
blocked: 0});
}
});

//add event listener for updates to local storage
chrome.storage.onChanged.addListener(updateEnabled);

//update backloader.enabled when it get's changed in local storage
function updateEnabled(changes, namespace) {
for (key in changes) {
var change = changes[key];
//only do something if it's "enabled" that gets updated
if(key == "enabled" && namespace == "local"){
backloader.enabled = change.newValue;
console.log("Updated local-storage-enable-status from " + change.oldValue + " to " + change.newValue);
}
}
}

var backloader = {
redirected: 0,
blocked: 0,
enabled: true,

/**
* Increments the value indicated by key (may be "redirected"
* or "blocked").
* @param {String} key - The variable to increment.
*/
increment: function(key){
var obj = {};
obj[key] = ++this[key];
chrome.storage.local.set(obj);
console.log("updated " + key + " to " + this[key]);
}
};

//register an event listener for all web requests
chrome.webRequest.onBeforeRequest.addListener(manageRequest, {urls: ["<all_urls>"]}, ["blocking"]);

Expand All @@ -7,6 +49,9 @@ chrome.webRequest.onBeforeRequest.addListener(manageRequest, {urls: ["<all_urls>
* @param request - The webrequest.
*/
function manageRequest(request){
//only run if filtering is enabled
if(!backloader.enabled) return;

var list = completeFilterList();
for(var i in list){
if(match(list[i].source, request.url)){
Expand All @@ -17,12 +62,14 @@ function manageRequest(request){

if(redirect){
console.log("Redirecting "+request.url+" to " + redirect);
backloader.increment("redirected");
return {redirectUrl: redirect};
}

else {
//block the request
console.log("Blocking " + request.url);
backloader.increment("blocked");
return {cancel: true};
}

Expand Down
20 changes: 20 additions & 0 deletions scripts/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
window.onload = function(){
chrome.storage.local.get(
["enabled", "redirected", "blocked"], initPopup);
};

function initPopup(items){
//add event listener to checkbox
document.getElementById("enabled").onclick = toggle;
//set whether the checkbox is checked
document.getElementById("enabled").checked = items.enabled;
//set the number redirected
document.getElementById("redirect-count").innerText= items.redirected;
//set the number blocked
document.getElementById("block-count").innerText = items.blocked;
}

function toggle(checkbox){
console.log("Changing enable-status to " + this.checked);
chrome.storage.local.set({enabled: this.checked});
}

0 comments on commit c714623

Please sign in to comment.