今天学习的一点代码:
首先使用 Process
Listener 接口可以跟踪一个页面(tab页)加载、完成加载、切换时候的状态。以下代码在tab页加载完成时,以及切换tab页的时候会触发自定义的函数
然后是在XPCOM中使用xmlhttprequest,和一般js里不同的是,需要调用一个组件类然后创建一个实例:
Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(Components.interfaces.nsIXMLHttpRequest);
不过这里的XHR一样要受到同源策略的限制。不知道GreaseMonkey的 GM_xmlhttpRequest 是怎么实现的,继续学习中。
整个例子核心代码如下:
- const STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START;
- const STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP;
- var myListener =
- {
- QueryInterface: function(aIID)
- {
- if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
- aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
- aIID.equals(Components.interfaces.nsISupports))
- return this;
- throw Components.results.NS_NOINTERFACE;
- },
- onStateChange: function(aWebProgress, aRequest, aFlag, aStatus)
- {
- // If you use myListener for more than one tab/window, use
- // aWebProgress.DOMWindow to obtain the tab/window which triggers the state change
- if(aFlag & STATE_START)
- {
- // This fires when the load event is initiated
- //alert("start");
- }
- if(aFlag & STATE_STOP)
- {
- // This fires when the load finishes
- //alert("stop");
- myExtension.doSomething(aWebProgress);
- }
- return 0;
- },
- onLocationChange: function(aProgress, aRequest, aURI)
- {
- // This fires when the location bar changes; i.e load event is confirmed
- // or when the user switches tabs. If you use myListener for more than one tab/window,
- // use aProgress.DOMWindow to obtain the tab/window which triggered the change.
-
- myExtension.doSomething(aProgress);
- return 0;
- },
- // For definitions of the remaining functions see XULPlanet.com
- onProgressChange: function() {return 0;},
- onStatusChange: function() {return 0;},
- onSecurityChange: function() {return 0;},
- onLinkIconAvailable: function() {return 0;}
- }
- var i; // counter
- var scriptText = {
- src: '',
- textContent: ''
- };
- var myExtension = {
- init: function() {
- // Listen for webpage loads
- gBrowser.addProgressListener(myListener,
- Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
- },
- uninit: function() {
- gBrowser.removeProgressListener(myListener);
- },
- doSomething: function(aProgress) {
- var doc = aProgress.DOMWindow.document;
-
- for (i=0; i
- scriptText.src = doc.getElementsByTagName("script")[i].src;
- scriptText.textContent = doc.getElementsByTagName("script")[i].textContent;
- if (scriptText.src == ""){
- //scriptText.textContent = doc.getElementsByTagName("script")[i].textContent;
- //alert(scriptText.textContent);
- }
- else {
- ajaxGet(scriptText.src);
- //alert(scriptText.src+'\n\n'+scriptText.textContent);
- }
- }
- }
- };
- window.addEventListener("load", function() {myExtension.init()}, false);
- window.addEventListener("unload", function() {myExtension.uninit()}, false);
- // XMLHttpRequest
- function ajaxGet(url){
- var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
- .createInstance(Components.interfaces.nsIXMLHttpRequest);
- req.onprogress = onProgress;
- req.onload = onLoad;
- req.onerror = onError;
- req.open("GET", url, true);
- req.send(null);
- }
- function onProgress(e){
-
- }
- function onLoad(e){
- dump(e.target.status+'\n\n'+e.target.responseText);
- }
- function onError(e){
-
- }
阅读(1292) | 评论(0) | 转发(0) |