Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts

    GM_registermenucommand Help

    Does anyone happen to know enough about Greasemonkey to be able to add a preferences window in the 'user scripts commands' menu? I have this simple code below where I give the user the option of omitting elements on a web page. I want to be able create a menu that will toggle the options on and off. I did a lot research in to GM_registermenucommand, but I just couldn't make head or tails of it. Thanks.

    Code:
    // @include        *fantasysports.yahoo.com*
    // ==/UserScript==
    
    //Removes ad on right column of league homepage
    (function(){
    
    a = document.getElementById( 'gamepromo' );
    if ( a )
    	a.parentNode.removeChild( a );
    
    })();
    
    //Removes the Ad Choice from the right column
    (function(){
    
    a = document.getElementsByClassName( 'ad_slug_table' );
    if ( a[0] )
    	a[0].parentNode.removeChild( a[0] );
    
    })();
    
    //Removes ad on right column of league homepage
    (function(){
    
    a = document.getElementById( 'yspadLREC' );
    if ( a )
    	a.parentNode.removeChild( a );
    
    })();

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    GM_registerMenuCommand is quite easy to use.

    Code:
    var toggleValue = true;
    
    GM_registerMenuCommand('Menu text', methodToInvoke);
    ...
    function methodToInvoke() {
       // this will be executed on click of the menu command
       // toggle a value here
       toggleValue = !toggleValue;
    }

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks for the feedback.

    What I am looking to do is add a preferences window that will pop up and allow the user to check/uncheck options and then the page refreshes. How might I go about doing that in the code?

    Thanks.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You can take one of many existing userscripts for that purpose, e.g. my script here :-)

  • Users who have thanked devnull69 for this post:

    bszen27 (09-20-2011)

  • #5
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Love your script by the way, devnull69.

    So let's say you take one of my functions that I want to be able to turn off/on, then refresh:

    Code:
    (function(){
    
    a = document.getElementById( 'gamepromo' );
    if ( a )
    	a.parentNode.removeChild( a );
    
    })();
    How do I exactly apply it to your script example? Sorry, I am a bit of newbie.

    Code:
    USP.theScriptName = 'Testscript';
    USP.init({theName:'OneOfMany', theText:'Which values should be displayed?', 
                theValues:['All','None'], theDefault:'All'},
             {theName:'intValue', theText:'Integer:', theDefault:100},
             {theName:'stringValue', theText:'String:', theDefault:'Testvalue'},
             {theName:'boolValue', theText:'Boolean?', theDefault:true}
        );

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Code:
    // this is the call for the preferences window
    USP.init({theName:'func1', theText:'Execute function 1?', theDefault:true});
    
    
    // this is what you should use to react on the preference setting
    if(USP.getValue('func1')) {
       // this code will be executed only if the preference setting "func1" is true
       a = document.getElementById( 'gamepromo' );
       if ( a )
    	a.parentNode.removeChild( a );   
    }

  • Users who have thanked devnull69 for this post:

    bszen27 (09-21-2011)

  • #7
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I put the following code into my program and I keep getting USP.valueList is null. Your script address is in @require.

    Code:
    window.addEventListener(
      'load',
      function () {
        // The type of 'theDefault' determines the type of the preference value
    
    	USP.init({theName:'OneOfMany', theText:'Which values should be displayed?', theValues:['All','None'], theDefault:'All'},
    	         {theName:'OneOfMany2', theText:'Yet another question?', theValues:['Yes','No'], theDefault:'Yes'},
    	         {theName:'stringValue', theText:'String:', theDefault:'Testvalue'},
    			 {theName:'func1', theText:'Execute function 1?', theDefault:true},  //my function
    	         {theName:'boolValue', theText:'Boolean?', theDefault:true}
     	);
    	GM_registerMenuCommand('Preferences for ~'+USP.theScriptName+'~', USP.invoke);
    	
      },
    true);
    
    if(USP.getValue('func1')) {
       // this code will be executed only if the preference setting "func1" is true
       a = document.getElementById( 'ft' );
    if ( a )
    	a.parentNode.removeChild( a );
    }

  • #8
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Then there is something wrong with the way you used @require. I tried this one and it worked perfectly (I used "func4" to make sure that the key didn't exist yet)
    Code:
    // @require        http://userscripts.org/scripts/source/45988.user.js
    // ==/UserScript==
    
    window.addEventListener(
      'load',
      function () {
        // The type of 'theDefault' determines the type of the preference value
    
    	USP.init({theName:'OneOfMany', theText:'Which values should be displayed?', theValues:['All','None'], theDefault:'All'},
    	         {theName:'OneOfMany2', theText:'Yet another question?', theValues:['Yes','No'], theDefault:'Yes'},
    	         {theName:'stringValue', theText:'String:', theDefault:'Testvalue'},
    			 {theName:'func4', theText:'Execute function 3?', theDefault:true},  //my function
    	         {theName:'boolValue', theText:'Boolean?', theDefault:true}
     	);
    	GM_registerMenuCommand('Preferences for ~'+USP.theScriptName+'~', USP.invoke);
       alert(USP.getValue('func4'));
      },
    true);
    
    if(USP.getValue('func4')) {
       // this code will be executed only if the preference setting "func1" is true
       alert('xyz');
    }

  • #9
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I have this exact code in my script and I get absolutely nothing from it when testing it on Bing (or any other site). I continue to get USP.valueList is null form line 180 of your script. Am I missing something? Does the file need to be paired with mine somehow (besides @require)?

    Code:
    // ==UserScript==
    // @name          	Test Script
    // @include		*bing.com*
    // @require        http://userscripts.org/scripts/source/45988.user.js
    
    // ==/UserScript==
    
    // Uncomment for testing purposes
    
    window.addEventListener(
      'load',
      function () {
        // The type of 'theDefault' determines the type of the preference value
    
    	USP.init({theName:'OneOfMany', theText:'Which values should be displayed?', theValues:['All','None'], theDefault:'All'},
    	         {theName:'OneOfMany2', theText:'Yet another question?', theValues:['Yes','No'], theDefault:'Yes'},
    	         {theName:'stringValue', theText:'String:', theDefault:'Testvalue'},
    			 {theName:'func4', theText:'Execute function 3?', theDefault:true},  //my function
    	         {theName:'boolValue', theText:'Boolean?', theDefault:true}
     	);
    	GM_registerMenuCommand('Preferences for ~'+USP.theScriptName+'~', USP.invoke);
       //alert(USP.getValue('func4'));
      },
    true);
    
    if(USP.getValue('func4')) {
       // this code will be executed only if the preference setting "func1" is true
       alert('xyz');
    }

  • #10
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Hm, I'm sorry but this exact script (literally) works fine for me. No errors and the preference window is showing up normally.

    Two questions:
    1. Do you use the latest version of Greasemonkey?
    2. Did you try to uninstall the script and reinstall it from scratch? "Some time back" Greasemonkey had problems with @require if the @require line was edited after the script has already been installed ...

  • #11
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    The preference window comes up fine. It just doesn't show an alert box no matter what I do with the evaluation function checkbox.

    I have the latest Greasemonkey and run Firefox 3.6. I also re-installed the script just to make sure.

  • #12
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Can you post the full script here? We can take a look at it then ...


  •  

    Posting Permissions

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