problem with "GLOBAL" onclick handler JS/ggreasemonkey

S

sammy

I have finaly got the GM_HTTP handlers to work but I still have a
problem with calling a global function as onclick handler ( for radio
button ).

The error says: "nrun is not defined"
In desperation I have tried to prepend "this." and "namespace."
but can't figure it out.
Although I am not going to call this one function from elsewhere,
I'd rather not paste the whole text of the function in the onclick quotes.

Thanks in advance.

<code>

// ==UserScript==
// @name page reader greasemonkey script
// @author Sambo
// @description insert some text on some page
// @include http://olg.ca/*
// @namespace sams_cluster
// @email (e-mail address removed)
// @version 0.1
// ==/UserScript==

var SCRIPT = {
url : 'file:///home/public/devel/linux/js/greasemonkey/sams_first.user.js',
version : '0.1' //same value as @version
};

//set global variables
var server = location.hostname;
var rootPath = "http://" + server + "/";
//var lang = new Array();
//var dom = new DOMUtils();

var raw = true;
var command_done = false;
var commands = ""

function response_handler(responseDetails)
{
GM_log(responseDetails.responseText);
}


function command_handler(responseDetails)
{
GM_log(responseDetails.responseText);
}

function fetch_command()
{
GM_log( "sfetch_command")
obj1 = { method:"GET",
url:"http://192.168.0.6:7777/command/"+location.host,
headers:{
"User-Agent":"monkeyagent",
"Accept":"text/monkey,text/xml",

"Content-type":"application/x-www-form-urlencoded" },
onload: command_handler };
GM_xmlhttpRequest( obj1 )
command_done = true;
}

function nrun()
{
GM_log("clicked");
run_flag = GM_getValue( "Rum_sams_reader", -1 )

item = document.getElementById("samspot").firstChild
if ( run_flag==0 )
{
run_flag = 1;
GM_setValue( "Rum_sams_reader", 1 )
item.checked=true
return( "True?" )
}
else
{
run_flag = 0;
GM_setValue( "Rum_sams_reader", 0 )
item.checked=false
return( "False?" )
}
}



function main()
{
run_flag = GM_getValue( "Rum_reader", -1 )

oloc = document.getElementById("tp1")
// oparent = oloc.parentNode() !!BAD
// GM_log(oloc.innerHTML);

// now insert and handle script on/off button
var osam=document.createElement("p");
osam.setAttribute("id", "samspot" );

if (run_flag == 0)
{
osam.innerHTML='<input type="checkbox" onclick="nrun();"
checked="false">';

}
else
{
osam.innerHTML='<input type="checkbox" onclick="nrun();"
checked="true">';
}
item = document.getElementById("side_info")
if ( item != null )
{
item.appendChild(osam);
}
else
{
document.body.appendChild( osam );
}
// GM_log( document.nrun )

if ( run_flag == 0 )
{
return
}


page_data = read_page()

obj1 = { method:"POST", url:"http://192.168.0.6:7777/info",
headers:{
"User-Agent":"monkeyagent",
"Accept":"text/monkey,text/xml",

"Content-type":"application/x-www-form-urlencoded" },
data: page_data ,
onload: response_handler };

GM_xmlhttpRequest( obj1 )
setTimeout( fetch_command, 1500 )
}


function read_page()
{
var vill_htm = "";
var page_contents = location.hostname + "\r\n";
var i = 0;
var x1 = 0;

page_contents = page_contents + "Current Page: " +
location.href + "\r\n"

if ( raw == true )
{
// GM_log( document.getElementsByTagName("head")[0].innerHTML
+ "\r\n" )
n1 = document.body;

s1 = location.href;
pos1 = s1.indexOf( 'travian' );
s1 = s1.slice( pos1 );
pos1 = s1.indexOf( '/' ) + 1;
cur_page = s1.slice( pos1);
pos1 = cur_page.indexOf( '?' );
if ( pos1 != -1 )
{
cur_page = cur_page.slice( 0, pos1 )
}
page_contents = page_contents + "\r\n";
GM_log( page_contents );

page_contents = page_contents + n1.innerHTML;
}
else
{
s1 = location.href;
}
return( page_contents )
}


function get_server_time(time_htm)
{
// var str_htm = document.getElementById('tp1').innerHTM;
var result = ""
strin1 = "Server time:"

// var obj1= obj.childNodes().innerHTM;
// var spos = obj.indexOf( "Server time: ")
// var epos = obj.indexOf( "</span>"

result = result.concat( time_htm )
result = result.concat( "\r\n" )
return result;

}


