From 0ee5f981f3e708246674d26f8a26fc9b6d8f5fc9 Mon Sep 17 00:00:00 2001 From: Einar Egilsson Date: Tue, 1 Jan 2008 21:40:32 +0000 Subject: git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@101 119bf307-c92d-0410-89bd-8f53e6181181 --- chrome/content/help.html | 120 ++++++++++++++++++++++++++++++ chrome/content/overlay.js | 82 ++++++++++++++------ chrome/content/overlay.xul | 6 +- chrome/content/redirect.js | 18 ++++- chrome/content/redirect.xul | 12 ++- chrome/content/redirectList.js | 62 +++++++-------- chrome/content/redirectList.xul | 51 +++++++------ chrome/content/redirector.js | 75 +++++++++++++++++-- chrome/content/redirlib.js | 6 +- chrome/locale/en-US/redirect.dtd | 3 +- chrome/locale/en-US/redirectList.dtd | 7 +- chrome/locale/en-US/redirector.dtd | 4 + chrome/locale/en-US/redirector.properties | 1 + defaults/preferences/redirector.js | 2 + 14 files changed, 345 insertions(+), 104 deletions(-) create mode 100644 chrome/content/help.html diff --git a/chrome/content/help.html b/chrome/content/help.html new file mode 100644 index 0000000..a9f5245 --- /dev/null +++ b/chrome/content/help.html @@ -0,0 +1,120 @@ + + + Redirector Help + + + +

Redirector Help

+

Table of contents

+
    +
  1. What is Redirector?
  2. +
  3. Basic usage + +
  4. +
  5. Wildcards
  6. +
  7. Regular expressions
  8. +
  9. XPath redirects
  10. +
  11. Examples
  12. +
+ + + +

What is Redirector?

+ +

Redirector is an extension for Firefox that allows you to automatically redirect from + one webpage to another. For example, every time you visit http://abc.com you will automatically + load http://def.com instead. This can be useful for instance to always redirect articles to printer friendly + versions, redirect http:// to https:// for sites that support both, bypass advertising pages that appear before + being able to view certain pages and more.

+ + +

Basic usage

+

To add a new redirect you can go to the Tools menuitem and select Redirector. That will + open the Redirector settings window which shows all your redirects. The window can also be opened + by right clicking on the R icon in your statusbar and selecting Manage redirects. + There you can press the Add... button and then you can enter the details for the new redirect. A redirect + consists of a few things: +

+

+ + +

Wildcards

+ +

Wildcards are the simplest way to specify include and exclude patterns. When you create a wildcard pattern there + is just one special character, the asterisk *. An asterisk in your pattern will match zero or more characters and you can + have more than one star in your pattern. Some examples: +

+ $1, $2, $3 in the redirect urls will match the text that the stars matched. Examples: + +

+ + +

Regular expressions

+ +

Regular expressions allow for more complicated patterns but they are a lot harder to learn than wildcards. I'm not gonna + create a regex tutorial here but normal javascript regex syntax works, look at http://regular-expressions.info for + an introduction to regular expressions. $1,$2 etc. can be used in the redirect url and will be replaced with contents of captures in + the regular expressions. Captures are specified with parantheses. Example: http://example.com/index.asp\?id=(\d+) will match the url + http://example.com/index.asp?id=12345 and $1 will be replaced by 12345. (A common mistake in regex patterns is to forget to escape + the ? sign in the querystring of the url. ? is a special character in regular expressions so if you want to match an url with a querystring + you should escape it as \?).

+ + +

XPath redirects

+

The redirect url can be specified as an xpath expression by starting it with xpath: and then you will be redirected to the url + that the xpath expression matches. Example: Redirect url is xpath:/div/span/a/@href, then you will be redirected to the href value + of the first link that's inside a div in the original page.

+ + +

Examples

+ +

To be continued in next version...

