Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

GreaseMonkey usefull APIs

Posted on 16/10/2010

Many of the GreaseMonkey users are not familiar with the very useful set of APIs it has to offer. These functions can help you, especially if you are using it for large projects.
I will show here a few of the more useful APIs to my taste, you can read the entire API reference in the product wiki.

Logging


  • A very useful function is GM_log().
  • It is used to print log messages into the JavaScript console.

Example:

GM_log("x="+x);

Storing data locally


  • Use the functions GM_setValue(), GM_getValue(), GM deleteValue() and GM_listValues() to store and retract data on the client side.
  • This data is located in the Firefox preferences mechanism.

Example:

var x =  GM_getValue("x","default value");
GM_log("x="+x);
GM_setValue("x","new value");

Adding menu commands


  • Another cool option is being able to add commands to be triggered from Firefox menu.
  • To do that use the function GM_registerMenuCommand().
  • This adds a new item under “Tools->Greasemonkey->User Script Commands”

Example:

function menuItemClicked(){
       alert("menuItemClicked");
}
GM_registerMenuCommand("aviTest", menuItemClicked);

Ajax calls


  • Use GM_xmlhttpRequest to make ajax calls very easily.

Example:

GM_xmlhttpRequest({
  method: "GET",
  url: "http://www.example.net/",
  headers: {
    "User-Agent": "Mozilla/5.0",    // If not specified, navigator.userAgent will be used.
    "Accept": "text/xml"            // If not specified, browser defaults will be used.
  },
  onload: function(response) {
    // Inject responseXML into existing Object if not present
    if (!response.responseXML) {
      response.responseXML = new DOMParser()
        .parseFromString(response.responseText, "text/xml");
    }

    GM_log([
      response.status,
      response.statusText,
      response.readyState,
      response.responseHeaders,
      response.responseText,
      response.finalUrl,
      response.responseXML
    ].join("\n"));
  }
});

Further reading

  • The full API reference

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
1d96f52e7159fe09c7a3dd2a9816d166-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme