From 96966ca83f96ed1babcd2bd23aa68feb63fbb7a7 Mon Sep 17 00:00:00 2001 From: Einar Egilsson Date: Tue, 15 Sep 2009 21:41:06 +0000 Subject: Total restructuring of files. Unescape matches fully working Export of redirects working. git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@249 119bf307-c92d-0410-89bd-8f53e6181181 --- chrome/content/code/browserOverlay.xul.js | 137 ++++++++++++ chrome/content/code/editRedirect.xul.js | 86 +++++++ chrome/content/code/redirectList.xul.js | 193 ++++++++++++++++ chrome/content/code/redirector.prototype.js | 333 ++++++++++++++++++++++++++++ chrome/content/help.html | 173 --------------- chrome/content/images/redirector.png | Bin 0 -> 1462 bytes chrome/content/images/statusactive.PNG | Bin 0 -> 360 bytes chrome/content/images/statusinactive.PNG | Bin 0 -> 396 bytes chrome/content/overlay.js | 142 ------------ chrome/content/overlay.xul | 38 ---- chrome/content/redirect.js | 86 ------- chrome/content/redirect.xul | 53 ----- chrome/content/redirectList.js | 158 ------------- chrome/content/redirectList.xul | 54 ----- chrome/content/redirector.png | Bin 1462 -> 0 bytes chrome/content/redirector.prototype.js | 272 ----------------------- chrome/content/statusactive.PNG | Bin 360 -> 0 bytes chrome/content/statusinactive.PNG | Bin 396 -> 0 bytes chrome/content/ui/browserOverlay.xul | 38 ++++ chrome/content/ui/editRedirect.xul | 53 +++++ chrome/content/ui/help.html | 173 +++++++++++++++ chrome/content/ui/redirectList.xul | 54 +++++ chrome/content/unittest/run.html | 87 ++++++++ chrome/content/unittest/testcases.js | 39 ++++ chrome/content/unittests.html | 86 ------- chrome/content/unittests.js | 39 ---- chrome/locale/en-US/browserOverlay.xul.dtd | 9 + chrome/locale/en-US/editRedirect.xul.dtd | 14 ++ chrome/locale/en-US/redirect.dtd | 14 -- chrome/locale/en-US/redirectList.dtd | 12 - chrome/locale/en-US/redirectList.xul.dtd | 12 + chrome/locale/en-US/redirector.dtd | 9 - chrome/locale/en-US/redirector.properties | 5 +- 33 files changed, 1232 insertions(+), 1137 deletions(-) create mode 100644 chrome/content/code/browserOverlay.xul.js create mode 100644 chrome/content/code/editRedirect.xul.js create mode 100644 chrome/content/code/redirectList.xul.js create mode 100644 chrome/content/code/redirector.prototype.js delete mode 100644 chrome/content/help.html create mode 100644 chrome/content/images/redirector.png create mode 100644 chrome/content/images/statusactive.PNG create mode 100644 chrome/content/images/statusinactive.PNG delete mode 100644 chrome/content/overlay.js delete mode 100644 chrome/content/overlay.xul delete mode 100644 chrome/content/redirect.js delete mode 100644 chrome/content/redirect.xul delete mode 100644 chrome/content/redirectList.js delete mode 100644 chrome/content/redirectList.xul delete mode 100644 chrome/content/redirector.png delete mode 100644 chrome/content/redirector.prototype.js delete mode 100644 chrome/content/statusactive.PNG delete mode 100644 chrome/content/statusinactive.PNG create mode 100644 chrome/content/ui/browserOverlay.xul create mode 100644 chrome/content/ui/editRedirect.xul create mode 100644 chrome/content/ui/help.html create mode 100644 chrome/content/ui/redirectList.xul create mode 100644 chrome/content/unittest/run.html create mode 100644 chrome/content/unittest/testcases.js delete mode 100644 chrome/content/unittests.html delete mode 100644 chrome/content/unittests.js create mode 100644 chrome/locale/en-US/browserOverlay.xul.dtd create mode 100644 chrome/locale/en-US/editRedirect.xul.dtd delete mode 100644 chrome/locale/en-US/redirect.dtd delete mode 100644 chrome/locale/en-US/redirectList.dtd create mode 100644 chrome/locale/en-US/redirectList.xul.dtd delete mode 100644 chrome/locale/en-US/redirector.dtd (limited to 'chrome') diff --git a/chrome/content/code/browserOverlay.xul.js b/chrome/content/code/browserOverlay.xul.js new file mode 100644 index 0000000..636c02b --- /dev/null +++ b/chrome/content/code/browserOverlay.xul.js @@ -0,0 +1,137 @@ +//// $Id$ + +var Redirector = Components.classes["@einaregilsson.com/redirector;1"].getService(Components.interfaces.nsISupports).wrappedJSObject; + +var RedirectorOverlay = { + + id : "redirector@einaregilsson.com", + name : "Redirector", + initialized : false, + strings : null, + + onLoad : function(event) { + try { + + // initialization code + Redirector.debug("Initializing..."); + document.getElementById('contentAreaContextMenu') + .addEventListener("popupshowing", function(e) { RedirectorOverlay.showContextMenu(e); }, false); + + if (!Redirector.getBoolPref('showContextMenu')) { + document.getElementById('redirector-context').hidden = true; + } + if (!Redirector.getBoolPref('showStatusBarIcon')) { + document.getElementById('redirector-status').hidden = true; + } + this.strings = document.getElementById("redirector-strings"); + this.prefObserver.register(); + this.setStatusBarImg(); + + Redirector.debug("Finished initialization"); + this.initialized = true; + + } catch(e) { + if (this.strings) { + alert(this.strings.getFormattedString("initError", [this.name]) + "\n\n" + e); + } else { + alert(e); + } + } + }, + + onUnload : function(event) { + RedirectorOverlay.prefObserver.unregister(); + Redirector.debug("Finished cleanup"); + }, + + showContextMenu : function(event) { + if (gContextMenu.onLink) { + document.getElementById("redirector-context").label = this.strings.getString('addLinkUrl'); + } else { + document.getElementById("redirector-context").label = this.strings.getString('addCurrentUrl'); + } + }, + + onContextMenuCommand: function(event) { + + var item = { exampleUrl : window.content.location.href, pattern: window.content.location.href}; + if (gContextMenu.onLink) { + item.redirectUrl = gContextMenu.link.toString(); + } + + window.openDialog("chrome://redirector/content/ui/editRedirect.xul", + "redirect", + "chrome,dialog,modal,centerscreen", item); + + if (item.saved) { + Redirector.addRedirect(item); + } + }, + + onMenuItemCommand: function(event) { + this.openSettings(); + }, + + toggleEnabled : function(event) { + Redirector.setEnabled(!Redirector.enabled); + }, + + openSettings : function() { + var windowName = "redirectorSettings"; + var windowsMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator); + var win = windowsMediator.getMostRecentWindow(windowName); + if (win) { + win.focus(); + } else { + window.openDialog("chrome://redirector/content/ui/redirectList.xul", + windowName, + "chrome,dialog,resizable=no,centerscreen", this); + } + + }, + + statusBarClick : function(event) { + var LEFT = 0, RIGHT = 2; + + if (event.button == LEFT) { + RedirectorOverlay.toggleEnabled(); + } else if (event.button == RIGHT) { + this.openSettings(); + } + }, + + setStatusBarImg : function() { + var statusImg = document.getElementById('redirector-statusbar-img'); + + if (Redirector.enabled) { + statusImg.src = 'chrome://redirector/content/images/statusactive.PNG' + statusImg.setAttribute('tooltiptext', this.strings.getString('enabledTooltip')); + } else { + statusImg.src = 'chrome://redirector/content/images/statusinactive.PNG' + statusImg.setAttribute('tooltiptext', this.strings.getString('disabledTooltip')); + } + }, + + prefObserver : { + + getService : function() { + return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.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); diff --git a/chrome/content/code/editRedirect.xul.js b/chrome/content/code/editRedirect.xul.js new file mode 100644 index 0000000..6e981d2 --- /dev/null +++ b/chrome/content/code/editRedirect.xul.js @@ -0,0 +1,86 @@ +//// $Id$ + +const kRedirectorWildcard = 'W'; +const kRedirectorRegex= 'R'; +var Redirector = Components.classes["@einaregilsson.com/redirector;1"].getService(Components.interfaces.nsISupports).wrappedJSObject; + +function $(id) { + return document.getElementById(id); +} + +var EditRedirect = { + + onLoad : function() { + var item = window.arguments[0]; + item.saved = false; + $('txtExampleUrl').value = item.exampleUrl; + $('txtPattern').value = item.pattern; + $('txtRedirectUrl').value = item.redirectUrl || ''; + $('txtExcludePattern').value = item.excludePattern || ''; + $('chkUnescapeMatches').setAttribute('checked', !!item.unescapeMatches); + + $('txtPattern').focus(); + this.strings = document.getElementById("redirector-strings"); + + if (item.patternType == kRedirectorRegex) { + $('rdoRegex').setAttribute('selected', true); + $('rdoWildcard').setAttribute('selected', false); + } + }, + + onAccept : function() { + 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.excludePattern = $('txtExcludePattern').value; + item.unescapeMatches = $('chkUnescapeMatches').hasAttribute('checked') && $('chkUnescapeMatches').getAttribute('checked'); + item.saved = true; + + return true; + }, + + msgBox : function(title, text) { + Components.classes["@mozilla.org/embedcomp/prompt-service;1"] + .getService(Components.interfaces.nsIPromptService) + .alert(window, title, text); + }, + + testPattern : function() { + var redirectUrl, pattern, excludePattern, example, extName, isExcluded, unescapeMatches; + redirectUrl = $('txtRedirectUrl').value; + pattern = $('txtPattern').value; + excludePattern = $('txtExcludePattern').value; + example = $('txtExampleUrl').value; + unescapeMatches = $('chkUnescapeMatches').checked; + + extName = this.strings.getString('extensionName'); + + if ($('rdoRegex').selected) { + redirectUrl = Redirector.regexMatch(pattern, example, redirectUrl, unescapeMatches); + if (excludePattern) { + isExcluded = Redirector.regexMatch(excludePattern, example, 'exclude'); + } + } else { + redirectUrl = Redirector.wildcardMatch(pattern, example, redirectUrl, unescapeMatches); + if (excludePattern) { + isExcluded = Redirector.wildcardMatch(excludePattern, example, 'exclude'); + } + } + + var isRedirectMatch = redirectUrl || (redirectUrl === '' && $('txtRedirectUrl').value === ''); + if (isRedirectMatch && !isExcluded) { + this.msgBox(extName, this.strings.getFormattedString('testPatternSuccess', [pattern, example, redirectUrl])); + } else if (isExcluded) { + this.msgBox(extName, this.strings.getFormattedString('testPatternExclude', [example, excludePattern])); + } else { + this.msgBox(extName, this.strings.getFormattedString('testPatternFailure', [pattern, example])); + } + } +}; \ No newline at end of file diff --git a/chrome/content/code/redirectList.xul.js b/chrome/content/code/redirectList.xul.js new file mode 100644 index 0000000..65957b4 --- /dev/null +++ b/chrome/content/code/redirectList.xul.js @@ -0,0 +1,193 @@ +//// $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); + var win; + var iter = windowsMediator.getEnumerator(null); + while (iter.hasMoreElements()) { + win = iter.getNext(); + if (win.name == windowName) { + win.focus(); + return; + } + } + window.openDialog("chrome://redirector/content/ui/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', unescapeMatches : false}; + + window.openDialog("chrome://redirector/content/ui/editRedirect.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/ui/editRedirect.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); + }, + + importExport : function(mode, captionKey, func) { + //Mostly borrowed from Adblock Plus + var picker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker); + picker.init(window, Redirector.getString(captionKey), mode); + picker.defaultExtension = ".rdx"; + var dir = Redirector.getDefaultDir(); + if (dir) { + picker.displayDirectory = dir; + } + picker.appendFilter(Redirector.getString('redirectorFiles'), '*.rdx'); + + if (picker.show() != picker.returnCancel) + { + try + { + func(picker.file); + } + catch (e) + { + alert(e); + } + } + }, + + export : function() { + this.importExport(Ci.nsIFilePicker.modeSave, 'exportCaption', function(file) { + Redirector.exportRedirects(file); + }); + }, + + import : function() { + this.importExport(Ci.nsIFilePicker.modeOpen, 'importCaption', function(file) { + Redirector.importRedirects(file); + }); + } +}; diff --git a/chrome/content/code/redirector.prototype.js b/chrome/content/code/redirector.prototype.js new file mode 100644 index 0000000..24a41ba --- /dev/null +++ b/chrome/content/code/redirector.prototype.js @@ -0,0 +1,333 @@ +//// $Id$ + +Cc = Components.classes; +Ci = Components.interfaces; +Cr = Components.results; +kRedirectorWildcard = 'W'; +kRedirectorRegex= 'R'; +nsIContentPolicy = Ci.nsIContentPolicy; + + +Redirector.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] == 'true' //This might be undefined for those upgrading from 1.7.1 but that's ok + }); + } + } + + }, + + getDefaultDir : function() { + return this.prefBranch.getCharPref('defaultDir'); + }, + + setDefaultDir : function(dir) { + this.prefBranch.setCharPref('defaultDir', dir.spec); + }, + + 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; + }, + + // nsIContentPolicy interface implementation + 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/code/redirector.prototype.js'); + + for (var key in Redirector.prototype) { + this[key] = Redirector.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() { + this.prefBranch.setCharPref('redirects', this.redirectsAsStrings().join(':::')); + }, + + redirectsAsStrings : function() { + var r + , tempList = []; + + for each (r in this.list) { + this.debug(r.unescapeMatches); + tempList.push([r.exampleUrl, r.pattern, r.redirectUrl, r.patternType, r.excludePattern, r.unescapeMatches].join(',,,')); + } + return tempList; + }, + + 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; + + }, + + exportRedirects : function(file) { + var fileStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream); + const PR_WRONLY = 0x02; + const PR_CREATE_FILE = 0x08; + const PR_TRUNCATE = 0x20; + + fileStream.init(file, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0644, 0); + //file.parent.QueryInterface(Ci.nsILocalFile) + var stream = Cc["@mozilla.org/intl/converter-output-stream;1"].createInstance(Ci.nsIConverterOutputStream); + stream.init(fileStream, "UTF-8", 16384, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER); + stream.writeString(this.redirectsAsStrings().join('\n')); + stream.close(); + }, + + importRedirects : function(file) { + var fileStream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream); + fileStream.init(file, 0x01, 0444, 0); //TODO: Find the actual constants for these magic numbers + + var stream = Cc["@mozilla.org/intl/converter-input-stream;1"].createInstance(Ci.nsIConverterInputStream); + stream.init(fileStream, "UTF-8", 16384, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER); + stream = stream.QueryInterface(Ci.nsIUnicharLineInputStream); + + var importCount = 0, existsCount = 0; + var lines = []; + var line = {value: null}; + while (stream.readLine(line)) { + var parts = line.replace('\n', '').split(',,,'); + if (parts.length < 6) { + var redirect = { + exampleUrl : parts[0], + pattern : parts[1], + redirectUrl : parts[2], + patternType : parts[3], + excludePattern : parts[4], + unescapeMatches : parts[5] == 'true' ? true : false + }; + + + } + } + stream.close(); + this.save(); + }, + + getString : function(name) { + return this.strings.GetStringFromName(name); + }, + + 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) { + + if (!pattern || !text) { + return null; + } + if (pattern.indexOf('*') == -1) { + return (pattern == text) ? redirectUrl : null; + } + + var parts = pattern.split('*'); + var first = parts[0], + last = parts[parts.length-1]; + + if (first) { + if (text.substr(0, first.length) != first) { + return null; + } + text = text.substr(first.length); + } + + if (last) { + if (text.substr(text.length-last.length) != last) { + return null; + } + text = text.substr(0, text.length-last.length); + } + + if ((first || last) && parts.length == 2) { + return redirectUrl.replace('$1', text); + } + parts.splice(0,1); + parts.splice(parts.length-1,1); + var pos = 0, lastPos = 0; + var matches = []; + for each(part in parts) { + pos = text.indexOf(part, lastPos); + if (pos == -1) { + return null; + } + var match = text.substr(lastPos, pos-lastPos); + matches.push(match); + lastPos = pos + part.length; + } + matches.push(text.substr(lastPos)); + for (var i = 1; i <= matches.length; i++) { + redirectUrl = redirectUrl.replace(new RegExp('\\$' + i, 'gi'), unescapeMatches ? unescape(matches[i-1]) : matches[i-1]); + } + + return redirectUrl; + } +}; diff --git a/chrome/content/help.html b/chrome/content/help.html deleted file mode 100644 index 0ce6769..0000000 --- a/chrome/content/help.html +++ /dev/null @@ -1,173 +0,0 @@ - - - - Redirector Help - - - -