+ + \ No newline at end of file diff --git a/chrome/content/overlay.js b/chrome/content/overlay.js index b7bd560..dd6f527 100644 --- a/chrome/content/overlay.js +++ b/chrome/content/overlay.js @@ -1,5 +1,6 @@ //// $Id$ + var RedirectorOverlay = { id : "redirector@einaregilsson.com", @@ -13,24 +14,19 @@ var RedirectorOverlay = { // initialization code RedirLib.initialize(this); RedirLib.debug("Initializing..."); - $('contentAreaContextMenu') .addEventListener("popupshowing", function(e) { RedirectorOverlay.showContextMenu(e); }, false); + if (!RedirLib.getBoolPref('showContextMenu')) { + $('redirector-context').hidden = true; + } + if (!RedirLib.getBoolPref('showStatusBarIcon')) { + $('redirector-status').hidden = true; + } Redirector.init(); - - var appcontent = window.document.getElementById('appcontent'); this.overrideOnStateChange(); - - if (appcontent && !appcontent.processed) { - appcontent.processed = true; - - appcontent.addEventListener('DOMContentLoaded', function(event) { - - RedirectorOverlay.onDOMContentLoaded(event); - - }, false); - } + this.overrideOpenNewWindowWith(); + this.overrideOpenNewTabWith(); this.strings = document.getElementById("redirector-strings"); Redirector.strings = this.strings; this.prefObserver.register(); @@ -53,11 +49,11 @@ var RedirectorOverlay = { var origOnStateChange = nsBrowserStatusHandler.prototype.onStateChange; nsBrowserStatusHandler.prototype.onStateChange = function(aWebProgress, aRequest, aStateFlags, aStatus) { - + if(aStateFlags & Ci.nsIWebProgressListener.STATE_START && aStateFlags| Ci.nsIWebProgressListener.STATE_IS_NETWORK && aStateFlags| Ci.nsIWebProgressListener.STATE_IS_REQUEST - && aRequest && aWebProgress.DOMWindow == content) { + && aRequest && aWebProgress.DOMWindow) { //If it's not a GET request we'll always do a slow redirect so the web will continue //to work in the way you'd expect @@ -99,17 +95,47 @@ var RedirectorOverlay = { }; }, + overrideOpenNewWindowWith: function() { + + window.__openNewWindowWith = window.openNewWindowWith; + window.openNewWindowWith = function (href, sourceURL, postData, allowThirdPartyFixup) { + var redirectUrl = Redirector.getRedirectUrlForInstantRedirect(href); + if (redirectUrl.url) { + __openNewWindowWith(redirectUrl.url, href, postData, allowThirdPartyFixup); + } else { + __openNewWindowWith(href, sourceURL, postData, allowThirdPartyFixup); + } + + }; + }, + + + overrideOpenNewTabWith: function() { + + window.__openNewTabWith = window.openNewTabWith; + window.openNewTabWith = function (href, sourceURL, postData, event, allowThirdPartyFixup) { + var redirectUrl = Redirector.getRedirectUrlForInstantRedirect(href); + if (redirectUrl.url) { + __openNewTabWith(redirectUrl.url, href, postData, event, allowThirdPartyFixup); + } else { + __openNewTabWith(href, sourceURL, postData, event, allowThirdPartyFixup); + } + + }; + }, + + onDOMContentLoaded : function(event) { var redirect, link, links, url; - - if (event.target != window.content.document) { + + if (event.target.toString().indexOf('HTMLDocument') == -1) { return; } - url = window.content.location.href; + url = event.target.location.href; RedirLib.debug('Processing url %1'._(url)); - Redirector.processUrl(url, window.content); + Redirector.processUrl(url, event.target); }, @@ -146,16 +172,25 @@ var RedirectorOverlay = { }, onMenuItemCommand: function(event) { - window.openDialog("chrome://redirector/content/redirectList.xul", - "redirectList", - "chrome,dialog,modal,centerscreen", this); - + Redirector.openSettings(); }, toggleEnabled : function(event) { RedirLib.setBoolPref('enabled', !RedirLib.getBoolPref('enabled')); }, + statusBarClick : function(event) { + var LEFT = 0, RIGHT = 2; + + alert($('redirector-status-popup').style.right); + if (event.button == LEFT) { + RedirectorOverlay.toggleEnabled(); + } else if (event.button == RIGHT) { + $('redirector-status-popup').left = $('redirector-status').left; + $('redirector-status-popup').showPopup(); + } + }, + setStatusBarImg : function() { var statusImg = $('redirector-statusbar-img'); @@ -195,4 +230,5 @@ var RedirectorOverlay = { }; window.addEventListener("load", function(event) { RedirectorOverlay.onLoad(event); }, false); +window.addEventListener("DOMContentLoaded", function(event) { RedirectorOverlay.onDOMContentLoaded(event); }, true); window.addEventListener("unload", function(event) { RedirectorOverlay.onUnload(event); }, false); \ No newline at end of file diff --git a/chrome/content/overlay.xul b/chrome/content/overlay.xul index e229530..3333be1 100644 --- a/chrome/content/overlay.xul +++ b/chrome/content/overlay.xul @@ -26,10 +26,14 @@ + + + + + onclick="RedirectorOverlay.statusBarClick(event);" /> diff --git a/chrome/content/redirect.js b/chrome/content/redirect.js index 08af778..be6cb96 100644 --- a/chrome/content/redirect.js +++ b/chrome/content/redirect.js @@ -8,9 +8,10 @@ var Redirect = { $('txtExampleUrl').value = item.exampleUrl; $('txtPattern').value = item.pattern; $('txtRedirectUrl').value = item.redirectUrl || ''; + $('txtExcludePattern').value = item.excludePattern || ''; $('chkOnlyIfLinkExists').checked = item.onlyIfLinkExists || false; - $('txtPattern').focus(); + $('txtPattern').focus(); this.strings = document.getElementById("redirector-strings"); if (item.patternType == kRedirectorRegex) { @@ -30,6 +31,7 @@ var Redirect = { } item.exampleUrl =$('txtExampleUrl').value; item.redirectUrl = $('txtRedirectUrl').value; + item.excludePattern = $('txtExcludePattern').value; item.onlyIfLinkExists = $('chkOnlyIfLinkExists').checked; item.saved = true; @@ -37,22 +39,32 @@ var Redirect = { }, testPattern : function() { - var redirectUrl, pattern, example, extName; + var redirectUrl, pattern, excludePattern, example, extName, isExcluded; redirectUrl = $('txtRedirectUrl').value; pattern = $('txtPattern').value; + excludePattern = $('txtExcludePattern').value; example = $('txtExampleUrl').value; extName = this.strings.getString('extensionName'); if ($('rdoRegex').selected) { redirectUrl = Redirector.regexMatch(pattern, example, redirectUrl); + if (excludePattern) { + isExcluded = Redirector.regexMatch(excludePattern, example, 'exclude'); + } } else { redirectUrl = Redirector.wildcardMatch(pattern, example, redirectUrl); + if (excludePattern) { + isExcluded = Redirector.wildcardMatch(excludePattern, example, 'exclude'); + } } - if (redirectUrl || (redirectUrl === '' && $('txtRedirectUrl').value === '')) { + var isRedirectMatch = redirectUrl || (redirectUrl === '' && $('txtRedirectUrl').value === ''); + if (isRedirectMatch && !isExcluded) { RedirLib.msgBox(extName, this.strings.getFormattedString('testPatternSuccess', [pattern, example, redirectUrl])); + } else if (isExcluded) { + RedirLib.msgBox(extName, this.strings.getFormattedString('testPatternExclude', [example, excludePattern])); } else { RedirLib.msgBox(extName, this.strings.getFormattedString('testPatternFailure', [pattern, example])); } diff --git a/chrome/content/redirect.xul b/chrome/content/redirect.xul index 68d23c4..83ed8df 100644 --- a/chrome/content/redirect.xul +++ b/chrome/content/redirect.xul @@ -28,20 +28,24 @@