diff options
author | Einar Egilsson | 2007-05-28 22:35:59 +0000 |
---|---|---|
committer | Einar Egilsson | 2007-05-28 22:35:59 +0000 |
commit | ca4130f47bbe7f461dc1bc29d4aebb2dec305677 (patch) | |
tree | 4d5ddf0f7b26f15fc65140dfe94d389d10209618 /chrome/content | |
parent | a7da44a01a575246c9fb55665c15ce9a9a400814 (diff) |
git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@55 119bf307-c92d-0410-89bd-8f53e6181181
Diffstat (limited to 'chrome/content')
-rw-r--r-- | chrome/content/common.js | 37 | ||||
-rw-r--r-- | chrome/content/overlay.xul | 6 | ||||
-rw-r--r-- | chrome/content/redirect.js | 5 | ||||
-rw-r--r-- | chrome/content/redirect.xul | 2 | ||||
-rw-r--r-- | chrome/content/redirector.js | 163 |
5 files changed, 91 insertions, 122 deletions
diff --git a/chrome/content/common.js b/chrome/content/common.js deleted file mode 100644 index 7d13e4b..0000000 --- a/chrome/content/common.js +++ /dev/null @@ -1,37 +0,0 @@ - - -var RedirectorCommon = { - - wildcardMatch : function(pattern, text) { - var parts - , part - , i - , pos; - - parts = pattern.split('*'); - - for (i in parts) { - - part = parts[i]; - - pos = text.indexOf(part); - - if (pos == -1) { - return false; - } - - if (i == 0 && pos != 0) { - return false; - } - - if (i == parts.length -1 && i != "" && text.substr(text.length - part.length) != part) { - return false; - - } - - text = text.substr(pos + part.length); - } - - return true; - } -};
\ No newline at end of file diff --git a/chrome/content/overlay.xul b/chrome/content/overlay.xul index 7951516..c65df70 100644 --- a/chrome/content/overlay.xul +++ b/chrome/content/overlay.xul @@ -6,8 +6,8 @@ xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> <script src="redirlib.js"/> - <script src="common.js"/> <script src="redirector.js"/> + <script src="overlay.js"/> <stringbundleset id="stringbundleset"> <stringbundle id="redirector-strings" src="chrome://redirector/locale/redirector.properties"/> @@ -16,12 +16,12 @@ <menupopup id="menu_ToolsPopup"> <menuitem id="redirector-menuitem" label="&RedirectorMenuItem.label;" accesskey="&RedirectorMenuItem.accesskey;" - oncommand="Redirector.onMenuItemCommand(event);"/> + oncommand="RedirectorOverlay.onMenuItemCommand(event);"/> </menupopup> <popup id="contentAreaContextMenu"> <menuitem id="redirector-context" label="&RedirectorContext.label;" accesskey="&RedirectorContext.accesskey;" insertafter="context-stop" - oncommand="Redirector.onContextMenuCommand(event)"/> + oncommand="RedirectorOverlay.onContextMenuCommand(event)"/> </popup> </overlay>
\ No newline at end of file diff --git a/chrome/content/redirect.js b/chrome/content/redirect.js index bd1a686..cfd2a2e 100644 --- a/chrome/content/redirect.js +++ b/chrome/content/redirect.js @@ -14,7 +14,8 @@ var Redirect = { var params = window.arguments[0]; params.out.pattern = $('txtPattern').value; - params.out.patternType = 'Wildcard'; + params.out.patternType = kRedirectorWildcard; + params.out.exampleUrl =$('txtExampleUrl').value; params.out.redirectUrl = $('txtRedirectUrl').value; params.out.onlyIfLinkExists = $('chkOnlyIfLinkExists').checked; @@ -23,7 +24,7 @@ var Redirect = { testPattern : function() { try { - alert(RedirectorCommon.wildcardMatch($('txtPattern').value, $('txtExampleUrl').value)); + alert(Redirector.wildcardMatch($('txtPattern').value, $('txtExampleUrl').value)); } catch(e) {alert(e);} } diff --git a/chrome/content/redirect.xul b/chrome/content/redirect.xul index ea8ac02..5d6ff2c 100644 --- a/chrome/content/redirect.xul +++ b/chrome/content/redirect.xul @@ -10,8 +10,8 @@ xmlns:nc="http://home.netscape.com/NC-rdf#"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
- <script type="application/x-javascript" src="common.js"/>
<script type="application/x-javascript" src="redirlib.js"/>
+ <script type="application/x-javascript" src="redirector.js"/>
<script type="application/x-javascript" src="redirect.js"/>
<style type="text/css">
textbox { width:500px; }
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 |