aboutsummaryrefslogtreecommitdiff
path: root/chrome
diff options
context:
space:
mode:
authorEinar Egilsson2009-09-14 21:19:32 +0000
committerEinar Egilsson2009-09-14 21:19:32 +0000
commit0225b1c8035a1c03dafa4c971d21facc76dbc2de (patch)
tree6fdf0bef8e5254c03040824f190917ad00cee69a /chrome
parent10e637b427e5cdd9b5b6660469391cac525da637 (diff)
Moved component implementation into chrome folder
git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@246 119bf307-c92d-0410-89bd-8f53e6181181
Diffstat (limited to 'chrome')
-rw-r--r--chrome/content/redirectList.js288
-rw-r--r--chrome/content/redirectList.xul2
-rw-r--r--chrome/content/redirector.prototype.js273
-rw-r--r--chrome/locale/en-US/redirectList.dtd2
4 files changed, 421 insertions, 144 deletions
diff --git a/chrome/content/redirectList.js b/chrome/content/redirectList.js
index 7583f5f..b3a3200 100644
--- a/chrome/content/redirectList.js
+++ b/chrome/content/redirectList.js
@@ -1,53 +1,53 @@
-//// $Id$
-
-var Redirector = Components.classes["@einaregilsson.com/redirector;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
-const Cc = Components.classes;
-const Ci = Components.interfaces;
-
-function $(id) {
- return document.getElementById(id);
-}
-
-var RedirectList = {
-
- id : "redirector@einaregilsson.com",
- name : "Redirector",
- lstRedirects: null,
- btnDelete : null,
- btnEdit : null,
-
- addItemsToListBox : function(items) {
-
- var list = $('lstRedirects');
- var item, row, value, newItem;
-
- for each (item in items) {
- newItem = this.template.cloneNode(true);
-
- newItem.getElementsByAttribute('name', 'dscrIncludePattern')[0].setAttribute('value', item.pattern);
- newItem.getElementsByAttribute('name', 'dscrExcludePattern')[0].setAttribute('value', item.excludePattern);
- newItem.getElementsByAttribute('name', 'dscrRedirectTo')[0].setAttribute('value', item.redirectUrl);
- newItem.item = item;
- list.appendChild(newItem);
- newItem.setAttribute("selected", false);
- }
-
- },
-
- onLoad : function() {
- try {
- this.lstRedirects = $('lstRedirects');
- this.lstRedirects.selType = 'single';
- this.template = document.getElementsByTagName('richlistitem')[0];
- this.lstRedirects.removeChild(this.template);
- this.btnDelete = $('btnDelete');
- this.btnEdit = $('btnEdit');
- this.addItemsToListBox(Redirector.list);
- } catch(e) {
- alert(e);
- }
- },
-
+//// $Id$
+
+var Redirector = Components.classes["@einaregilsson.com/redirector;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+
+function $(id) {
+ return document.getElementById(id);
+}
+
+var RedirectList = {
+
+ id : "redirector@einaregilsson.com",
+ name : "Redirector",
+ lstRedirects: null,
+ btnDelete : null,
+ btnEdit : null,
+
+ addItemsToListBox : function(items) {
+
+ var list = $('lstRedirects');
+ var item, row, value, newItem;
+
+ for each (item in items) {
+ newItem = this.template.cloneNode(true);
+
+ newItem.getElementsByAttribute('name', 'dscrIncludePattern')[0].setAttribute('value', item.pattern);
+ newItem.getElementsByAttribute('name', 'dscrExcludePattern')[0].setAttribute('value', item.excludePattern);
+ newItem.getElementsByAttribute('name', 'dscrRedirectTo')[0].setAttribute('value', item.redirectUrl);
+ newItem.item = item;
+ list.appendChild(newItem);
+ newItem.setAttribute("selected", false);
+ }
+
+ },
+
+ onLoad : function() {
+ try {
+ this.lstRedirects = $('lstRedirects');
+ this.lstRedirects.selType = 'single';
+ this.template = document.getElementsByTagName('richlistitem')[0];
+ this.lstRedirects.removeChild(this.template);
+ this.btnDelete = $('btnDelete');
+ this.btnEdit = $('btnEdit');
+ this.addItemsToListBox(Redirector.list);
+ } catch(e) {
+ alert(e);
+ }
+ },
+
openHelp : function() {
var windowName = "redirectorHelp";
var windowsMediator = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
@@ -62,97 +62,97 @@ var RedirectList = {
}
window.openDialog("chrome://redirector/content/help.html", windowName, "chrome,dialog,resizable=yes,location=0,toolbar=0,status=0,width=800px,height=600px,centerscreen", this);
},
-
- close : function() {
- window.close();
- },
-
- moveUp : function(){
- if (this.lstRedirects.selectedIndex <= 0) {
- return;
- }
- this.switchItems(this.lstRedirects.selectedIndex-1);
- },
-
- moveDown : function() {
- if (this.lstRedirects.selectedIndex == Redirector.list.length-1) {
- return;
- }
- this.switchItems(this.lstRedirects.selectedIndex);
- },
-
- switchItems : function(firstIndex) {
- var first = Redirector.list[firstIndex];
- var second = Redirector.list[firstIndex+1];
- Redirector.list[firstIndex] = second;
- Redirector.list[firstIndex+1] = first;
- this.setListItemValues(this.lstRedirects.children[firstIndex+1], first);
- this.setListItemValues(this.lstRedirects.children[firstIndex], second);
- this.lstRedirects.selectedIndex -= 1;
- Redirector.save();
- },
-
- setListItemValues : function(listItem, item){
- listItem.getElementsByAttribute('name', 'dscrIncludePattern')[0].setAttribute('value', item.pattern);
- listItem.getElementsByAttribute('name', 'dscrExcludePattern')[0].setAttribute('value', item.excludePattern);
- listItem.getElementsByAttribute('name', 'dscrRedirectTo')[0].setAttribute('value', item.redirectUrl);
- },
-
- addRedirect : function() {
-
- var item = { pattern : '', exampleUrl : '', redirectUrl : '', patternType : 'W'};
-
- window.openDialog("chrome://redirector/content/redirect.xul",
- "redirect",
- "chrome,dialog,modal,centerscreen", item);
-
- if (item.saved) {
- this.addItemsToListBox([item]);
- Redirector.addRedirect(item);
- }
-
- },
-
- 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) {
- this.setListItemValues(listItem, item);
- Redirector.save();
- }
- },
-
- deleteRedirect : function() {
- var index = this.lstRedirects.selectedIndex;
-
- if (index == -1) {
- return;
- }
-
- try {
- this.lstRedirects.removeChild(this.lstRedirects.children[index]);
- Redirector.deleteAt(index);
- } catch(e) {
- alert(e);
- }
- },
-
- selectionChange : function() {
- var index = $('lstRedirects').selectedIndex;
-
- $('btnEdit').disabled = (index == -1);
- $('btnDelete').disabled = (index == -1);
- }
-
-};
+
+ close : function() {
+ window.close();
+ },
+
+ moveUp : function(){
+ if (this.lstRedirects.selectedIndex <= 0) {
+ return;
+ }
+ this.switchItems(this.lstRedirects.selectedIndex-1);
+ },
+
+ moveDown : function() {
+ if (this.lstRedirects.selectedIndex == Redirector.list.length-1) {
+ return;
+ }
+ this.switchItems(this.lstRedirects.selectedIndex);
+ },
+
+ switchItems : function(firstIndex) {
+ var first = Redirector.list[firstIndex];
+ var second = Redirector.list[firstIndex+1];
+ Redirector.list[firstIndex] = second;
+ Redirector.list[firstIndex+1] = first;
+ this.setListItemValues(this.lstRedirects.children[firstIndex+1], first);
+ this.setListItemValues(this.lstRedirects.children[firstIndex], second);
+ this.lstRedirects.selectedIndex -= 1;
+ Redirector.save();
+ },
+
+ setListItemValues : function(listItem, item){
+ listItem.getElementsByAttribute('name', 'dscrIncludePattern')[0].setAttribute('value', item.pattern);
+ listItem.getElementsByAttribute('name', 'dscrExcludePattern')[0].setAttribute('value', item.excludePattern);
+ listItem.getElementsByAttribute('name', 'dscrRedirectTo')[0].setAttribute('value', item.redirectUrl);
+ },
+
+ addRedirect : function() {
+
+ var item = { pattern : '', exampleUrl : '', redirectUrl : '', patternType : 'W'};
+
+ window.openDialog("chrome://redirector/content/redirect.xul",
+ "redirect",
+ "chrome,dialog,modal,centerscreen", item);
+
+ if (item.saved) {
+ this.addItemsToListBox([item]);
+ Redirector.addRedirect(item);
+ }
+
+ },
+
+ 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) {
+ this.setListItemValues(listItem, item);
+ Redirector.save();
+ }
+ },
+
+ deleteRedirect : function() {
+ var index = this.lstRedirects.selectedIndex;
+
+ if (index == -1) {
+ return;
+ }
+
+ try {
+ this.lstRedirects.removeChild(this.lstRedirects.children[index]);
+ Redirector.deleteAt(index);
+ } catch(e) {
+ alert(e);
+ }
+ },
+
+ selectionChange : function() {
+ var index = $('lstRedirects').selectedIndex;
+
+ $('btnEdit').disabled = (index == -1);
+ $('btnDelete').disabled = (index == -1);
+ }
+
+};
diff --git a/chrome/content/redirectList.xul b/chrome/content/redirectList.xul
index f43a0b1..77bbf9b 100644
--- a/chrome/content/redirectList.xul
+++ b/chrome/content/redirectList.xul
@@ -43,6 +43,8 @@
<button id="btnDelete" onclick="RedirectList.deleteRedirect();" label="&btnDelete.label;" disabled="true" />
<button id="btnClose" onclick="RedirectList.close();" label="&btnClose.label;"/>
<button id="btnHelp" onclick="RedirectList.openHelp();" label="&btnHelp.label;"/>
+ <button id="btnImport" onclick="RedirectList.import();" label="&btnImport.label;"/>
+ <button id="btnExport" onclick="RedirectList.export();" label="&btnExport.label;"/>
<!-- TODO: include in v2.0
<button id="btnUp" onclick="RedirectList.moveUp();" label="UP" disabled="false" />
<button id="btnDown" onclick="RedirectList.moveDown();" label="Down" />
diff --git a/chrome/content/redirector.prototype.js b/chrome/content/redirector.prototype.js
new file mode 100644
index 0000000..6d8d3d2
--- /dev/null
+++ b/chrome/content/redirector.prototype.js
@@ -0,0 +1,273 @@
+//// $Id$
+
+Cc = Components.classes;
+Ci = Components.interfaces;
+Cr = Components.results;
+kRedirectorWildcard = 'W';
+kRedirectorRegex= 'R';
+nsIContentPolicy = Ci.nsIContentPolicy;
+
+
+RedirectorPolicy.prototype = {
+ prefBranch : null,
+ list : null,
+ strings : null,
+ cout : Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService),
+
+ init : function() {
+ this.prefBranch = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch("extensions.redirector.");
+
+ //Check if we need to update existing redirects
+
+ var data = this.prefBranch.getCharPref('redirects');
+ var version = this.prefBranch.getCharPref('version');
+ this.debugEnabled = this.prefBranch.getBoolPref('debug');
+ this.enabled = this.prefBranch.getBoolPref('enabled');
+ this.loadStrings();
+ //Here update checks are handled
+ if (version == 'undefined') { //Either a fresh install of Redirector, or first time install of v2.0
+ if (data) { //There is some data in redirects, we are upgrading from a previous version, need to upgrade data
+ var tempList = eval(data);
+ var arr;
+ var newArr = []
+ for each (arr in tempList) {
+ if (arr.length == 5) {
+ arr.push(''); //For those that don't have an exclude pattern. Backwards compatibility is a bitch!
+ }
+ arr.splice(3,1); //Remove the "only if link exists" data
+ newArr.push(arr.join(',,,'));
+ }
+ this.prefBranch.setCharPref('redirects', newArr.join(':::'));
+ }
+ this.prefBranch.setCharPref('version', '2.0');
+ }
+ //Update finished
+
+ //Now get from the new format
+ data = this.prefBranch.getCharPref('redirects');
+ var arr;
+ this.list = [];
+ if (data != '') {
+ for each (redirectString in data.split(':::')) {
+ arr = redirectString.split(',,,');
+ this.list.push({
+ exampleUrl : arr[0],
+ pattern : arr[1],
+ redirectUrl : arr[2],
+ patternType : arr[3],
+ excludePattern : arr[4],
+ unescapeMatches : !!arr[5] //This might be undefined for those upgrading from 1.7.1 but that's ok
+ });
+ }
+ }
+
+ },
+
+ loadStrings : function() {
+ var src = 'chrome://redirector/locale/redirector.properties';
+ var localeService = Cc["@mozilla.org/intl/nslocaleservice;1"].getService(Ci.nsILocaleService);
+ var appLocale = localeService.getApplicationLocale();
+ var stringBundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
+ this.strings = stringBundleService.createBundle(src, appLocale);
+ },
+
+ debug : function(msg) {
+ if (this.debugEnabled) {
+ this.cout.logStringMessage('REDIRECTOR: ' + msg);
+ }
+ },
+
+ // nsIContentPolicy interface implementation
+ shouldLoad: function(contentType, contentLocation, requestOrigin, aContext, mimeTypeGuess, extra) {
+ if (!this.enabled) {
+ return nsIContentPolicy.ACCEPT;
+ }
+ if (contentLocation.scheme != "http" && contentLocation.scheme != "https") {
+ return nsIContentPolicy.ACCEPT;
+ }
+
+ if (contentType != nsIContentPolicy.TYPE_DOCUMENT) {
+ return nsIContentPolicy.ACCEPT;
+ }
+
+ if (!aContext || !aContext.loadURI) {
+ return nsIContentPolicy.ACCEPT;
+ }
+ this.debug("CHECK: " + contentLocation.spec);
+
+ var url = contentLocation.spec;
+
+ for each (var redirect in this.list) {
+ var redirectUrl = this.getRedirectUrl(url, redirect);
+ if (redirectUrl) {
+ redirectUrl = this.makeAbsoluteUrl(url, redirectUrl);
+ this.debug('Redirecting ' + url + ' to ' + redirectUrl);
+ aContext.loadURI(redirectUrl, requestOrigin, null);
+ return nsIContentPolicy.REJECT_REQUEST;
+ }
+ }
+ return nsIContentPolicy.ACCEPT;
+ },
+
+ shouldProcess: function(contentType, contentLocation, requestOrigin, insecNode, mimeType, extra) {
+ return nsIContentPolicy.ACCEPT;
+ },
+
+ setEnabled : function(val) {
+ this.enabled = val;
+ this.prefBranch.setBoolPref('enabled', val);
+ },
+
+ reload : function() {
+ Cc["@mozilla.org/moz/jssubscript-loader;1"]
+ .getService(Ci.mozIJSSubScriptLoader)
+ .loadSubScript('chrome://redirector/content/redirector.prototype.js');
+
+ for (var key in RedirectorPolicy.prototype) {
+ this[key] = RedirectorPolicy.prototype[key];
+ }
+ this.init();
+ },
+
+ addRedirect : function(redirect) {
+ this.list.push(redirect);
+ this.save();
+ },
+
+ deleteAt : function(index) {
+ this.list.splice(index, 1);
+ this.save();
+ },
+
+ save : function() {
+ var r
+ , tempList = [];
+
+ for each (r in this.list) {
+ tempList.push([r.exampleUrl, r.pattern, r.redirectUrl, r.patternType, r.excludePattern, r.unescapeMatches].join(',,,'));
+ }
+ this.prefBranch.setCharPref('redirects', tempList.join(':::'));
+ },
+
+ getBoolPref : function(name) {
+ return this.prefBranch.getBoolPref(name);
+ },
+
+ regexMatch : function(pattern, text, redirectUrl, unescapeMatches) {
+
+ if (!pattern) {
+ return null;
+ }
+ var strings, rx, match;
+ try {
+ rx = new RegExp(pattern, 'gi');
+ match = rx.exec(text);
+ } catch(e) {
+ this.msgBox(this.strings.GetStringFromName('extensionName'), this.strings.formatStringFromName('regexPatternError', [pattern, e.toString()],2));
+ return null;
+ }
+
+ var rxrepl;
+
+ if (match) {
+ for (var i = 1; i < match.length; i++) {
+ rxrepl = new RegExp('\\$' + i, 'gi');
+ redirectUrl = redirectUrl.replace(rxrepl, unescapeMatches ? unescape(match[i]) : match[i]);
+ }
+ return redirectUrl;
+ }
+
+ return null;
+
+ },
+
+ msgBox : function(title, text) {
+ Cc["@mozilla.org/embedcomp/prompt-service;1"]
+ .getService(Ci.nsIPromptService)
+ .alert(null, title, text);
+ },
+
+ getRedirectUrl: function(url, redirect) {
+
+ if (redirect.patternType == kRedirectorWildcard) {
+ if (this.wildcardMatch(redirect.excludePattern, url, 'whatever')) {
+ this.debug(url + ' matches exclude pattern ' + redirect.excludePattern);
+ return null;
+ }
+ return this.wildcardMatch(redirect.pattern, url, redirect.redirectUrl, redirect.unescapeMatches);
+ } else if (redirect.patternType == kRedirectorRegex) {
+ if (this.regexMatch(redirect.excludePattern, url, 'whatever')) {
+ this.debug(url + ' matches exclude pattern ' + redirect.excludePattern);
+ return null;
+ }
+ return this.regexMatch(redirect.pattern, url, redirect.redirectUrl, redirect.unescapeMatches);
+ }
+ return null;
+ },
+
+ makeAbsoluteUrl : function(currentUrl, relativeUrl) {
+
+ if (relativeUrl.match(/https?:/)) {
+ return relativeUrl;
+ }
+
+ var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
+ //this.debug(currentUrl);
+ var uri = ioService.newURI(currentUrl, null, null);
+
+ return uri.resolve(relativeUrl);
+ },
+
+ wildcardMatch : function(pattern, text, redirectUrl, unescapeMatches) {
+ var parts
+ , part
+ , i
+ , pos
+ , originalText
+ , stars;
+
+ if (!pattern) {
+ return null;
+ }
+ parts = pattern.split('*');
+
+ stars = [];
+ originalText = text;
+ var starStart = -1;
+
+ for (i in parts) {
+
+ part = parts[i];
+
+ pos = text.lastIndexOf(part);
+
+ if (pos == -1) {
+ return null;
+ }
+
+ if (i == 0 && pos != 0) {
+ return null;
+ }
+
+ if (i == parts.length -1 && i != "" && text.substr(text.length - part.length) != part) {
+ return null;
+ }
+
+ if (i == 0) {
+ //Do nothing, part will be added on next run
+ } else if (i == parts.length-1 && parts[i] == '') {
+ stars.push(unescapeMatches ? unescape(text) : text);
+ } else {
+ stars.push(unescapeMatches ? unescape(text.substr(0, pos)) : text.substr(0, pos));
+ }
+
+ text = text.substr(pos + part.length);
+ }
+
+ for (var i = 1; i <= stars.length; i++) {
+ redirectUrl = redirectUrl.replace(new RegExp('\\$' + i, 'gi'), stars[i-1]);
+ }
+
+ return redirectUrl;
+ }
+};
diff --git a/chrome/locale/en-US/redirectList.dtd b/chrome/locale/en-US/redirectList.dtd
index a4475a6..e09b038 100644
--- a/chrome/locale/en-US/redirectList.dtd
+++ b/chrome/locale/en-US/redirectList.dtd
@@ -8,3 +8,5 @@
<!ENTITY btnDelete.label "Delete">
<!ENTITY btnClose.label "Close">
<!ENTITY btnHelp.label "Help">
+<!ENTITY btnImport.label "Import...">
+<!ENTITY btnExport.label "Export...">