Results 1 to 2 of 2

Thread: Extension Help

  1. #1

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    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.

  2. #2
    Hyperactive Member coothead's Avatar
    Join Date
    Oct 2007
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    285

    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>
    


    ~ the original bald headed old fart ~

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width