From a7da44a01a575246c9fb55665c15ce9a9a400814 Mon Sep 17 00:00:00 2001 From: Einar Egilsson Date: Mon, 21 May 2007 23:21:52 +0000 Subject: Redirector git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@54 119bf307-c92d-0410-89bd-8f53e6181181 --- chrome/content/redirector.js | 46 +++++++++++++++++++++++++++---- chrome/content/redirlib.js | 4 +++ chrome/locale/en-US/redirector.properties | 4 ++- defaults/preferences/redirector.js | 1 + 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/chrome/content/redirector.js b/chrome/content/redirector.js index 6767840..732602b 100644 --- a/chrome/content/redirector.js +++ b/chrome/content/redirector.js @@ -17,6 +17,9 @@ var Redirector = { $('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) { @@ -44,17 +47,48 @@ var Redirector = { }, onDOMContentLoaded : function(event) { - var redirect; - try { + var redirect, link, links, url; + + url = window.content.location.href; + + RedirLib.debug('Processing url %1'._(url)); + for each (redirect in this.redirects) { - if (RedirectorCommon.wildcardMatch(redirect.pattern, window.content.location.href)) { - window.content.location.href = redirect.redirectUrl; + if (RedirectorCommon.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); + return; + } + } + + RedirLib.debug('Did not find a link for %1'._(redirect.redirectUrl)); + + } else { + this.goto(redirect); + } } } - } catch(e) {alert(e);} }, + goto : function(redirect) { + + if (redirect.redirectUrl == window.content.location.href) { + RedirLib.msgBox(this.strings.getString('extensionName'), this.strings.getFormattedString('recursiveError', [redirect.pattern, redirect.redirectUrl])); + } else { + window.content.location.href = redirect.redirectUrl; + } + }, + onUnload : function(event) { //Clean up here RedirLib.debug("Finished cleanup"); @@ -82,6 +116,8 @@ var Redirector = { if (params.out.pattern) { this.redirects.push(params.out); } + + RedirLib.setCharPref('redirects', this.redirects.toSource()); }, onMenuItemCommand: function(event) { diff --git a/chrome/content/redirlib.js b/chrome/content/redirlib.js index 493c583..71856dd 100644 --- a/chrome/content/redirlib.js +++ b/chrome/content/redirlib.js @@ -66,6 +66,10 @@ var RedirLib = { return this._prefBranch.getCharPref(branch); }, + setCharPref : function(branch, value) { + return this._prefBranch.setCharPref(branch, value); + }, + getBoolPref : function(branch) { return this._prefBranch.getBoolPref(branch); }, diff --git a/chrome/locale/en-US/redirector.properties b/chrome/locale/en-US/redirector.properties index 110f82c..5d01a1f 100644 --- a/chrome/locale/en-US/redirector.properties +++ b/chrome/locale/en-US/redirector.properties @@ -1,4 +1,6 @@ initError=Failed to initialize %1. extensions.redirector@einaregilsson.com.description=Automatically redirects to user-defined urls on certain pages +extensionName=Redirector addCurrentUrl=Add current url to Redirector -addLinkUrl=Add link url to Redirector \ No newline at end of file +addLinkUrl=Add link url to Redirector +recursiveError=A redirect with the pattern %S matches %S and is trying to redirect to it again. You should change this rule so it won't work recursively. \ No newline at end of file diff --git a/defaults/preferences/redirector.js b/defaults/preferences/redirector.js index d7985a9..7b108aa 100644 --- a/defaults/preferences/redirector.js +++ b/defaults/preferences/redirector.js @@ -1,6 +1,7 @@ //// $Id$ pref("extensions.redirector.debug", false); +pref("extensions.redirector.redirects", '[]'); // See http://kb.mozillazine.org/Localize_extension_descriptions pref("extensions.redirector@einaregilsson.com.description", "chrome://redirector/locale/redirector.properties"); \ No newline at end of file -- cgit v1.2.3-70-g09d2