aboutsummaryrefslogtreecommitdiff
path: root/chrome/content/redirector.js
blob: 63ee85d22bf7d9b24ef69370d2c0017eb897dda4 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const kRedirectorWildcard = 'W';
const kRedirectorRegex= 'R';

var Redirector = {

    list : [],

    init : function() {
        this.load();
        this.prefObserver.register();
    },

    save : function() {
        var r
          , tempList = [];

        for each (r in this.list) {
            tempList.push([r.exampleUrl, r.pattern, r.redirectUrl, r.onlyIfLinkExists, r.patternType]);
        }
        RedirLib.setCharPref('redirects', tempList.toSource());
    },

    load : function() {
        var tempList = eval(RedirLib.getCharPref('redirects'));
        var arr;

        this.list = [];

        for each (arr in tempList) {
            this.list.push({
                exampleUrl          : arr[0],
                pattern             : arr[1],
                redirectUrl         : arr[2],
                onlyIfLinkExists    : arr[3],
                patternType         : arr[4]
            });
        }

    },

    addRedirect : function(redirect) {
        this.list.push(redirect);
        this.save();
    },

    processUrl : function(url) {
        var redirect, link, links;
        for each (redirect in this.list) {

            if (redirect.patternType == kRedirectorWildcard && this.wildcardMatch(redirect.pattern, url)) {
                RedirLib.debug('%1 matches %2'._(redirect.pattern, url));
                if (redirect.onlyIfLinkExists) {
                    links = window.content.document.getElementsByTagName('a');

                    for each(link in links) {

                        if (link.href && link.href.toString() == redirect.redirectUrl) {
                            RedirLib.debug('Found a link for %1'._(redirect.redirectUrl));
                            this._goto(redirect);
                            return;
                        }
                    }

                    RedirLib.debug('Did not find a link for %1'._(redirect.redirectUrl));

                } else {
                    this._goto(redirect);
                }
            }
        }
    },

    _goto : function(redirect) {

        if (redirect.redirectUrl == window.content.location.href) {
            RedirLib.msgBox(this.strings.getString('extensionName'), this.strings.getFormattedString('recursiveError', [redirect.pattern, redirect.redirectUrl]));
        } else {
            window.content.location.href = redirect.redirectUrl;
        }
    },

    wildcardMatch : function(pattern, text) {
        var parts
          , part
          , i
          , pos;

        parts = pattern.split('*');

        for (i in parts) {

            part = parts[i];

            pos = text.indexOf(part);

            if (pos == -1) {
                return false;
            }

            if (i == 0 && pos != 0) {
                return false;
            }

            if (i == parts.length -1 && i != "" && text.substr(text.length - part.length) != part) {
                return false;

            }

            text = text.substr(pos + part.length);
        }

        return true;
    },

    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.redirects') {
                Redirector.load();
            }
        }

    },
};