userscript: ¿Perdí algo de JS?
Frecuentes
Visto 106 veces
0
I do not understand what's going on...
I have a simple userscript, that add couple DIVs, css styles and JS functions in the pages I visit
In particular, I have one DIV that trigger a JS function with a onClick listener - this function is a "toggle" function (display/hide an other DIV):
function togglegm(etat) {
if (etat = 'on') {
document.getElementById('greasemky').style.display = 'block';
document.getElementById('greasemkytoggle').innerHTML = '<a href="javascript:return(false);" onClick="togglegm(\'off\');"></a>';
} else if (etat = 'off') {
document.getElementById('greasemky').style.display = 'none';
document.getElementById('greasemkytoggle').innerHTML = '<a href="javascript:return(false);" onClick="togglegm(\'on\');"></a>';
}
}
var script2 = d.createElement('script');
script2.appendChild(d.createTextNode(togglegm));
(d.body || d.head || d.documentElement).appendChild(script2);
The DIV "greasemkytoggle" only contains a link with a onClick that trigger "togglegm('on'), and my objective is that when togglegm(on) is executed, the innerHTML of this DIV becomes a trigger for togglegm(off).
Now the weird part... when I click on my DIV greasemkytoggle, the function togglegm(on) is perfectly executed (greasemky is displayed), and the innerHTML is perfectly changed with a link for "togglegm(off)", BUT if I click again, then nothing happens.
I looked at the source code, and discovered that my JS function just disappeared (that's why nothing happened on the last click)! Now, there is an empty function replacing my togglegm():
<script>
scriptHolderArray1
</script>
Do you understand that kind of behaviour...?
I found nothing online for that kind of situation...
2 Respuestas
1
GreaseMonkey runs under a much more security conscience set of rules.
Attach the event listeners using the proper DOM3 (addEventListener) method.
It is never a good idea (in user scripts or general scripting) to assign Javascript through innerHTML.
It is never a good idea to use the "javascript:" pseudo-protocol.
Respondido 24 ago 12, 21:08
Thank Jeremy, you gave my an idea... I will try an other solution - jrm
0
Los problemas son etat = 'on'
y etat = 'off'
.
If you want to set values, use
etat = 'on'
etat = 'off'
If you want to compare, use:
etat == 'on'
etat == 'off'
Por otro lado, href="javascript:return(false);"
throws an error on Firefox because there is a return outside a function (SyntaxError: return not in function). You should do href="javascript:void(0);"
o return false
at the end of the onclick event.
Anyway, I don't understand very well what you are doing here:
var script2 = d.createElement('script');
script2.appendChild(d.createTextNode(togglegm));
(d.body || d.head || d.documentElement).appendChild(script2);
Tienes una función togglegm
loaded to browser's memory by a <script>
elemento.
Then, you create a new <script>
with that function and append it to the document, in order to load it to browser's memory again (I guess).
¿Por qué?
Respondido 24 ago 12, 21:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas javascript userscripts or haz tu propia pregunta.
Those last three lines - what are they supposed to do? - Viktor S.
Thank you all, I find a solution! - jrm