Search code examples
javascriptgoogle-chromebookmarklet

JS: Bookmarklet - Create a bookmarklet that fires a local js file to fill up fields in a form of the current page


I'm trying to create a bookmarklet that fills up fields on the form of my current page.

Here's a simple script that fills up 1 field with a given value:

javascript:(function(){d=document;e=d.getElementById("ember469");e.value="dummy3@restricted.com";e.blur();})();

This is working properly if I store is as a bookmark. Now I want to store this JS on a local file in my machine because I'd like to fill up X number of fields and I dont want to maintain the code from within the bookmarklet.

I tried creating a bookmarklet that calls my local js "test.js" using:

javascript:(function(){var js=document.createElement('script');js.setAttribute('src', 'C:/Users/path/to/my/sample.js');document.body.appendChild(js);}());

I got this idea from these links:
Link 1
Link 2

Here's my sample.js contents:

function(){d=document;e=d.getElementById("ember469");e.value="dummy3@restricted.com";e.blur();}

I've looked around SO but I can't find a sample that loads a local js, most samples are loading an online js file.

I'm unable to make this work. What am I missing?

Any help is appreciated!!

UPDATE: Based on @Shugar's answer below, I've updated both my sample.js and bookmarklet. I can see the script tag getting inserted on the page but that script doesnt fire. The field isn't getting populated.

enter image description here


Solution

  • I hope the following way will work for you. (Just edited the posted code a little.)


    Bookmarklet:

    javascript: (function() {
        var js = document.createElement('script');
        js.setAttribute('src', 'C:/Users/path/to/my/sample.js');
        document.body.appendChild(js);
    })();
    


    sample.js:

    (function() {
        d = document;
        e = d.getElementById("ember469");
        e.value = "dummy3@restricted.com";
        e.blur();
    })();