Mr_Mig

Crossvrowser Userscript pattern

Sep 27th, 2011
5,884
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name myUserJS
  3. // @description My first crossbrowser userscript
  4. // @author Mr_Mig
  5. // @license MIT
  6. // @version 1.0
  7. // @include http://userscripts.org/*
  8. // ==/UserScript==
  9.  
  10. // wrap the script in a closure (opera, ie)
  11. // do not spoil the global scope
  12. // The script can be transformed into a bookmarklet easily :)
  13. (function(window, undefined ) {
  14.  
  15.     // normalized window
  16.     var w;
  17.     if (unsafeWindow != "undefined"){
  18.         w = unsafeWindow
  19.     } else {
  20.         w = window;
  21.     }
  22.  
  23.     // You can inject almost any javascript library here.
  24.     // Just pass the w as the window reference,
  25.         // e.g. jquery.min.js embedding:
  26.     // (function(a,b){function ci(a) ... a.jQuery=a.$=d})(w);
  27.  
  28.  
  29.     // do not run in frames
  30.     if (w.self != w.top){
  31.         return;
  32.     }
  33.  
  34.     // additional url check.
  35.     // Google Chrome do not treat @match as intended sometimes.
  36.     if (/http:\/\/userscripts.org/.test(w.location.href)){
  37.         //Below is the userscript code itself
  38.    
  39.    
  40.         alert("Hello from the userscript");
  41.     }
  42. })(window);
RAW Paste Data