aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEinar Egilsson2007-06-02 00:24:35 +0000
committerEinar Egilsson2007-06-02 00:24:35 +0000
commitb0c6f7a2bff3d57f1c8d05a561dbd0bbca0228ca (patch)
treeb9d11f284141f8aacd80e917bcd05877c3aee6e1
parentf77a93fe56b90c89d3a881d9af84d43e451efe8d (diff)
Redirector: All main functionality except Regex working.
git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@57 119bf307-c92d-0410-89bd-8f53e6181181
-rw-r--r--chrome/content/overlay.js56
-rw-r--r--chrome/content/overlay.xul9
-rw-r--r--chrome/content/redirect.js39
-rw-r--r--chrome/content/redirect.xul8
-rw-r--r--chrome/content/redirectList.js62
-rw-r--r--chrome/content/redirectList.xul28
-rw-r--r--chrome/content/redirector.js59
-rw-r--r--chrome/content/redirlib.js4
-rw-r--r--chrome/content/statusactive.PNGbin0 -> 360 bytes
-rw-r--r--chrome/content/statusinactive.PNGbin0 -> 396 bytes
-rw-r--r--chrome/locale/en-US/redirect.dtd4
-rw-r--r--chrome/locale/en-US/redirectList.dtd9
-rw-r--r--chrome/locale/en-US/redirector.dtd2
-rw-r--r--chrome/locale/en-US/redirector.properties6
-rw-r--r--defaults/preferences/redirector.js1
15 files changed, 236 insertions, 51 deletions
diff --git a/chrome/content/overlay.js b/chrome/content/overlay.js
index 9092938..a230d23 100644
--- a/chrome/content/overlay.js
+++ b/chrome/content/overlay.js
@@ -32,6 +32,9 @@ var RedirectorOverlay = {
}
this.strings = document.getElementById("redirector-strings");
+ this.prefObserver.register();
+ this.setStatusBarImg();
+
RedirLib.debug("Finished initialization");
this.initialized = true;
@@ -74,17 +77,17 @@ var RedirectorOverlay = {
onContextMenuCommand: function(event) {
- params = { inn : { url : window.content.location.href}, out : {} };
+ var item = { exampleUrl : window.content.location.href, pattern: window.content.location.href};
if (gContextMenu.onLink) {
- params.inn.redirect = gContextMenu.link.toString();
+ item.redirectUrl = gContextMenu.link.toString();
}
window.openDialog("chrome://redirector/content/redirect.xul",
"redirect",
- "chrome,dialog,modal,centerscreen", params);
+ "chrome,dialog,modal,centerscreen", item);
- if (params.out.pattern) {
- Redirector.addRedirect(params.out);
+ if (item.saved) {
+ Redirector.addRedirect(item);
}
},
@@ -96,6 +99,47 @@ var RedirectorOverlay = {
},
+ toggleEnabled : function(event) {
+ RedirLib.setBoolPref('enabled', !RedirLib.getBoolPref('enabled'));
+ },
+
+ setStatusBarImg : function() {
+ var statusImg = $('redirector-statusbar-img');
+
+ if (RedirLib.getBoolPref('enabled')) {
+ statusImg.src = 'chrome://redirector/content/statusactive.png'
+ statusImg.setAttribute('tooltiptext', this.strings.getString('enabledTooltip'));
+ } else {
+ statusImg.src = 'chrome://redirector/content/statusinactive.png'
+ statusImg.setAttribute('tooltiptext', this.strings.getString('disabledTooltip'));
+ }
+ },
+
+ 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);
+ },
+
+ observe : function(subject, topic, data) {
+ if (topic == 'nsPref:changed' && data == 'extensions.redirector.enabled') {
+ RedirectorOverlay.setStatusBarImg();
+ }
+ }
+
+ }
+
+
};
window.addEventListener("load", function(event) { RedirectorOverlay.onLoad(event); }, false);
-window.addEventListener("unload", function(event) { RedirectorOverlay.onUnload(event); }, false); \ No newline at end of file
+window.addEventListener("unload", function(event) { RedirectorOverlay.onUnload(event); }, false);
+
+
diff --git a/chrome/content/overlay.xul b/chrome/content/overlay.xul
index c65df70..d180d1f 100644
--- a/chrome/content/overlay.xul
+++ b/chrome/content/overlay.xul
@@ -24,4 +24,13 @@
insertafter="context-stop"
oncommand="RedirectorOverlay.onContextMenuCommand(event)"/>
</popup>
+ <statusbar id="status-bar">
+ <statusbarpanel id="redirector-status">
+ <image id="redirector-statusbar-img" src="chrome://redirector/content/statusactive.png"
+ tooltiptext="Redirector is enabled;"
+ style="width:16px; height:16px;"
+ onclick="RedirectorOverlay.toggleEnabled();" />
+ </statusbarpanel>
+ </statusbar>
+
</overlay> \ No newline at end of file
diff --git a/chrome/content/redirect.js b/chrome/content/redirect.js
index cfd2a2e..5be85f7 100644
--- a/chrome/content/redirect.js
+++ b/chrome/content/redirect.js
@@ -3,29 +3,40 @@
var Redirect = {
onLoad : function() {
- var params = window.arguments[0];
- $('txtExampleUrl').value = params.inn.url;
- $('txtPattern').value = params.inn.url;
- $('txtRedirectUrl').value = params.inn.redirect || '';
-
+ var item = window.arguments[0];
+ item.saved = false;
+ $('txtExampleUrl').value = item.exampleUrl;
+ $('txtPattern').value = item.pattern;
+ $('txtRedirectUrl').value = item.redirectUrl || '';
+ $('chkOnlyIfLinkExists').checked = item.onlyIfLinkExists || false;
+
+ if (item.patternType == kRedirectorRegex) {
+ $('rdoRegex').setAttribute('selected', true);
+ $('rdoWildcard').setAttribute('selected', false);
+ }
},
onAccept : function() {
- var params = window.arguments[0];
-
- params.out.pattern = $('txtPattern').value;
- params.out.patternType = kRedirectorWildcard;
- params.out.exampleUrl =$('txtExampleUrl').value;
- params.out.redirectUrl = $('txtRedirectUrl').value;
- params.out.onlyIfLinkExists = $('chkOnlyIfLinkExists').checked;
+ var item = window.arguments[0];
+
+ item.pattern = $('txtPattern').value;
+ if ($('rdoRegex').selected) {
+ item.patternType = kRedirectorRegex;
+ } else {
+ item.patternType = kRedirectorWildcard;
+ }
+ item.exampleUrl =$('txtExampleUrl').value;
+ item.redirectUrl = $('txtRedirectUrl').value;
+ item.onlyIfLinkExists = $('chkOnlyIfLinkExists').checked;
+ item.saved = true;
return true;
},
testPattern : function() {
- try {
+ var match;
+
alert(Redirector.wildcardMatch($('txtPattern').value, $('txtExampleUrl').value));
- } catch(e) {alert(e);}
}
}; \ No newline at end of file
diff --git a/chrome/content/redirect.xul b/chrome/content/redirect.xul
index 5d6ff2c..8db1891 100644
--- a/chrome/content/redirect.xul
+++ b/chrome/content/redirect.xul
@@ -32,8 +32,14 @@
<text value="&txtRedirectUrl.label;"/>
<textbox id="txtRedirectUrl"/>
</row>
+ <row>
+ <checkbox id="chkOnlyIfLinkExists" label="&chkOnlyIfLinkExists.label;"/>
+ <radiogroup>
+ <radio id="rdoWildcard" label="&rdoWildcard.label;" accesskey="&rdoWildcard.accessKey;" selected="true"/>
+ <radio id="rdoRegex" label="&rdoRegex.label;" accesskey="&rdoRegex.accessKey;"/>
+ </radiogroup>
+ </row>
</rows>
</grid>
- <checkbox id="chkOnlyIfLinkExists" label="&chkOnlyIfLinkExists.label;"/>
</dialog>
diff --git a/chrome/content/redirectList.js b/chrome/content/redirectList.js
index 7a273cd..081359f 100644
--- a/chrome/content/redirectList.js
+++ b/chrome/content/redirectList.js
@@ -2,6 +2,9 @@ var RedirectList = {
id : "redirector@einaregilsson.com",
name : "Redirector",
+ lstRedirects: null,
+ btnDelete : null,
+ btnEdit : null,
addItemsToListBox : function(items) {
@@ -11,10 +14,13 @@ var RedirectList = {
for each (item in items) {
row = document.createElement('listitem');
- this.createCell(row, item.exampleUrl);
this.createCell(row, item.pattern);
+ this.createCell(row, item.exampleUrl);
this.createCell(row, item.redirectUrl);
this.createCell(row, item.onlyIfLinkExists);
+ this.createCell(row, item.patternType);
+
+ row.item = item;
list.appendChild(row);
}
@@ -32,10 +38,64 @@ var RedirectList = {
try {
RedirLib.initialize(this);
Redirector.init();
+ this.lstRedirects = $('lstRedirects');
+ this.btnDelete = $('btnDelete');
+ this.btnEdit = $('btnEdit');
this.addItemsToListBox(Redirector.list);
} catch(e) {
alert(e);
}
+ },
+
+ close : function() {
+ window.close();
+ },
+
+ editRedirect : function() {
+
+ var listItem = this.lstRedirects.selectedItem;
+
+ if (!listItem) {
+ return;
+ }
+
+ var item = listItem.item;
+
+ window.openDialog("chrome://redirector/content/redirect.xul",
+ "redirect",
+ "chrome,dialog,modal,centerscreen", item);
+
+ if (item.saved) {
+ var map = [item.pattern, item.exampleUrl, item.redirectUrl, item.onlyIfLinkExists, item.patternType];
+
+ for (var i in map) {
+ listItem.childNodes[i].setAttribute('value', map[i]);
+ listItem.childNodes[i].setAttribute('label', map[i]);
+ }
+
+ Redirector.save();
+ }
+
+ },
+
+ deleteRedirect : function() {
+ var index = this.lstRedirects.selectedIndex;
+
+ if (index == -1) {
+ return;
+ }
+
+ try {
+ this.lstRedirects.removeItemAt(index);
+ Redirector.deleteAt(index);
+ } catch(e) {alert(e);}
+ },
+
+ selectionChange : function() {
+ var index = this.lstRedirects.selectedIndex;
+
+ this.btnEdit.disabled = (index == -1);
+ this.btnDelete.disabled = (index == -1);
}
};
diff --git a/chrome/content/redirectList.xul b/chrome/content/redirectList.xul
index 3c0b687..688457b 100644
--- a/chrome/content/redirectList.xul
+++ b/chrome/content/redirectList.xul
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<!DOCTYPE dialog SYSTEM "chrome://redirector/locale/redirectList.dtd">
-<dialog title="&window.title;"
+<window title="&window.title;"
orient="vertical"
autostretch="always"
onload="RedirectList.onLoad();"
buttons="accept"
- ondialogaccept="return RedirectList.onAccept();"
+ width="600px;"
+ height="400px;"
xmlns:nc="http://home.netscape.com/NC-rdf#"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
@@ -14,24 +15,31 @@
<script type="application/x-javascript" src="redirector.js"/>
<script type="application/x-javascript" src="redirectList.js"/>
- <dialogheader title="&header;" description="&header.description;"/>
-
- <listbox id="lstRedirects">
+ <vbox>
+ <listbox id="lstRedirects" height="300px" ondblclick="RedirectList.editRedirect();" onselect="RedirectList.selectionChange();">
<listhead>
<listheader label="&colPattern.label;"/>
+ <listheader label="&colExample.label;"/>
<listheader label="&colRedirectTo.label;"/>
<listheader label="&colOnlyIfLinkExits.label;"/>
<listheader label="&colPatternType.label;"/>
</listhead>
<listcols>
- <listcol flex="1"/>
- <listcol flex="1"/>
- <listcol flex="1"/>
- <listcol flex="1"/>
+ <listcol/>
+ <listcol/>
+ <listcol/>
+ <listcol/>
+ <listcol/>
</listcols>
</listbox>
-</dialog>
+ <hbox style="align:right;">
+ <button id="btnEdit" onclick="RedirectList.editRedirect();" label="&btnEdit.label;" disabled="true" />
+ <button id="btnDelete" onclick="RedirectList.deleteRedirect();" label="&btnDelete.label;" disabled="true" />
+ <button id="btnClose" onclick="RedirectList.close();" label="&btnClose.label;"/>
+ </hbox>
+ </vbox>
+</window>
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
+};
diff --git a/chrome/content/redirlib.js b/chrome/content/redirlib.js
index 71856dd..894c088 100644
--- a/chrome/content/redirlib.js
+++ b/chrome/content/redirlib.js
@@ -70,6 +70,10 @@ var RedirLib = {
return this._prefBranch.setCharPref(branch, value);
},
+ setBoolPref : function(branch, value) {
+ return this._prefBranch.setBoolPref(branch, value);
+ },
+
getBoolPref : function(branch) {
return this._prefBranch.getBoolPref(branch);
},
diff --git a/chrome/content/statusactive.PNG b/chrome/content/statusactive.PNG
new file mode 100644
index 0000000..06ce766
--- /dev/null
+++ b/chrome/content/statusactive.PNG
Binary files differ
diff --git a/chrome/content/statusinactive.PNG b/chrome/content/statusinactive.PNG
new file mode 100644
index 0000000..8b83562
--- /dev/null
+++ b/chrome/content/statusinactive.PNG
Binary files differ
diff --git a/chrome/locale/en-US/redirect.dtd b/chrome/locale/en-US/redirect.dtd
index 3014c52..d5ff86c 100644
--- a/chrome/locale/en-US/redirect.dtd
+++ b/chrome/locale/en-US/redirect.dtd
@@ -5,4 +5,8 @@
<!ENTITY txtRedirectUrl.label "Redirect to">
<!ENTITY chkOnlyIfLinkExists.label "Only if link exists">
<!ENTITY btnTestPattern.label "Test pattern">
+<!ENTITY rdoWildcard.label "Wildcard">
+<!ENTITY rdoWildcard.accessKey "W">
+<!ENTITY rdoRegex.label "Regular Expression">
+<!ENTITY rdoRegex.accessKey "R">
diff --git a/chrome/locale/en-US/redirectList.dtd b/chrome/locale/en-US/redirectList.dtd
index 7436cb7..9af7605 100644
--- a/chrome/locale/en-US/redirectList.dtd
+++ b/chrome/locale/en-US/redirectList.dtd
@@ -1,9 +1,10 @@
<!-- $Id$ -->
-<!ENTITY window.title "Redirects">
-<!ENTITY header "Pattern">
+<!ENTITY window.title "Redirector Settings">
<!ENTITY colPattern.label "Pattern">
+<!ENTITY colExample.label "Example URL">
<!ENTITY colRedirectTo.label "Redirect to">
<!ENTITY colOnlyIfLinkExits.label "Only if link exists">
<!ENTITY colPatternType.label "Pattern type">
-<!ENTITY header "Redirects">
-<!ENTITY header.description "All active redirects">
+<!ENTITY btnEdit.label "Edit...">
+<!ENTITY btnDelete.label "Delete">
+<!ENTITY btnClose.label "Close">
diff --git a/chrome/locale/en-US/redirector.dtd b/chrome/locale/en-US/redirector.dtd
index 3755f33..daec007 100644
--- a/chrome/locale/en-US/redirector.dtd
+++ b/chrome/locale/en-US/redirector.dtd
@@ -2,4 +2,4 @@
<!ENTITY RedirectorMenuItem.label "Redirector">
<!ENTITY RedirectorMenuItem.accesskey "R">
<!ENTITY RedirectorContext.label "Add url to Redirector...">
-<!ENTITY RedirectorContext.accesskey "A"> \ No newline at end of file
+<!ENTITY RedirectorContext.accesskey "A">
diff --git a/chrome/locale/en-US/redirector.properties b/chrome/locale/en-US/redirector.properties
index 5d01a1f..df70f6b 100644
--- a/chrome/locale/en-US/redirector.properties
+++ b/chrome/locale/en-US/redirector.properties
@@ -3,4 +3,8 @@ extensions.redirector@einaregilsson.com.description=Automatically redirects to u
extensionName=Redirector
addCurrentUrl=Add current url to Redirector
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
+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.
+enabledTooltip=Redirector is enabled
+disabledTooltip=Redirector is disabled
+testPatternSuccess=The pattern %S matches example URL %S, and would redirect you to %S
+testPatternFailure=The pattern %S does not match example URL %S \ No newline at end of file
diff --git a/defaults/preferences/redirector.js b/defaults/preferences/redirector.js
index 7b108aa..693c099 100644
--- a/defaults/preferences/redirector.js
+++ b/defaults/preferences/redirector.js
@@ -1,6 +1,7 @@
//// $Id$
pref("extensions.redirector.debug", false);
+pref("extensions.redirector.enabled", true);
pref("extensions.redirector.redirects", '[]');
// See http://kb.mozillazine.org/Localize_extension_descriptions