Permalink
Browse files

Manifest 2 and updated API calls - Thanks to Matthew Duggan

  • Loading branch information...
1 parent 8ab79eb commit c62f870771b2d6cdb6f1805de13dd92122a35c13 @ocodia committed Jul 10, 2012
Showing with 110 additions and 12 deletions.
  1. +102 −0 background.js
  2. +4 −4 check.js
  3. +4 −8 manifest.json
View
@@ -0,0 +1,102 @@
+var logging = false;
+
+chrome.extension.onMessage.addListener(onRequest);
+
+chrome.browserAction.onClicked.addListener(function (tab) {
+ chrome.tabs.executeScript(tab.id, {file:'check.js'}, function () {
+ var blacklistDefaults =
+ "googleleads.g.doubleclick.net\n" +
+ "doubleclick.net\n" +
+ "googleadservices.com\n" +
+ "www.googleadservices.com\n" +
+ "googlesyndication.com\n" +
+ "adservices.google.com\n" +
+ "appliedsemantics.com";
+
+ // Set up the defaults if no values are present in LocalStorage
+ if (getItem("blacklist") === null) {
+ setItem("blacklist", blacklistDefaults);
+ }
+
+ var blacklist = getItem("blacklist");
+
+ chrome.tabs.sendMessage(tab.id, {bl:blacklist});
+ });
+});
+
+function onRequest(request, sender, callback) {
+ if (request.action == "check") {
+ if (request.url) {
+ check(request.url, callback);
+ }
+ }
+ return true;
+}
+
+// Timeout for each link is 60+1 seconds
+var timeout = 30000;
+
+function check(url, callback) {
+ var XMLHttpTimeout = null;
+ var xhr = new XMLHttpRequest();
+ xhr.onreadystatechange = function (data) {
+ if (xhr.readyState == 4) {
+ clearTimeout(XMLHttpTimeout);
+ return callback(xhr.status);
+ }
+ };
+
+ try {
+ xhr.open("HEAD", url, true);
+ xhr.send();
+ }
+ catch(e){
+ console.log(e);
+ }
+
+ XMLHttpTimeout=setTimeout(function (){return callback(408); xhr.abort();}, timeout += 1000);
+}
+
+// OPTIONS: Management
+
+// OPTIONS: Set items in localstore
+function setItem(key, value) {
+ try {
+ log("Inside setItem:" + key + ":" + value);
+ window.localStorage.removeItem(key);
+ window.localStorage.setItem(key, value);
+ }catch(e) {
+ log("Error inside setItem");
+ log(e);
+ }
+ log("Return from setItem" + key + ":" + value);
+}
+
+// OPTIONS: Get items from localstore
+function getItem(key) {
+ var value;
+ log('Get Item:' + key);
+ try {
+ value = window.localStorage.getItem(key);
+ }catch(e) {
+ log("Error inside getItem() for key:" + key);
+ log(e);
+ value = "null";
+ }
+ log("Returning value: " + value);
+ return value;
+}
+
+// OPTIONS: Zap all items in localstore
+function clearStrg() {
+ log('about to clear local storage');
+ window.localStorage.clear();
+ log('cleared');
+}
+
+function log(txt) {
+ if(logging) {
+ console.log(txt);
+ }
+}
+
View
@@ -10,9 +10,9 @@ String.prototype.contains = function(text) {
};
-chrome.extension.onRequest.addListener(
+chrome.extension.onMessage.addListener(
- function(request, sender, sendResponse) {
+ function(request, sender) {
// Gather links
var pageLinks = document.getElementsByTagName('a');
@@ -140,7 +140,7 @@ chrome.extension.onRequest.addListener(
// Send links to get checked via XHR
function checkURL(url, link) {
- chrome.extension.sendRequest({"action": "check", "url": url},
+ chrome.extension.sendMessage({"action": "check", "url": url},
function (response) {
if (response) {
if (200 <= response && response < 400) {
@@ -165,7 +165,7 @@ chrome.extension.onRequest.addListener(
});
}
- sendResponse({});
+ return true;
});
View
@@ -1,20 +1,16 @@
{
"options_page": "options.html",
- "background_page": "background.html",
"browser_action": {
"default_icon": "weeicon.png",
"default_title": "Check my links"
},
- "content_scripts": [ {
- "all_frames": true,
- "js": ["check.js"],
- "matches": [ "http://*/*", "https://*/*" ]
- } ],
"description": "Check My Links is a link checker that crawls through your webpage and looks for broken links.",
"icons": { "128": "icon.png" },
"name": "Check My Links (External)",
"permissions": [ "tabs", "http://*/*", "https://*/*" ],
- "version": "3.0.2",
- "update_url": "http://code.ocodia.com/extensions/cml/updates.xml"
+ "version": "3.1.0",
+ "manifest_version": 2,
+ "update_url": "http://code.ocodia.com/extensions/cml/updates.xml",
+ "background": { "scripts": ["background.js"] }
}

0 comments on commit c62f870

Please sign in to comment.