Redirector Help

-

Table of contents

- - - - -

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. - 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 \?).

- - -

Examples

- -
    -
  1. - Static redirect
    - Redirects from http://example.com/foo to http://example.com/bar -

    - Include pattern: http://example.com/foo
    - Exclude pattern:
    - Redirect to: http://example.com/bar
    - Pattern type: Wildcard
    -

    - -
  2. -
  3. - Redirect using query string parameter and wildcards
    - Redirects from http://example.com/index.php?id=12345&a=b to http://example.com/printerfriendly.php?id=12345&a=b - where 12345 could be any number. -

    - Include pattern: http://example.com/index.php?id=*&a=b
    - Exclude pattern:
    - Redirect to: http://example.com/printerfriendly.com?id=$1&a=b
    - Pattern type: Wildcard
    -

    -
  4. -
  5. - Redirect using query string parameter and regular expressions
    - Redirects from http://example.com/index.php?id=12345&a=b to http://example.com/printerfriendly.php?id=12345&a=b - where 12345 could be any number. -

    - Include pattern: http://example.com/index.php\?id=(\d+)&a=b
    - Exclude pattern:
    - Redirect to: http://example.com/printerfriendly.com?id=$1&a=b
    - Pattern type: Regular Expression
    -

    -
  6. -
  7. - Redirect to a different folder using wildcards
    - Redirects from http://example.com/category/fish/index.php to http://example.com/category/cats/index.php - where fish could be any word. The exclude pattern makes sure that there is only one - folder there, so for instance http://example.com/category/fish/cat/mouse/index.php would not match. -

    - Include pattern: http://example.com/category/*/index.php
    - Exclude pattern: http://example.com/category/*/*/index.php
    - Redirect to: http://example.com/category/cats/index.php
    - Pattern type: Wildcard
    -

    -
  8. -
  9. - Redirect http to https using wildcards
    - Redirects from http://mail.google.com/randomcharacters to https://mail.google.com/randomcharacters - where randomcharacters could be anything. -

    - Include pattern: http://mail.google.com*
    - Exclude pattern:
    - Redirect to: https://mail.google.com$1
    - Pattern type: Wildcard
    -

    -
  10. -
