theDeSilva.com Andrew de Silva

Greasemonkey – Add button to page to call function within GreaseMonkey

January 21st, 2010 · No Comments · Javascript

Here’s a really quick way to add a button into an existing page and then have the button call a function that is within greasemonkey script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
window.addEventListener("load", function(e) {
  addButton();
}, false);
 
function addButton(){
 var buttonElems = document.getElementsByTagName('buttonElementName');
 buttonElems[0].innerHTML = buttonElems[0].innerHTML + '<input id="greasemonkeyButton" type="button" value="Call Greasemonkey Function" />'
addButtonListener();
}
 
function addButtonListener(){
  var button = document.getElementById("greasemonkeyButton");
  button.addEventListener('click',doMonkey,true);
}
 
function doMonkey(){
	//do something
}
Share and Enjoy:
  • Digg
  • StumbleUpon
  • del.icio.us
  • Reddit
  • Technorati
  • Facebook
  • Google Bookmarks
  • LinkedIn

Tags: ····