if (document.body)
{
// if DOM.location.hostname == "http://s8.trunameavian.us/dorf1.php
main();
}

</code>
 
T

Thomas 'PointedEars' Lahn

sammy said:
[...] I still have a problem with calling a global function as onclick
handler ( for radio button ).

The error says: "nrun is not defined"
In desperation I have tried to prepend "this." and "namespace."
but can't figure it out.

Fear is a bad advisor.
Although I am not going to call this one function from elsewhere,
I'd rather not paste the whole text of the function in the onclick
quotes.
[...]
function nrun()
{
[...]
}

function main()
{
[...]
// now insert and handle script on/off button
var osam=document.createElement("p");
osam.setAttribute("id", "samspot" );

Replace this line with

osam.id = "samspot";
if (run_flag == 0)
{
osam.innerHTML='<input type="checkbox" onclick="nrun();"
checked="false">';
[...]

If you think about it, ihe identifier of a function defined in the
privileged Greasemonkey script must be unknown in the unprivileged event
handler attribute code. So you cannot use the `innerHTML' string to define
the event listener per the event-handler attribute. (In general, you should
avoid using `innerHTML'; in particular, if you want your DOM scripts to work
with XHTML as well.)

However, it should be possible to create an element object and add an event
listener for the `click' event of the element it represents, with DOM 2 methods:

var inp = document.createElement("input");
inp.type = "checkbox";

/* or inp.addEventListener("click", function() { nrun(); }, false); */
inp.addEventListener("click", nrun, false);

osam.appendChild(inp);

BTW: <p><input></p> does not appear to be appropriate (there is no
paragraph); use <div><input></p> or semantical markup except the `p' element
instead.


HTH

PointedEars
 
S

sammy

Thanks for the clarification, I seam to remember having problem having
my clickable item apearing all together when I was using text or image
inside a "DIV". That made me think there was some sort of problem with
it not being fully parsed making me look for some sort of refresh /
reparse function. I guess long way it is.
osam.appendChild(inp);

BTW: <p><input></p> does not appear to be appropriate (there is no
paragraph); use <div><input></p> or semantical markup except the `p' element
instead.


HTH

PointedEars

Since you stirred this up , I didn't think there was an "</p>" tag.

Does <p> have (or supposed to have ) affect on rendering?

<div> is purely DOM cotainer tag , correct?
 
S

sammy

sammy said:
Thanks for the clarification, I seam to remember having problem having
my clickable item apearing all together when I was using text or image
inside a "DIV". That made me think there was some sort of problem with
it not being fully parsed making me look for some sort of refresh /
reparse function. I guess long way it is.



Since you stirred this up , I didn't think there was an "</p>" tag.

Does <p> have (or supposed to have ) affect on rendering?

<div> is purely DOM cotainer tag , correct?

Oh, just realized why you were talking about <p>, it should have been
<div> element, but since my inserted element does not appear in "page
source" with the code I posted, it escaped me till now.
Looks like my statement about my element not apearing on the page may
have been wrong.
 
T

Thomas 'PointedEars' Lahn

sammy said:
Thanks for the clarification, I seam to remember having problem having
my clickable item apearing all together when I was using text or image
inside a "DIV". That made me think there was some sort of problem with
it not being fully parsed making me look for some sort of refresh /
reparse function. I guess long way it is.

Sorry, I don't follow. Chances are that you should (not) guess again.
osam.appendChild(inp);

BTW: <p><input></p> does not appear to be appropriate (there is no
paragraph); use <div><input></p> or semantical markup except the `p' element
instead.

Since you stirred this up , I didn't think there was an "</p>" tag.

Not good.
Does <p> have (or supposed to have ) affect on rendering?

Yes, as the `p' element marks up a *paragraph*, there is usually a margin
above and below it. CSS can, for example, indent the first line of each
paragraph.

<div> is purely DOM cotainer tag , correct?

Please read <http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.1>. (You
should probably read the entire HTML 4.01 and CSS Specifications while you
are at it. These are Web development basics; you really should not do any
Web scripting before you are well-versed in them.)

_`div'_ is a container _element_ without intrinsic meaning, so to speak.
Therefore, it should only be used where meaning is unimportant. Like
anything in (X)HTML, it does not require, but is accessible through, the DOM.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top