- - \ No newline at end of file diff --git a/chrome/content/images/redirector.png b/chrome/content/images/redirector.png new file mode 100644 index 0000000..f8de12c Binary files /dev/null and b/chrome/content/images/redirector.png differ diff --git a/chrome/content/images/statusactive.PNG b/chrome/content/images/statusactive.PNG new file mode 100644 index 0000000..06ce766 Binary files /dev/null and b/chrome/content/images/statusactive.PNG differ diff --git a/chrome/content/images/statusinactive.PNG b/chrome/content/images/statusinactive.PNG new file mode 100644 index 0000000..8b83562 Binary files /dev/null and b/chrome/content/images/statusinactive.PNG differ diff --git a/chrome/content/overlay.js b/chrome/content/overlay.js deleted file mode 100644 index 760f828..0000000 --- a/chrome/content/overlay.js +++ /dev/null @@ -1,142 +0,0 @@ -//// $Id$ - -var Redirector = Components.classes["@einaregilsson.com/redirector;1"].getService(Components.interfaces.nsISupports).wrappedJSObject; - -function $(id) { - return document.getElementById(id); -} - -var RedirectorOverlay = { - - id : "redirector@einaregilsson.com", - name : "Redirector", - initialized : false, - strings : null, - - onLoad : function(event) { - try { - - // initialization code - Redirector.debug("Initializing..."); - $('contentAreaContextMenu') - .addEventListener("popupshowing", function(e) { RedirectorOverlay.showContextMenu(e); }, false); - - if (!Redirector.getBoolPref('showContextMenu')) { - $('redirector-context').hidden = true; - } - if (!Redirector.getBoolPref('showStatusBarIcon')) { - $('redirector-status').hidden = true; - } - this.strings = document.getElementById("redirector-strings"); - this.prefObserver.register(); - this.setStatusBarImg(); - - Redirector.debug("Finished initialization"); - this.initialized = true; - - } catch(e) { - if (this.strings) { - alert(this.strings.getFormattedString("initError", [this.name]) + "\n\n" + e); - } else { - alert(e); - } - } - }, - - onUnload : function(event) { - RedirectorOverlay.prefObserver.unregister(); - Redirector.debug("Finished cleanup"); - }, - - showContextMenu : function(event) { - if (gContextMenu.onLink) { - $("redirector-context").label = this.strings.getString('addLinkUrl'); - } else { - $("redirector-context").label = this.strings.getString('addCurrentUrl'); - } - }, - - onContextMenuCommand: function(event) { - - var item = { exampleUrl : window.content.location.href, pattern: window.content.location.href}; - if (gContextMenu.onLink) { - item.redirectUrl = gContextMenu.link.toString(); - } - - window.openDialog("chrome://redirector/content/redirect.xul", - "redirect", - "chrome,dialog,modal,centerscreen", item); - - if (item.saved) { - Redirector.addRedirect(item); - } - }, - - onMenuItemCommand: function(event) { - this.openSettings(); - }, - - toggleEnabled : function(event) { - Redirector.setEnabled(!Redirector.enabled); - }, - - openSettings : function() { - var windowName = "redirectorSettings"; - var windowsMediator = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator); - var win = windowsMediator.getMostRecentWindow(windowName); - if (win) { - win.focus(); - } else { - window.openDialog("chrome://redirector/content/redirectList.xul", - windowName, - "chrome,dialog,resizable=no,centerscreen", this); - } - - }, - - statusBarClick : function(event) { - var LEFT = 0, RIGHT = 2; - - if (event.button == LEFT) { - RedirectorOverlay.toggleEnabled(); - } else if (event.button == RIGHT) { - this.openSettings(); - //$('redirector-status-popup').showPopup(); - } - }, - - setStatusBarImg : function() { - var statusImg = $('redirector-statusbar-img'); - - if (Redirector.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); diff --git a/chrome/content/overlay.xul b/chrome/content/overlay.xul deleted file mode 100644 index 9c4619e..0000000 --- a/chrome/content/overlay.xul +++ /dev/null @@ -1,38 +0,0 @@ - - - - - + + +

