Posted By

scriptmakingcom on 03/22/08


Tagged

addeventlistener


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

SpinZ


Detecting Page Onload, FF EXTENSION DEV


 / Published in: JavaScript
 

URL: http://forums.mozillazine.org/viewtopic.php?t=606756&postdays=0&postorder=asc&postsperpage=15&start=15

  1. var myExtension = {
  2.  
  3. // This allows you to disable the listener
  4. // without having to remove the script file from
  5. // your xul overlay every time
  6. enabled: true,
  7.  
  8. // Log info on each loaded/unloaded page to the console?
  9. dumpToConsole: false,
  10.  
  11. // Display an alert box displaying page info as each page loads/unloads?
  12. enableAlerts: true,
  13.  
  14. onWindowLoad: function(e){
  15. // Should we even bother?
  16. if(!this.enabled) return;
  17.  
  18. // Get some useful services
  19. this.cs = Components.classes["@mozilla.org/consoleservice;1"]
  20. .getService(Components.interfaces.nsIConsoleService);
  21. this.ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  22. .getService(Components.interfaces.nsIPromptService);
  23.  
  24. // What end of line character do we use?
  25. this.eol = this.getEol();
  26.  
  27. // Note that it is necessary to wrap our onPageLoad function
  28. // and explicitly state "myExtension" here instead of just using
  29. // the "this" keyword
  30.  
  31. // Notice that I use gBrowser here because
  32. // window.gBrowser == document.getElementById("content")
  33. // If gBrowser is not defined then you have bigger problems than your extension.
  34.  
  35. // Add our page listener(s)
  36. //gBrowser.addEventListener("DOMContentLoaded", function(e) { myExtension.onPageLoad(e); }, true);
  37. gBrowser.addEventListener("load", function(e) { myExtension.onPageLoad(e); }, true);
  38.  
  39.  
  40. // Notice we use our onPageLoad even for unload
  41. // because we can tell the type of page event by event.type
  42. // This uses less code
  43. gBrowser.addEventListener("unload", function(e) { myExtension.onPageLoad(e); }, true);
  44. },
  45.  
  46. onPageLoad: function(e) {
  47. if(e.originalTarget instanceof HTMLDocument) {
  48. var doc = e.originalTarget;
  49. if(!doc.defaultView.frameElement) {
  50. // For top level documents only
  51. var title = (doc.title || "<Untitled>");
  52. var message = "Page Event: \"" + e.type + "\"" + this.eol + title + this.eol + doc.documentURI;
  53. if(this.dumpToConsole) {
  54. this.cs.logStringMessage("myExtension" + this.eol + message);
  55. }
  56. if(this.enableAlerts) {
  57. this.ps.alert(window,"myExtension",message);
  58. }
  59. }
  60. // Do something depending on the event type
  61. /* switch(e.type) {
  62.   case "DOMContentLoaded": break;
  63.   case "load": break;
  64.   case "unload": break;
  65.   default: break;
  66.   } */
  67. }
  68. },
  69.  
  70. getPlatform: function() {
  71. var platform = navigator.platform.toLowerCase();
  72. if(platform.indexOf('win') != -1) return 'win'; // win
  73. else if(platform.indexOf('mac') != -1) return 'mac'; // mac
  74. else return 'other'; // other (nix)
  75. },
  76. getEol: function() {
  77. switch(this.getPlatform()) {
  78. case 'win': return '
  79. ';
  80. case 'mac': return '\r';
  81. case 'other': return '\n';
  82. default: return '<eol>';
  83. }
  84. }
  85. };
  86. window.addEventListener("load", function(e) { myExtension.onWindowLoad(e); }, false);

Report this snippet  

You need to login to post a comment.