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 ++++ 2 files changed, 45 insertions(+), 5 deletions(-) (limited to 'chrome/content') 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); }, -- cgit v1.2.3-70-g09d2