JavaScript alert not working in Firefox 6

前端 未结 3 1817
小鲜肉
小鲜肉 2020-11-29 06:01

I tried running this JavaScript code in the address bar in Firefox 6:

javascript:alert(\"Hello\")

I get a

Referenc

3条回答
  •  情歌与酒
    2020-11-29 06:28

    If your clickable bookmarklet got broken and you want it back, you can create a clickable button instead using Custom Buttons Firefox extension.

    The advantages of button over running from Scratchpad:

    • you can actually save the bookmarklet (button),
    • you can have a nice own icon (create some image e.g. PNG file, import it and base64_encode it inside the new button dialog).

    The extension is a bit special because the buttons run at Firefox chrome level, so they're a bit more privileged (you can interact with the browser's API), and there's no 1-to-1 correspondence between normal JS and the button code (it needs some tweaking). More precisely, document and window seen from button are not the ones you were expecting.

    However, you can assign the 'good' window and document to your variables, and then work on these variables instead (better not redefine window;)

    Here's a sample code I written which works pretty well in Fx10:

    // get proper 'window' and 'document' into our private variables
    var theWindow = window.top.getBrowser().selectedBrowser.contentWindow;
    var theDocument = theWindow.document;
    
    // here we go
    var input = theDocument.getElementById("foo");
    input.focus(); // just to show you it's working, unnecessary in fact
    
    // simulate keyboard event
    var evt = theDocument.createEvent("KeyboardEvent");
    evt.initKeyEvent ("keypress", true, true, theWindow ,
                0, 0, 0, 0, 0, 65); // 65 == "A"
    input.dispatchEvent(evt);
    
    // show alert after 2 sec
    theWindow.setTimeout( function(){
      input.value += "B";
      theWindow.alert(input.value); // alerts "AB"
    },2000);
    

    Instead of using global functions directly (like setTimeout, or alert), you have to put theWindow. before them, and replace document/window with local theDocument/theWindow and it's seems to be working. I haven't tested it thoroughly however with very complicated cases.

    To add a button, right click on any button you already have and choose 'Add new button...'.

提交回复
热议问题