aboutsummaryrefslogtreecommitdiff
path: root/chrome/content/overlay.js
blob: dd6f5278e5ca5dc0f8baa95efe6479a3ba27a0e5 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//// $Id$


var RedirectorOverlay = {

    id          : "redirector@einaregilsson.com",
    name        : "Redirector",
    initialized : false,
    strings     : null,

    onLoad : function(event) {
        try {

            // initialization code
            RedirLib.initialize(this);
            RedirLib.debug("Initializing...");
            $('contentAreaContextMenu')
                .addEventListener("popupshowing", function(e) { RedirectorOverlay.showContextMenu(e); }, false);

            if (!RedirLib.getBoolPref('showContextMenu')) {
                $('redirector-context').hidden = true;
            }
            if (!RedirLib.getBoolPref('showStatusBarIcon')) {
                $('redirector-status').hidden = true;
            }
            Redirector.init();
            this.overrideOnStateChange();            
            this.overrideOpenNewWindowWith();
            this.overrideOpenNewTabWith();
            this.strings = document.getElementById("redirector-strings");
            Redirector.strings = this.strings;
            this.prefObserver.register();
            this.setStatusBarImg();

            RedirLib.debug("Finished initialization");
            this.initialized = true;

        } catch(e) {
            //Don't use RedirLib because it's initialization might have failed.
            if (this.strings) {
                alert(this.strings.getString("initError")._(this.name) + "\n\n" + e);
            } else {
                alert(e);
            }
        }
    },

    overrideOnStateChange : function() {
        var origOnStateChange = nsBrowserStatusHandler.prototype.onStateChange;

        nsBrowserStatusHandler.prototype.onStateChange = function(aWebProgress, aRequest, aStateFlags, aStatus) {
            
            if(aStateFlags & Ci.nsIWebProgressListener.STATE_START
            && aStateFlags| Ci.nsIWebProgressListener.STATE_IS_NETWORK
            && aStateFlags| Ci.nsIWebProgressListener.STATE_IS_REQUEST
                && aRequest && aWebProgress.DOMWindow) {
      
                //If it's not a GET request we'll always do a slow redirect so the web will continue
                //to work in the way you'd expect
                try {
                    var oHttp = aRequest.QueryInterface(Ci.nsIHttpChannel);
                    var method = oHttp.requestMethod;
          
                    if (method != "GET") {
                        origOnStateChange.apply(this, arguments);
                        return;
                    }
        
                } catch(ex) {
                    origOnStateChange.apply(this, arguments);
                    return;
                }

                var uri = aRequest.QueryInterface(Ci.nsIChannel).URI.spec;
                
                RedirLib.debug('Checking url %1 for instant redirect'._(uri));
                var redirectUrl = Redirector.getRedirectUrlForInstantRedirect(uri);
                if (redirectUrl.url && oHttp.notificationCallbacks) {
                    const NS_BINDING_ABORTED = 0x804b0002;
                    aRequest.cancel(NS_BINDING_ABORTED);
                    var newStateFlags = Ci.nsIWebProgressListener.STATE_STOP | Ci.nsIWebProgressListener.STATE_IS_NETWORK | Ci.nsIWebProgressListener.STATE_IS_REQUEST;
                    origOnStateChange.call(this, aWebProgress, aRequest, newStateFlags, "");
                    var interfaceRequestor = oHttp.notificationCallbacks.QueryInterface(Ci.nsIInterfaceRequestor);
                    var targetDoc = interfaceRequestor.getInterface(Ci.nsIDOMWindow).document;    
                    var gotoUrl = Redirector.makeAbsoluteUrl(uri, redirectUrl.url);
                    Redirector.goto(gotoUrl, redirectUrl.pattern, uri, targetDoc); 
                } else {
                    origOnStateChange.apply(this, arguments);
                }

            } else {
                origOnStateChange.apply(this, arguments);
            }
            
        };
    },

    overrideOpenNewWindowWith: function() {
        
        window.__openNewWindowWith = window.openNewWindowWith;
        window.openNewWindowWith = function (href, sourceURL, postData, allowThirdPartyFixup) {
            var redirectUrl = Redirector.getRedirectUrlForInstantRedirect(href);
            if (redirectUrl.url) {
                __openNewWindowWith(redirectUrl.url, href, postData, allowThirdPartyFixup);
            } else {
                __openNewWindowWith(href, sourceURL, postData, allowThirdPartyFixup);
            }
        
        };
      },


    overrideOpenNewTabWith: function() {
        
        window.__openNewTabWith = window.openNewTabWith;
        window.openNewTabWith = function (href, sourceURL, postData, event, allowThirdPartyFixup) {
            var redirectUrl = Redirector.getRedirectUrlForInstantRedirect(href);
            if (redirectUrl.url) {
                __openNewTabWith(redirectUrl.url, href, postData, event, allowThirdPartyFixup);
            } else {
                __openNewTabWith(href, sourceURL, postData, event, allowThirdPartyFixup);
            }
        
        };
    },

  
    onDOMContentLoaded : function(event) {
        var redirect, link, links, url;
        
        if (event.target.toString().indexOf('HTMLDocument') == -1) {
            return;
        }

        url = event.target.location.href;

        RedirLib.debug('Processing url %1'._(url));
        Redirector.processUrl(url, event.target);
    },


    onUnload : function(event) {
        RedirectorOverlay.prefObserver.unregister();
        Redirector.prefObserver.unregister();
        //Clean up here
        RedirLib.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) {
        Redirector.openSettings();
    },

    toggleEnabled : function(event) {
        RedirLib.setBoolPref('enabled', !RedirLib.getBoolPref('enabled'));
    },

    statusBarClick : function(event) {
        var LEFT = 0, RIGHT = 2;

        alert($('redirector-status-popup').style.right);        
        if (event.button == LEFT) {
            RedirectorOverlay.toggleEnabled();
        } else if (event.button == RIGHT) {
            $('redirector-status-popup').left = $('redirector-status').left;
            $('redirector-status-popup').showPopup();
        }
    },

    setStatusBarImg : function() {
        var statusImg = $('redirector-statusbar-img');

        if (RedirLib.getBoolPref('enabled')) {
            statusImg.src = 'chrome://redirector/content/statusactive.PNG'
            statusImg.setAttribute('tooltiptext', this.strings.getString('enabledTooltip'));
            Redirector.enabled = true;
        } else {
            statusImg.src = 'chrome://redirector/content/statusinactive.PNG'
            statusImg.setAttribute('tooltiptext', this.strings.getString('disabledTooltip'));
            Redirector.enabled = false;
        }
    },

    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("DOMContentLoaded", function(event) { RedirectorOverlay.onDOMContentLoaded(event); }, true);
window.addEventListener("unload", function(event) { RedirectorOverlay.onUnload(event); }, false);