From b0c6f7a2bff3d57f1c8d05a561dbd0bbca0228ca Mon Sep 17 00:00:00 2001 From: Einar Egilsson Date: Sat, 2 Jun 2007 00:24:35 +0000 Subject: Redirector: All main functionality except Regex working. git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@57 119bf307-c92d-0410-89bd-8f53e6181181 --- chrome/content/redirector.js | 59 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 13 deletions(-) (limited to 'chrome/content/redirector.js') diff --git a/chrome/content/redirector.js b/chrome/content/redirector.js index 63ee85d..8ec4af7 100644 --- a/chrome/content/redirector.js +++ b/chrome/content/redirector.js @@ -5,6 +5,8 @@ var Redirector = { list : [], + enabled : true, + init : function() { this.load(); this.prefObserver.register(); @@ -43,42 +45,67 @@ var Redirector = { this.save(); }, + deleteAt : function(index) { + this.list.splice(index, 1); + this.save(); + }, + processUrl : function(url) { - var redirect, link, links; + var redirect, link, links, redirectUrl, match; + + if (!this.enabled) { + return; + } + for each (redirect in this.list) { - if (redirect.patternType == kRedirectorWildcard && this.wildcardMatch(redirect.pattern, url)) { + var redirectUrl; + + if (redirect.patternType == kRedirectorWildcard) { + match = this.wildcardMatch(redirect.pattern, url); + redirectUrl = redirect.redirectUrl; + } else if (redirect.patternType == kRedirectorRegex) { + match = this.regexMatch(redirect.pattern, url); + redirectUrl = redirect.redirectUrl; + } + + if (match) { 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); + if (link.href && link.href.toString() == redirectUrl) { + RedirLib.debug('Found a link for %1'._(redirectUrl)); + this._goto(redirectUrl, redirect.pattern); return; } } - RedirLib.debug('Did not find a link for %1'._(redirect.redirectUrl)); + RedirLib.debug('Did not find a link for %1'._(redirectUrl)); } else { - this._goto(redirect); + this._goto(redirectUrl, redirect.pattern); } } } }, - _goto : function(redirect) { + _goto : function(redirectUrl, pattern) { - if (redirect.redirectUrl == window.content.location.href) { - RedirLib.msgBox(this.strings.getString('extensionName'), this.strings.getFormattedString('recursiveError', [redirect.pattern, redirect.redirectUrl])); + if (redirectUrl == window.content.location.href) { + RedirLib.msgBox(this.strings.getString('extensionName'), this.strings.getFormattedString('recursiveError', [pattern, redirectUrl])); } else { - window.content.location.href = redirect.redirectUrl; + window.content.location.href = redirectUrl; } }, + regexMatch : function(pattern, text) { + alert('regexmatch'); + + }, + wildcardMatch : function(pattern, text) { var parts , part @@ -127,10 +154,16 @@ var Redirector = { }, observe : function(subject, topic, data) { - if (topic == 'nsPref:changed' && data == 'extensions.redirector.redirects') { + if (topic != 'nsPref:changed') { + return; + } + + if (data == 'extensions.redirector.redirects') { Redirector.load(); + } else if (data == 'extensions.redirector.enabled') { + Redirector.enabled = RedirLib.getBoolPref('enabled'); } } }, -}; \ No newline at end of file +}; -- cgit v1.2.3-70-g09d2