aboutsummaryrefslogtreecommitdiff
path: root/chrome/content/redirect.js
blob: fe468f05de53d21d6c1b3f336939b2de00a3c6ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//// $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 Redirect = {

    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 || '';

        $('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.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;
        redirectUrl = $('txtRedirectUrl').value;
        pattern = $('txtPattern').value;
        excludePattern = $('txtExcludePattern').value;
        example = $('txtExampleUrl').value;

        extName = this.strings.getString('extensionName');

        if ($('rdoRegex').selected) {
            redirectUrl = Redirector.regexMatch(pattern, example, redirectUrl);
            if (excludePattern) {
                isExcluded = Redirector.regexMatch(excludePattern, example, 'exclude');
            }
        } else {
            redirectUrl = Redirector.wildcardMatch(pattern, example, redirectUrl);
            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]));
        }
    }
};