From b0c6f7a2bff3d57f1c8d05a561dbd0bbca0228ca Mon Sep 17 00:00:00 2001
From: Einar Egilsson
Date: Sat, 2 Jun 2007 00:24:35 +0000
Subject: Redirector: All main functionality except Regex working.
git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@57 119bf307-c92d-0410-89bd-8f53e6181181
---
chrome/content/overlay.js | 56 ++++++++++++++++++++++++++++++----
chrome/content/overlay.xul | 9 ++++++
chrome/content/redirect.js | 39 +++++++++++++++---------
chrome/content/redirect.xul | 8 ++++-
chrome/content/redirectList.js | 62 +++++++++++++++++++++++++++++++++++++-
chrome/content/redirectList.xul | 28 +++++++++++------
chrome/content/redirector.js | 59 ++++++++++++++++++++++++++++--------
chrome/content/redirlib.js | 4 +++
chrome/content/statusactive.PNG | Bin 0 -> 360 bytes
chrome/content/statusinactive.PNG | Bin 0 -> 396 bytes
10 files changed, 220 insertions(+), 45 deletions(-)
create mode 100644 chrome/content/statusactive.PNG
create mode 100644 chrome/content/statusinactive.PNG
(limited to 'chrome/content')
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)"/>
+
+
+
+
+
+
\ 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 @@
+
+
+
+
+
+
+
-
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 @@
-
+
+
+
+
+
+
+
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
Binary files /dev/null and b/chrome/content/statusactive.PNG differ
diff --git a/chrome/content/statusinactive.PNG b/chrome/content/statusinactive.PNG
new file mode 100644
index 0000000..8b83562
Binary files /dev/null and b/chrome/content/statusinactive.PNG differ
--
cgit v1.2.3-70-g09d2