Redirector Unit Tests

+ + + + \ No newline at end of file diff --git a/chrome/content/unittest/testcases.js b/chrome/content/unittest/testcases.js new file mode 100644 index 0000000..ea6e26b --- /dev/null +++ b/chrome/content/unittest/testcases.js @@ -0,0 +1,39 @@ +//// $Id$ +var tests = { + "Wildcard matches" : { + run : function(data) { + var pattern = data[0], + url = data[1], + expected = data[2]; + var parts = expected.split(','); + var redirectUrl = ''; + if (!(parts.length == 1 && parts[0] == '')) { + for (var i in parts) { + redirectUrl += '$' + (parseFloat(i)+1) + ','; + } + redirectUrl = redirectUrl.substr(0, redirectUrl.length-1); + } + var result = redirector.wildcardMatch(pattern, url, redirectUrl, false); + return result == expected; + }, + + describe : function(data) { return data[0] + ' == ' + data[1] + ', matches=' + data[2]; }, + tests : [ + ['http://foo*', 'http://foobar.is', 'bar.is'], + ['http://foo*', 'http://foo', ''], + ['*://foo.is', 'http://foo.is', 'http'], + ['*http://foo.is', 'http://foo.is', ''], + ['http*foo*', 'http://foobar.is', '://,bar.is'], + ['http*foo*', 'http://foo', '://,'], + ['*://f*.is', 'http://foo.is', 'http,oo'], + ['*http://f*.is', 'http://foo.is', ',oo'], + ['*foo*', 'http://foo', 'http://,'], + ['*foo*', 'foobar.is', ',bar.is'], + ['*foo*', 'http://foobar.is', 'http://,bar.is'], + ['http://foo.is', 'http://foo.is', ''], + ['*', 'http://foo.is', 'http://foo.is'], + ['*://*oo*bar*', 'http://foo.is/bar/baz', 'http,f,.is/,/baz'], + ['*://**oo*bar*', 'http://foo.is/bar/baz', 'http,,f,.is/,/baz'], + ] + } +}; diff --git a/chrome/content/unittests.html b/chrome/content/unittests.html deleted file mode 100644 index 0989562..0000000 --- a/chrome/content/unittests.html +++ /dev/null @@ -1,86 +0,0 @@ - - - - Redirector Unit Tests - - - - -

