Theme Graphic
Theme Graphic

Jonesy's Great Code Archive

This is for me, not you. But I hope you find it useful none-the-less. Suggestions or general comments are appreciated.
Posted on Thursday, November 19, 2009 at 9:15 PM

GreaseMonkey script: Add button to page and bind onclick function

Here is an example of how to add a button to a page at a specific location and then have it call a function onclick.

// Inserts javascript that will be called by the autoCheckOrderButton
var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.innerHTML =  'function checkForOrders() { alert("inside checkForOrders"); }';
document.getElementsByTagName("head")[0].appendChild(scriptElement);
window.addButton = function () {
	// Get the location on the page where you want to create the button
	var targetDiv = document.getElementById('offer');
	
	// Create a div to surround the button
	var newDiv = document.createElement('div');
	newDiv.setAttribute('id', 'autoCheckOrder');
	
	// Create the button and set its attributes
	var inputButton = document.createElement('input');
	inputButton.name = 'autoCheckOrderButton';
	inputButton.type = 'button';
	inputButton.value = 'Start Checkin';
	inputButton.setAttribute("onclick", "checkForOrders();");...
Comments: 2 Tags: JavaScript, GreaseMonkey

 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.