...

View Full Version : GM_registermenucommand Help



bszen27
09-19-2011, 07:40 PM
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.



// @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 );

})();

devnull69
09-19-2011, 10:53 PM
GM_registerMenuCommand is quite easy to use.



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;
}

bszen27
09-20-2011, 12:04 AM
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.

devnull69
09-20-2011, 08:11 AM
You can take one of many existing userscripts for that purpose, e.g. my script here (http://userscripts.org/scripts/show/45988) :-)

bszen27
09-20-2011, 03:57 PM
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:


(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.


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}
);

devnull69
09-20-2011, 06:17 PM
// 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 );
}

bszen27
09-21-2011, 12:59 AM
I put the following code into my program and I keep getting USP.valueList is null. Your script address is in @require.


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 );
}

devnull69
09-21-2011, 08:09 AM
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)


// @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');
}

bszen27
09-22-2011, 12:21 AM
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)?


// ==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');
}

devnull69
09-22-2011, 09:14 AM
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 ...

bszen27
09-24-2011, 03:10 PM
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.

devnull69
09-25-2011, 12:44 PM
Can you post the full script here? We can take a look at it then ...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum