diff options
| author | Einar Egilsson <einar@einaregilsson.com> | 2009-09-14 21:19:32 +0000 | 
|---|---|---|
| committer | Einar Egilsson <einar@einaregilsson.com> | 2009-09-14 21:19:32 +0000 | 
| commit | 0225b1c8035a1c03dafa4c971d21facc76dbc2de (patch) | |
| tree | 6fdf0bef8e5254c03040824f190917ad00cee69a | |
| parent | 10e637b427e5cdd9b5b6660469391cac525da637 (diff) | |
Moved component implementation into chrome folder
git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@246 119bf307-c92d-0410-89bd-8f53e6181181
| -rw-r--r-- | Redirector.csproj | 45 | ||||
| -rw-r--r-- | Redirector.sln | 17 | ||||
| -rw-r--r-- | chrome/content/redirectList.js | 288 | ||||
| -rw-r--r-- | chrome/content/redirectList.xul | 2 | ||||
| -rw-r--r-- | chrome/content/redirector.prototype.js | 273 | ||||
| -rw-r--r-- | chrome/locale/en-US/redirectList.dtd | 2 | ||||
| -rw-r--r-- | components/redirector.js | 272 | ||||
| -rw-r--r-- | install.rdf | 4 | 
8 files changed, 437 insertions, 466 deletions
| diff --git a/Redirector.csproj b/Redirector.csproj deleted file mode 100644 index 558bb46..0000000 --- a/Redirector.csproj +++ /dev/null @@ -1,45 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?>
 -<!-- $Id$ -->
 -<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 -  <PropertyGroup>
 -    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
 -    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
 -    <ProductVersion>9.0.21022</ProductVersion>
 -    <SchemaVersion>2.0</SchemaVersion>
 -    <ProjectGuid>{1C962CCA-74DC-4F72-8C83-7617F2275D1A}</ProjectGuid>
 -    <OutputType>Exe</OutputType>
 -    <AppDesignerFolder>Properties</AppDesignerFolder>
 -    <RootNamespace>Redirector</RootNamespace>
 -    <AssemblyName>Redirector</AssemblyName>
 -    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
 -    <FileAlignment>512</FileAlignment>
 -  </PropertyGroup>
 -  <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
 -    <UseVSHostingProcess>false</UseVSHostingProcess>
 -  </PropertyGroup>
 -  <ItemGroup>
 -    <Content Include="chrome\content\help.html" />
 -    <Content Include="chrome\content\overlay.js" />
 -    <Content Include="chrome\content\redirect.js" />
 -    <Content Include="chrome\content\redirectList.js" />
 -    <Content Include="chrome\content\redirector.png" />
 -    <Content Include="chrome\content\statusactive.PNG" />
 -    <Content Include="chrome\content\statusinactive.PNG" />
 -    <Content Include="components\redirector.js" />
 -    <Content Include="defaults\preferences\redirector.js" />
 -  </ItemGroup>
 -  <ItemGroup>
 -    <None Include="chrome.manifest" />
 -    <None Include="chrome\content\overlay.xul" />
 -    <None Include="chrome\content\redirect.xul" />
 -    <None Include="chrome\content\redirectList.xul" />
 -    <None Include="chrome\locale\en-US\redirect.dtd" />
 -    <None Include="chrome\locale\en-US\redirectList.dtd" />
 -    <None Include="chrome\locale\en-US\redirector.dtd" />
 -    <None Include="chrome\locale\en-US\redirector.properties" />
 -    <None Include="install.rdf" />
 -  </ItemGroup>
 -  <Target Name="Build" />
 -  <Target Name="Rebuild" />
 -  <Target Name="Clean" />
 -</Project>
\ No newline at end of file diff --git a/Redirector.sln b/Redirector.sln deleted file mode 100644 index 22a7088..0000000 --- a/Redirector.sln +++ /dev/null @@ -1,17 +0,0 @@ -
 -Microsoft Visual Studio Solution File, Format Version 10.00
 -# Visual Studio 2008
 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Redirector", "Redirector.csproj", "{1C962CCA-74DC-4F72-8C83-7617F2275D1A}"
 -EndProject
 -Global
 -	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 -		Debug|Any CPU = Debug|Any CPU
 -	EndGlobalSection
 -	GlobalSection(ProjectConfigurationPlatforms) = postSolution
 -		{1C962CCA-74DC-4F72-8C83-7617F2275D1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 -		{1C962CCA-74DC-4F72-8C83-7617F2275D1A}.Debug|Any CPU.Build.0 = Debug|Any CPU
 -	EndGlobalSection
 -	GlobalSection(SolutionProperties) = preSolution
 -		HideSolutionNode = FALSE
 -	EndGlobalSection
 -EndGlobal
 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..."> diff --git a/components/redirector.js b/components/redirector.js index 4e44c11..d0d9694 100644 --- a/components/redirector.js +++ b/components/redirector.js @@ -2,269 +2,25 @@  const CSSB_CONTRACTID = "@einaregilsson.com/redirector;1";
  const CSSB_CID = Components.ID("{b7a7a54f-0581-47ff-b086-d6920cb7a3f7}");
 -const Cc = Components.classes;
 -const Ci = Components.interfaces;
 -const Cr = Components.results;
 -const kRedirectorWildcard = 'W';
 -const kRedirectorRegex= 'R';
 +var Cc = Components.classes;
 +var Ci = Components.interfaces;
 +var Cr = Components.results;
 +var kRedirectorWildcard = 'W';
 +var kRedirectorRegex= 'R';
 +var nsIContentPolicy = Ci.nsIContentPolicy;
 -const nsIContentPolicy = Ci.nsIContentPolicy;
  function RedirectorPolicy() {
 -    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
 -            });
 -        }
 -    }
 +	this.init();
  }
 -RedirectorPolicy.prototype = {
 -    prefBranch : null,
 -    list : null,
 -    strings : null,
 -    cout : Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService),
 -
 -    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() {
 -		//TODO: Reload implementation    
 -    }, 
 -    
 -    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;
 -    }
 -};
 +try {
 +Cc["@mozilla.org/moz/jssubscript-loader;1"]
 +	.getService(Ci.mozIJSSubScriptLoader)
 +		.loadSubScript('chrome://redirector/content/redirector.prototype.js');
 +}catch(e) {
 +Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(e);
 +}
  /*
   * Factory object
   */
 diff --git a/install.rdf b/install.rdf index f86b411..093cd5f 100644 --- a/install.rdf +++ b/install.rdf @@ -5,7 +5,7 @@    <Description about="urn:mozilla:install-manifest">      <em:id>redirector@einaregilsson.com</em:id>      <em:name>Redirector</em:name> -    <em:version>1.7.2</em:version> +    <em:version>1.8</em:version>      <em:creator>Einar Egilsson</em:creator>      <em:description>Automatically redirects to user-defined urls on certain pages</em:description>      <em:homepageURL>http://tech.einaregilsson.com/projects/redirector/</em:homepageURL> @@ -14,7 +14,7 @@      <em:targetApplication>        <Description>          <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id> <!-- firefox --> -        <em:minVersion>3.0</em:minVersion> +        <em:minVersion>3.5</em:minVersion>          <em:maxVersion>3.5.*</em:maxVersion>        </Description>      </em:targetApplication> | 
