]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/_base/_loader/hostenv_ff_ext.js
2 Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
8 // a host environment specifically built for Mozilla extensions, but derived
9 // from the browser host environment
10 if(typeof window
!= 'undefined'){
11 dojo
.isBrowser
= true;
12 dojo
._name
= "browser";
16 // http://developer.mozilla.org/en/mozIJSSubScriptLoader
19 // attempt to figure out the path to dojo if it isn't set in the config
22 // this is a scope protection closure. We set browser versions and grab
23 // the URL we were loaded from here.
25 // FIXME: need to probably use a different reference to "document" to get the hosting XUL environment
27 d
.baseUrl
= d
.config
.baseUrl
;
29 // fill in the rendering support information in dojo.render.*
31 var dua
= n
.userAgent
;
32 var dav
= n
.appVersion
;
33 var tv
= parseFloat(dav
);
35 d
.isMozilla
= d
.isMoz
= tv
;
37 d
.isFF
= parseFloat(dua
.split("Firefox/")[1]) || undefined;
41 d
.isQuirks
= document
.compatMode
== "BackCompat";
44 // TODO: is the HTML LANG attribute relevant?
45 d
.locale
= dojo
.config
.locale
|| n
.language
.toLowerCase();
47 d
._xhrObj = function(){
48 return new XMLHttpRequest();
51 // monkey-patch _loadUri to handle file://, chrome://, and resource:// url's
52 var oldLoadUri
= d
._loadUri
;
53 d
._loadUri = function(uri
, cb
){
54 var handleLocal
= ["file:", "chrome:", "resource:"].some(function(prefix
){
55 return String(uri
).indexOf(prefix
) == 0;
59 // http://developer.mozilla.org/en/mozIJSSubScriptLoader
60 var l
= Components
.classes
["@mozilla.org/moz/jssubscript-loader;1"]
61 .getService(Components
.interfaces
.mozIJSSubScriptLoader
);
62 var value
= l
.loadSubScript(uri
, d
.global
)
66 // otherwise, call the pre-existing version
67 return oldLoadUri
.apply(d
, arguments
);
72 d
._isDocumentOk = function(http
){
73 var stat
= http
.status
|| 0;
74 return (stat
>= 200 && stat
< 300) || // Boolean
75 stat
== 304 || // allow any 2XX response code
76 stat
== 1223 || // get it out of the cache
77 (!stat
&& (location
.protocol
=="file:" || location
.protocol
=="chrome:") );
81 // var owloc = window.location+"";
82 // var base = document.getElementsByTagName("base");
83 // var hasBase = (base && base.length > 0);
86 d
._getText = function(/*URI*/ uri
, /*Boolean*/ fail_ok
){
87 // summary: Read the contents of the specified uri and return those contents.
89 // A relative or absolute uri. If absolute, it still must be in
90 // the same "domain" as we are.
92 // Default false. If fail_ok and loading fails, return null
93 // instead of throwing.
94 // returns: The response text. null is returned when there is a
95 // failure and failure is okay (an exception otherwise)
97 // alert("_getText: " + uri);
99 // NOTE: must be declared before scope switches ie. this._xhrObj()
100 var http
= d
._xhrObj();
102 if(!hasBase
&& dojo
._Url
){
103 uri
= (new dojo
._Url(uri
)).toString();
105 if(d
.config
.cacheBust
){
106 //Make sure we have a string before string methods are used on uri
108 uri
+= (uri
.indexOf("?") == -1 ? "?" : "&") + String(d
.config
.cacheBust
).replace(/\W+/g,"");
110 var handleLocal
= ["file:", "chrome:", "resource:"].some(function(prefix
){
111 return String(uri
).indexOf(prefix
) == 0;
115 // http://forums.mozillazine.org/viewtopic.php?p=921150#921150
116 var ioService
= Components
.classes
["@mozilla.org/network/io-service;1"]
117 .getService(Components
.interfaces
.nsIIOService
);
118 var scriptableStream
=Components
119 .classes
["@mozilla.org/scriptableinputstream;1"]
120 .getService(Components
.interfaces
.nsIScriptableInputStream
);
122 var channel
= ioService
.newChannel(uri
, null, null);
123 var input
= channel
.open();
124 scriptableStream
.init(input
);
125 var str
= scriptableStream
.read(input
.available());
126 scriptableStream
.close();
130 http
.open('GET', uri
, false);
134 if(!d
._isDocumentOk(http
)){
135 var err
= Error("Unable to load "+uri
+" status:"+ http
.status
);
136 err
.status
= http
.status
;
137 err
.responseText
= http
.responseText
;
141 if(fail_ok
){ return null; } // null
142 // rethrow the exception
145 return http
.responseText
; // String
149 d
._windowUnloaders
= [];
152 d
.windowUnloaded = function(){
154 // signal fired by impending window destruction. You may use
155 // dojo.addOnWIndowUnload() or dojo.connect() to this method to perform
156 // page/application cleanup methods. See dojo.addOnWindowUnload for more info.
157 var mll
= d
._windowUnloaders
;
164 d
.addOnWindowUnload = function(/*Object?*/obj
, /*String|Function?*/functionName
){
166 // registers a function to be triggered when window.onunload fires.
167 // Be careful trying to modify the DOM or access JavaScript properties
168 // during this phase of page unloading: they may not always be available.
169 // Consider dojo.addOnUnload() if you need to modify the DOM or do heavy
172 // | dojo.addOnWindowUnload(functionPointer)
173 // | dojo.addOnWindowUnload(object, "functionName")
174 // | dojo.addOnWindowUnload(object, function(){ /* ... */});
176 d
._onto(d
._windowUnloaders
, obj
, functionName
);
182 dojo
._defaultContext
= [ window
, document
];
184 dojo
.pushContext = function(/*Object|String?*/g
, /*MDocumentElement?*/d
){
186 // causes subsequent calls to Dojo methods to assume the
187 // passed object and, optionally, document as the default
188 // scopes to use. A 2-element array of the previous global and
189 // document are returned.
191 // dojo.pushContext treats contexts as a stack. The
192 // auto-detected contexts which are initially provided using
193 // dojo.setContext() require authors to keep state in order to
194 // "return" to a previous context, whereas the
195 // dojo.pushContext and dojo.popContext methods provide a more
196 // natural way to augment blocks of code to ensure that they
197 // execute in a different window or frame without issue. If
198 // called without any arguments, the default context (the
199 // context when Dojo is first loaded) is instead pushed into
200 // the stack. If only a single string is passed, a node in the
201 // intitial context's document is looked up and its
202 // contextWindow and contextDocument properties are used as
203 // the context to push. This means that iframes can be given
204 // an ID and code can be executed in the scope of the iframe's
205 // document in subsequent calls easily.
207 // The global context. If a string, the id of the frame to
208 // search for a context and document.
210 // The document element to execute subsequent code with.
211 var old
= [dojo
.global
, dojo
.doc
];
215 n
= dojo
._defaultContext
;
218 if(!d
&& dojo
.isString(g
)){
219 var t
= document
.getElementById(g
);
220 if(t
.contentDocument
){
221 n
= [t
.contentWindow
, t
.contentDocument
];
226 dojo
.setContext
.apply(dojo
, n
);
230 dojo
.popContext = function(){
232 // If the context stack contains elements, ensure that
233 // subsequent code executes in the *previous* context to the
234 // current context. The current context set ([global,
235 // document]) is returned.
237 if(!contexts
.length
){
240 dojo
.setContext
.apply(dojo
, contexts
.pop());
245 // don't really like the current arguments and order to
246 // _inContext, so don't make it public until it's right!
247 dojo
._inContext = function(g
, d
, f
){
248 var a
= dojo
._toArray(arguments
);
253 dojo
.pushContext(g
, d
);
261 dojo
._initFired
= false;
262 // BEGIN DOMContentLoaded, from Dean Edwards (http://dean.edwards.name/weblog/2006/06/again/)
263 dojo
._loadInit = function(e
){
264 dojo
._initFired
= true;
265 // allow multiple calls, only first one will take effect
266 // A bug in khtml calls events callbacks for document for event which isnt supported
267 // for example a created contextmenu event calls DOMContentLoaded, workaround
268 var type
= (e
&& e
.type
) ? e
.type
.toLowerCase() : "load";
269 if(arguments
.callee
.initialized
|| (type
!= "domcontentloaded" && type
!= "load")){ return; }
270 arguments
.callee
.initialized
= true;
271 if(dojo
._inFlightCount
== 0){
272 dojo
._modulesLoaded();
279 var _handleNodeEvent = function(evtName, fp){
281 // non-destructively adds the specified function to the node's
283 // evtName: should be in the form "onclick" for "onclick" handlers.
284 // Make sure you pass in the "on" part.
285 var oldHandler = _w[evtName] || function(){};
286 _w[evtName] = function(){
287 fp.apply(_w, arguments);
288 oldHandler.apply(_w, arguments);
292 // FIXME: dojo.unloaded requires dojo scope, so using anon function wrapper.
293 _handleNodeEvent("onbeforeunload", function() { dojo.unloaded(); });
294 _handleNodeEvent("onunload", function() { dojo.windowUnloaded(); });
300 // this event fires a lot, namely for all plugin XUL overlays and for
301 // all iframes (in addition to window navigations). We only want
302 // Dojo's to fire once..but we might care if pages navigate. We'll
303 // probably need an extension-specific API
304 if(!dojo
.config
.afterOnLoad
){
305 window
.addEventListener("DOMContentLoaded",function(e
){
307 // console.log("DOM content loaded", e);
311 } //if (typeof window != 'undefined')
313 //Register any module paths set up in djConfig. Need to do this
314 //in the hostenvs since hostenv_browser can read djConfig from a
315 //script tag's attribute.
317 var mp
= dojo
.config
["modulePaths"];
319 for(var param
in mp
){
320 dojo
.registerModulePath(param
, mp
[param
]);
325 //Load debug code if necessary.
326 if(dojo
.config
.isDebug
){
327 // logging stub for extension logging
328 console
.log = function(m
){
329 var s
= Components
.classes
["@mozilla.org/consoleservice;1"].getService(
330 Components
.interfaces
.nsIConsoleService
332 s
.logStringMessage(m
);
334 console
.debug = function(){
335 console
.log(dojo
._toArray(arguments
).join(" "));
337 // FIXME: what about the rest of the console.* methods? And is there any way to reach into firebug and log into it directly?