From ca4130f47bbe7f461dc1bc29d4aebb2dec305677 Mon Sep 17 00:00:00 2001 From: Einar Egilsson Date: Mon, 28 May 2007 22:35:59 +0000 Subject: git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@55 119bf307-c92d-0410-89bd-8f53e6181181 --- chrome/content/redirector.js | 163 ++++++++++++++++++++++--------------------- 1 file changed, 84 insertions(+), 79 deletions(-) (limited to 'chrome/content/redirector.js') diff --git a/chrome/content/redirector.js b/chrome/content/redirector.js index 732602b..73d59fa 100644 --- a/chrome/content/redirector.js +++ b/chrome/content/redirector.js @@ -1,71 +1,64 @@ -//// $Id$ +const kRedirectorWildcard = 'W'; +const kRedirectorRegex= 'R'; var Redirector = { - id : "redirector@einaregilsson.com", - name : "Redirector", - initialized : false, - strings : null, - redirects : [], + list : [], - onLoad : function(event) { - try { - - // initialization code - RedirLib.initialize(this); - RedirLib.debug("Initializing..."); - - $('contentAreaContextMenu') - .addEventListener("popupshowing", function(e) { Redirector.showContextMenu(e); }, false); - - this.redirects = eval(RedirLib.getCharPref('redirects')); - - var appcontent = window.document.getElementById('appcontent'); - - if (appcontent && !appcontent.processed) { - appcontent.processed = true; + init : function() { + this.load(); + this.prefObserver.register(); + }, - appcontent.addEventListener('DOMContentLoaded', function(event) { + save : function() { + var r + , tempList = []; - Redirector.onDOMContentLoaded(event); + for each (r in this.list) { + tempList.push([r.exampleUrl, r.pattern, r.redirectUrl, r.onlyIfLinkExists, r.patternType]); + } + alert(tempList.toSource()); + RedirLib.setCharPref('redirects', tempList.toSource()); + }, - }, false); - } - this.strings = document.getElementById("redirector-strings"); + load : function() { + var tempList = eval(RedirLib.getCharPref('redirects')); + var arr; - RedirLib.debug("Finished initialization"); - this.initialized = true; + this.list = []; - } catch(e) { - //Don't use RedirLib because it's initialization might have failed. - if (this.strings) { - alert(this.strings.getString("initError")._(this.name) + "\n\n" + e); - } else { - alert(e); - } + for each (arr in tempList) { + this.list.push({ + exampleUrl : arr[0], + pattern : arr[1], + redirectUrl : arr[2], + onlyIfLinkExists : arr[3], + patternType : arr[4] + }); } - }, - onDOMContentLoaded : function(event) { - var redirect, link, links, url; + }, - url = window.content.location.href; + addRedirect : function(redirect) { + this.list.push(redirect); + alert(redirect.toSource()); + this.save(); + }, - RedirLib.debug('Processing url %1'._(url)); + processUrl : function(url) { + var redirect, link, links; + for each (redirect in this.list) { - for each (redirect in this.redirects) { - if (RedirectorCommon.wildcardMatch(redirect.pattern, url)) { + if (redirect.patternType == kRedirectorWildcard && this.wildcardMatch(redirect.pattern, url)) { RedirLib.debug('%1 matches %2'._(redirect.pattern, url)); - if (redirect.onlyIfLinkExists) { - links = window.content.document.getElementsByTagName('a'); for each(link in links) { if (link.href && link.href.toString() == redirect.redirectUrl) { RedirLib.debug('Found a link for %1'._(redirect.redirectUrl)); - this.goto(redirect); + this._goto(redirect); return; } } @@ -73,14 +66,13 @@ var Redirector = { RedirLib.debug('Did not find a link for %1'._(redirect.redirectUrl)); } else { - this.goto(redirect); + this._goto(redirect); } } } - }, - goto : function(redirect) { + _goto : function(redirect) { if (redirect.redirectUrl == window.content.location.href) { RedirLib.msgBox(this.strings.getString('extensionName'), this.strings.getFormattedString('recursiveError', [redirect.pattern, redirect.redirectUrl])); @@ -89,45 +81,58 @@ var Redirector = { } }, - onUnload : function(event) { - //Clean up here - RedirLib.debug("Finished cleanup"); - }, + wildcardMatch : function(pattern, text) { + var parts + , part + , i + , pos; - showContextMenu : function(event) { - if (gContextMenu.onLink) { - $("redirector-context").label = this.strings.getString('addLinkUrl'); - } else { - $("redirector-context").label = this.strings.getString('addCurrentUrl'); - } - }, + parts = pattern.split('*'); - onContextMenuCommand: function(event) { + for (i in parts) { - params = { inn : { url : window.content.location.href}, out : {} }; - if (gContextMenu.onLink) { - params.inn.redirect = gContextMenu.link.toString(); - } + part = parts[i]; + + pos = text.indexOf(part); + + if (pos == -1) { + return false; + } + + if (i == 0 && pos != 0) { + return false; + } - window.openDialog("chrome://redirector/content/redirect.xul", - "redirect", - "chrome,dialog,modal,centerscreen", params); + if (i == parts.length -1 && i != "" && text.substr(text.length - part.length) != part) { + return false; - if (params.out.pattern) { - this.redirects.push(params.out); + } + + text = text.substr(pos + part.length); } - RedirLib.setCharPref('redirects', this.redirects.toSource()); + return true; }, - onMenuItemCommand: function(event) { - window.openDialog("chrome://redirector/content/redirectList.xul", - "redirectList", - "chrome,dialog,modal,centerscreen", this); + prefObserver : { - }, + getService : function() { + return Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranchInternal); + }, + + register: function() { + this.getService().addObserver('extensions.redirector', this, false); + }, -}; + unregister: function() { + this.getService().removeObserver('extensions.redirector', this); + }, -window.addEventListener("load", function(event) { Redirector.onLoad(event); }, false); -window.addEventListener("unload", function(event) { Redirector.onUnload(event); }, false); \ No newline at end of file + observe : function(subject, topic, data) { + if (topic == 'nsPref:changed' && data == 'extensions.redirector.redirects') { + Redirector.load(); + } + } + + }, +}; \ No newline at end of file -- cgit v1.2.3-70-g09d2