-
Extension Help
I don't normally use javascript, but I'm writing a script for greasemonkey to extend a specific page on stumbleupon.com.
Basically, I've just started, and I'm trying to add a textbox to the page.
Code:
// ==UserScript==
// @author Francis Stokes
// @name Recent Shares Ext.
// @namespace rse
// @description Extension to the stanndard Recent Shares Page
// @include http://*.stumbleupon.com/shares/
// ==/UserScript==
var tb = document.createElement('input');
tb.setAttribute("type", "text");
var f = document.forms[0];
f.appendChild(tb);
No textbox in sight. I don't think Im doing it right.
Any corrections would be helpful.
-
Re: Extension Help
Hi there francisstokes,
the probable reason for it not working is that you have not allowed the page to load before calling the script.
This means that document.forms[0] does not as yet exist.
Also, for the page to validate you need your form to to be coded either like this...
Code:
<form action="#">
<div id="myform">
</div>
</form>
...or this...
Code:
<form action="#">
<fieldset>
<legend></legend>
</fieldset>
</form>
For the first option use this script...
Code:
<script type="text/javascript">
if(window.addEventListener){
window.addEventListener('load',mytest,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',mytest);
}
}
function mytest(){
// ==UserScript==
// @author Francis Stokes
// @name Recent Shares Ext.
// @namespace rse
// @description Extension to the stanndard Recent Shares Page
// @include http://*.stumbleupon.com/shares/
// ==/UserScript==
tb=document.createElement('input');
tb.setAttribute('type','text');
f=document.getElementById('myform');
f.appendChild(tb);
}
</script>
...and the second use this...
Code:
<script type="text/javascript">
if(window.addEventListener){
window.addEventListener('load',mytest,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',mytest);
}
}
function mytest(){
// ==UserScript==
// @author Francis Stokes
// @name Recent Shares Ext.
// @namespace rse
// @description Extension to the stanndard Recent Shares Page
// @include http://*.stumbleupon.com/shares/
// ==/UserScript==
tb=document.createElement('input');
tb.setAttribute('type','text');
f=document.forms[0][0];
f.appendChild(tb);
}
</script>