Redirector Unit Tests

- - - - \ No newline at end of file diff --git a/chrome/content/unittests.js b/chrome/content/unittests.js deleted file mode 100644 index ea6e26b..0000000 --- a/chrome/content/unittests.js +++ /dev/null @@ -1,39 +0,0 @@ -//// $Id$ -var tests = { - "Wildcard matches" : { - run : function(data) { - var pattern = data[0], - url = data[1], - expected = data[2]; - var parts = expected.split(','); - var redirectUrl = ''; - if (!(parts.length == 1 && parts[0] == '')) { - for (var i in parts) { - redirectUrl += '$' + (parseFloat(i)+1) + ','; - } - redirectUrl = redirectUrl.substr(0, redirectUrl.length-1); - } - var result = redirector.wildcardMatch(pattern, url, redirectUrl, false); - return result == expected; - }, - - describe : function(data) { return data[0] + ' == ' + data[1] + ', matches=' + data[2]; }, - tests : [ - ['http://foo*', 'http://foobar.is', 'bar.is'], - ['http://foo*', 'http://foo', ''], - ['*://foo.is', 'http://foo.is', 'http'], - ['*http://foo.is', 'http://foo.is', ''], - ['http*foo*', 'http://foobar.is', '://,bar.is'], - ['http*foo*', 'http://foo', '://,'], - ['*://f*.is', 'http://foo.is', 'http,oo'], - ['*http://f*.is', 'http://foo.is', ',oo'], - ['*foo*', 'http://foo', 'http://,'], - ['*foo*', 'foobar.is', ',bar.is'], - ['*foo*', 'http://foobar.is', 'http://,bar.is'], - ['http://foo.is', 'http://foo.is', ''], - ['*', 'http://foo.is', 'http://foo.is'], - ['*://*oo*bar*', 'http://foo.is/bar/baz', 'http,f,.is/,/baz'], - ['*://**oo*bar*', 'http://foo.is/bar/baz', 'http,,f,.is/,/baz'], - ] - } -}; diff --git a/chrome/locale/en-US/browserOverlay.xul.dtd b/chrome/locale/en-US/browserOverlay.xul.dtd new file mode 100644 index 0000000..3aaa0fc --- /dev/null +++ b/chrome/locale/en-US/browserOverlay.xul.dtd @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/chrome/locale/en-US/editRedirect.xul.dtd b/chrome/locale/en-US/editRedirect.xul.dtd new file mode 100644 index 0000000..40098b8 --- /dev/null +++ b/chrome/locale/en-US/editRedirect.xul.dtd @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/chrome/locale/en-US/redirect.dtd b/chrome/locale/en-US/redirect.dtd deleted file mode 100644 index 40098b8..0000000 --- a/chrome/locale/en-US/redirect.dtd +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - diff --git a/chrome/locale/en-US/redirectList.dtd b/chrome/locale/en-US/redirectList.dtd deleted file mode 100644 index e09b038..0000000 --- a/chrome/locale/en-US/redirectList.dtd +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/chrome/locale/en-US/redirectList.xul.dtd b/chrome/locale/en-US/redirectList.xul.dtd new file mode 100644 index 0000000..e09b038 --- /dev/null +++ b/chrome/locale/en-US/redirectList.xul.dtd @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/chrome/locale/en-US/redirector.dtd b/chrome/locale/en-US/redirector.dtd deleted file mode 100644 index 3aaa0fc..0000000 --- a/chrome/locale/en-US/redirector.dtd +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/chrome/locale/en-US/redirector.properties b/chrome/locale/en-US/redirector.properties index f884575..0e10390 100644 --- a/chrome/locale/en-US/redirector.properties +++ b/chrome/locale/en-US/redirector.properties @@ -11,4 +11,7 @@ testPatternSuccess=The pattern %S matches example URL %S, and would redirect you testPatternFailure=The pattern %S does not match example URL %S testPatternExclude=Example URL %S matches the exclude pattern %S and so would not be redirected regexPatternError=The pattern '%S' is not a legal regular expression pattern. Details: %S -xpathDeprecated=XPath patterns are no longer supported as of version 1.5.1, please remove those redirects. \ No newline at end of file +xpathDeprecated=XPath patterns are no longer supported as of version 1.5.1, please remove those redirects. +redirectorFiles=Redirector files (*.rdx) +exportCaption=Export redirects... +importCaption=Import redirects... \ No newline at end of file -- cgit v1.2.3-70-g09d2