greasemonkey specific javascript irregularities

B

bosky101

alright ive heard a lot about the onclick being needing to be replaced
with click ,and some other healthy practices like that . but im still
not able to figure out the problem . my click event is not triggered .
no error shown in javascript console as well . any idea ?

// JavaScript Document
window.addEventListener("load", function() {
var i=0;
draw();

function draw(){
var t = document.createElement("div");
t.innerHTML = "<div id=\"div1\" style=\"margin: 0 auto 0
auto;\"><input name=\"submit\" type=\"submit\" id='btnStart'
value=\"Start\"></div>";
document.body.insertBefore(t, document.body.firstChild);
document.getElementById('btnStart').setAttribute("click",
function()
{start()},false);
}

function start(){
alert('started');
}

},false);

the div1 is shown, but events are not triggered . sometimes i get a
funciton not defined as well.

Keep Clicking,
Bosky
 
M

Martin Honnen

document.getElementById('btnStart').setAttribute("click",
function()
{start()},false);


I think you want
document.getElementById('btnStart').addEventListener("click",
function()
{start()},false);

Using setAttribute with a function argument and a third argument does
not make sense with Mozilla/Firefox and Greasemonkey scripts.
 
R

Rob Williscroft

wrote in in
comp.lang.javascript:
alright ive heard a lot about the onclick being needing to be replaced
with click
[snip]

document.getElementById('btnStart').setAttribute("click",
function()
{start()},false);


document.getElementById('btnStart').addEventListener(
"click",
function() {start()},
false
);

The event is called "click" the attribute is called "onclick".

Either of:

document.getElementById('btnStart').onclick = function() {start()};
document.getElementById('btnStart').setAttribute(
"onclick",
"alert('started');"
);

Will also work.
}

function start(){
alert('started');
}

},false);

Rob.
 
B

bosky101

now ive come across one more problem .the dreaded 'permissions' denial
..

i have the folowing two lines appended in prefs.js:
user_pref("capability.principal.codebase.p0.granted",
"UniversalBrowserRead");
user_pref("signed.applets.codebase_principal_support", true);

the ajax calls in my greasemonkey code goes as follows :

if (window.XMLHttpRequest)
{ //FF
setSecurity();
req = new XMLHttpRequest();
[snip]
..
..
..

[/snip]
setSecurity();
req.open("GET", dest, true);
[snip/]

function setSecurity(){
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
}
catch (e) {
alert('Permission UniversalBrowserRead denied.');
}
}

now when i embed this code in html it works well. i still get the
Permission UniversalBrowserRead denied. message (exception caught) .
where am i going wrong ?

Thanks again ...
- Bhasker
 
B

bosky101

1) actaully i seem to be getting a 'netscape is not defined' message in
my exception. bugzilla.mozilla.org/show_bug.cgi?id=35116 says that
changing prefs.js ,and then restarting firefox will give the 'netscape
is not defined' message. but i kept restarting FF . still no luck.

2)searching on the 'right' methods to apply privileges in firefox (
without signing ),i came across
http://almaer.com/blog/archives/cat_tech.html that says that :

function securePrivilege(priv) {
// insert the try/catch code from above, plus anything for the
other browsers
}

and then calling securePrivilege('UniversalBrowserRead') WILL NOT WORK
at all !... the privilege that you secure, is only applied in that
scope. This means that the priviledge is only even around INSIDE
securePrivilege(priv), and is thus useless " .... IS this true ?

2) does that mean that the FOLLOWING also will not work since its in a
if/else scope?! :

if (typeof netscape != 'undefined' && typeof netscape.security
!='undefined') {
try
{
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');}catch(e){alert(e.message);}
}

else{ alert(e.message);}
req.open("GET", dest, true);
..
..

3) i also tried avoiding the priveleges line ,and simply depending on
appending a user_pref("signed.applets.codebase_principal_support",
true); in prefs.js . it STILL dint work .

4)most of the tutorials and guides give incomplete implementations and
just say add a line ,and presto! it shud work . does anyone have a
working example including the xmlhttprequest calls ,and WHERE EXACTLY
to call the privilege setting lines ?

Keep Clicking,
bosky
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top