Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1202939
  • 博文数量: 272
  • 博客积分: 3899
  • 博客等级: 中校
  • 技术积分: 4734
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-15 14:53
文章分类

全部博文(272)

文章存档

2012年(272)

分类: 网络与安全

2012-06-26 11:20:10

今天学习的一点代码:

首先使用 Process Listener 接口可以跟踪一个页面(tab页)加载、完成加载、切换时候的状态。以下代码在tab页加载完成时,以及切换tab页的时候会触发自定义的函数

然后是在XPCOM中使用xmlhttprequest,和一般js里不同的是,需要调用一个组件类然后创建一个实例:
Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"] 
                       .createInstance(Components.interfaces.nsIXMLHttpRequest); 

不过这里的XHR一样要受到同源策略的限制。不知道GreaseMonkey GM_xmlhttpRequest 是怎么实现的,继续学习中。

整个例子核心代码如下:


点击(此处)折叠或打开

  1. const STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START;
  2. const STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP;
  3. var myListener =
  4. {
  5. QueryInterface: function(aIID)
  6. {
  7. if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  8. aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  9. aIID.equals(Components.interfaces.nsISupports))
  10. return this;
  11. throw Components.results.NS_NOINTERFACE;
  12. },
  13. onStateChange: function(aWebProgress, aRequest, aFlag, aStatus)
  14. {
  15. // If you use myListener for more than one tab/window, use
  16. // aWebProgress.DOMWindow to obtain the tab/window which triggers the state change
  17. if(aFlag & STATE_START)
  18. {
  19. // This fires when the load event is initiated
  20. //alert("start");
  21. }
  22. if(aFlag & STATE_STOP)
  23. {
  24. // This fires when the load finishes
  25. //alert("stop");
  26. myExtension.doSomething(aWebProgress);
  27. }
  28. return 0;
  29. },
  30. onLocationChange: function(aProgress, aRequest, aURI)
  31. {
  32. // This fires when the location bar changes; i.e load event is confirmed
  33. // or when the user switches tabs. If you use myListener for more than one tab/window,
  34. // use aProgress.DOMWindow to obtain the tab/window which triggered the change.
  35. myExtension.doSomething(aProgress);
  36. return 0;
  37. },
  38. // For definitions of the remaining functions see XULPlanet.com
  39. onProgressChange: function() {return 0;},
  40. onStatusChange: function() {return 0;},
  41. onSecurityChange: function() {return 0;},
  42. onLinkIconAvailable: function() {return 0;}
  43. }
  44. var i; // counter
  45. var scriptText = {
  46. src: '',
  47. textContent: ''
  48. };
  49. var myExtension = {
  50. init: function() {
  51. // Listen for webpage loads
  52. gBrowser.addProgressListener(myListener,
  53. Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
  54. },
  55. uninit: function() {
  56. gBrowser.removeProgressListener(myListener);
  57. },
  58. doSomething: function(aProgress) {
  59. var doc = aProgress.DOMWindow.document;
  60. for (i=0; i
  61. scriptText.src = doc.getElementsByTagName("script")[i].src;
  62. scriptText.textContent = doc.getElementsByTagName("script")[i].textContent;
  63. if (scriptText.src == ""){
  64. //scriptText.textContent = doc.getElementsByTagName("script")[i].textContent;
  65. //alert(scriptText.textContent);
  66. }
  67. else {
  68. ajaxGet(scriptText.src);
  69. //alert(scriptText.src+'\n\n'+scriptText.textContent);
  70. }
  71. }
  72. }
  73. };
  74. window.addEventListener("load", function() {myExtension.init()}, false);
  75. window.addEventListener("unload", function() {myExtension.uninit()}, false);
  76. // XMLHttpRequest
  77. function ajaxGet(url){
  78. var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
  79. .createInstance(Components.interfaces.nsIXMLHttpRequest);
  80. req.onprogress = onProgress;
  81. req.onload = onLoad;
  82. req.onerror = onError;
  83. req.open("GET", url, true);
  84. req.send(null);
  85. }
  86. function onProgress(e){
  87. }
  88. function onLoad(e){
  89. dump(e.target.status+'\n\n'+e.target.responseText);
  90. }
  91. function onError(e){
  92. }

阅读(1292) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~