Greasemonkey Tools For Developers v1.9.1

By Empty_Soul Last update Aug 23, 2010 — Installed 82,474 times.

Script Summary: A Custom Javascript Object that maps all the Greasemonkey functions to a simpler "GM" Object. Also, I have included some other useful functions with the GM Object as well.



Version: 1.9.1

  • [UPDATED] 8/16/2010 : Added new "check" object to the GM object for validating form fields or strings
  • [UPDATED] ( 8/1/2010 10:15pm ) API v1.9 Change:
    • GM.setVal() is now GM.value.set()
    • GM.getVal() is now GM.value.get()
    • GM.delVal() is now GM.value.del()
    • GM.listVals() is now GM.value.list()
  • [UPDATED] 8/1/2010 working on better function explinations for GM Object API
  • [UPDATED] 5/7/2010 to all the functions referenced on Greasespot.net

  • How to Use

    Just include this line in your metadata block

    // @require http://userscripts.org/scripts/source/72585.user.js

    Then use any GM.method or GM.object.method in your script

    GM.css('body div.noshow {display: none;}');
    
    GM.value.set('visible', true);
    
    var isVisible = GM.value.get('visible'); => returns true
    
    var testVar = "Some Value"
    GM.typeCheck(testVar, 'string'); => returns true
    GM.typeCheck(testVar, 'integer'); => returns false
    
    etc...


    Mapped Greasemonkey Functions

    GM.version

    • Returns (string) : Current GM object version number.

    GM.uw

    • Reference to the current page, shortcut for unsafeWindow.

    GM.log(message)

    • Parameters:
      • message : object/int/string/array/bool : what will be displayed in the javascript console.

    GM.value

    • Object for operations with memory values.
    • Methods:
      • GM.value.set(key, value)

        • Parameters:
          • key : string : name of the key to retrieve the value.
          • value : string/boolean/int : value to be stored in persistent memory.

      • GM.value.get(key, default)

        • If passed just a (string) key value
          GM.value.get('undefinedStoredValue');
          The function will return false if the value of 'key' is unset or was never defined.
        • If passed a key and a default value
          GM.value.get('undefinedStoredValue', 'unset');
          Returns the default value 'unset' instead of false

      • GM.value.delVal(key)

        • Parameters:
          • key : string : name of the value in memory to delete.

      • GM.value.list()

        • Returns:
          • array : (string) names of values stored memory.

    GM.resTxt(resourceName)

    • Parameters:
      • string : resourceName : The name provided when the @resource was defined.
    • Returns:
      • String : Given a defined @resource, this method returns it as a string.

    GM.resUrl(resourceName)

    • Parameters:
      • string : resourceName : The name provided when the @resource was defined
    • Returns:
      • string : A base64 string of encoded data: URI.

    GM.css(cssString)

    • Parameters:
      • string : cssString : string of CSS to embed in the page.

    GM.ajax(ajaxObject)

    • Parameters:
      • string : ajaxObject : object containing parameters needed for request.
    • Example:
    • GM.ajax({
              method: "GET",
              url: "http://www.example.com/",
              onload: function(response) {
                  alert(response.responseText);
              }
          });
    • More Info ...

    GM.ajaxHeaders =

    • Default headers that can be used in when using POST and GM.ajax()
    • {
              "Content-type" : "application/x-www-form-urlencoded",
              "User-Agent" : "Mozilla/5.0",
              "Accept" : "text/xml"
          }
    • Example:
    • GM.ajax({
              method: "POST",
              url: "http://www.example.com/login",
              data: "username=jon&pw=abc123",
              headers: GM.ajaxHeaders,
              onload: function(response) {
                  alert(response.responseText);
              }
          });

    GM.ajaxLoadGif

    GM.newTab(url)

    • Parameters:
      • string : url : string for new URL to open in a new tab.

    GM.menuReg(caption, commandFunc, accelKey, accelModifiers, accessKey)

    • Parameters:
      • string : caption : Caption to display on the menu item.
      • function : commandFunc : Function The function to call when this menu item is selected by the user.
      • string : accelKey : A single character or keycode that can trigger the command.
      • string : accelModifiers : A string listing modifiers that must be pressed with the accelKey.
        If there's more than one, then they should be separated with spaces.
        Available modifiers are: shift, alt, meta, control, and accel.
      • string : accessKey : A single character that can be used to jump to the command when the menu is open.

    Greasemonkey Tools

    GM.obj2url(formData)

    • Parameters:
      • object : formData : generates a get/post url string for passing data through GM.ajax.
    • Example:
    • var obj = {
              "username" : "EmptySoul",
              "amount" : 1000,
              "function" : "store"
          }
          obj2url(obj) = "username=EmptySoul&amount=1000&function=store
      GM.ajax({
              method: "POST",
              url: "http://www.example.com/login",
              data: GM.obj2url(obj),
              headers: GM.ajaxHeaders,
              onload: function(response) {
                  alert(response.responseText);
              }
          });

    GM.typeCheck(value, type)

    • Parameters:
      • key : string : name of the key to retrieve the value
      • value : string/boolean/int : value to be stored in persistent memory
    • Returns:
      • boolean : true/false : after comparing if (typeof(value) == type);
    • Example:
    • var value = (5 * 8);
          var return = GM.typeCheck(value, 'number'); => true
          var return = GM.typeCheck(value, 'string'); => false

    GM.addCommas(number)

    • Parameters:
      • number : integer : integer to be output as formatted string.
    • Returns:
      • string : integer formatted with commas
    • Example:
    • GM.addCommas(1234567890); => "1,234,567,890"

    GM.timer

    • Object for timing script execution
    • Methods:
      • start : Starts the timer.
      • stop : Stops the timer.
    • Fields:
      • duration : float : amount of elapsed time, in seconds.
    • Example:
    • GM.timer.start();
      function(){ ... time consuming function ... }
      GM.timer.stop();
      alert('elapsed time: ' + GM.timer.duration + 's'); => "elapsed time: 0.846s"

    True / False Check Object

    GM.check

    • Object for operations with checking values.
    • Methods:
      • GM.check.isUsername(value)

        • Parameters:
          • value : string : Checks if the value consists of only: letters, numbers & underscores
        • Returns:
          • boolean : true / false

      • GM.check.isEmail(value)

        • Parameters:
          • value : string : Checks if the value is a valid email address
        • Returns:
          • boolean : true / false

      • GM.check.isIP(value)

        • Parameters:
          • value : string : Checks if the value is a valid IP address
        • Returns:
          • boolean : true / false

      • GM.check.inURL(value)

        • Parameters:
          • value : string : Checks if the value is in the current page's URL
        • Returns:
          • boolean : true / false

      • GM.check.isPhoneNum(value)

        • Parameters:
          • value : string : Checks if the value is a valid phone number
        • Returns:
          • boolean : true / false