2

Background of the story:

  • I have several web pages in which I need to complete data for a lot of input fields;

  • for not losing time (while testing), I have a JavaScript file that helps me (I'm using it in an e2e test);

  • I created a bookmark in my browser, in which I added this as URL:

enter image description here

URL VALUE: javascript:(function()document.body.appendChild(document.createElement('script')).src='../path_to_another_js_file.js')();
  • this path src='../path_to_another_js_file.js' will open a js file with the next content:
(function ($) {
    var fieldData = {
        "ContactData.EMail": "[email protected]",
        "ContactData.EMailConfirmation": "[email protected]",
        etc...
    };
})(jQuery);

ISSUE:

I want to eliminate the src='../path_to_another_js_file.js' from the bookmark and instead of that path, to pass the function from this external file as string, like that:

javascript:(function()document.body.appendChild(document.createElement('script'))
.textContent(String(
    (function ($) {
        var fieldData = {
            "ContactData.EMail": "[email protected]",
            "ContactData.EMailConfirmation": "[email protected]",
            //etc...
        };
    })(jQuery);
))})();

I can't do this any under circumstances, because this won't get executed/ it won't even create the js script in my FE, due some some syntax issues.

Did any of you encountered by any chance the same case I have here?? 10X

4
  • user a userscript and Tampermonkey instead Commented Sep 6, 2019 at 11:09
  • none of them are an option, for the moment. I must make this work, because I'm using it in an automated e2e test script :) Commented Sep 6, 2019 at 11:44
  • The src attribute of the script element is specifically for pointing to a URL. You can't put anything you want in there. If you want a bookmarklet, write it such that it does not need to be in a script element. Commented Sep 6, 2019 at 11:53
  • ok, then consider I'm using textContent(), or something else. the issue is the same one. Or maybe point to a method/way that would allow me to do use this as a autofillscript by adding like this, in a bookmark. Commented Sep 6, 2019 at 12:02

2 Answers 2

1

Your code should look like this:

javascript:(function ($) {
    var fieldData = {
        "ContactData.EMail": "[email protected]",
        "ContactData.EMailConfirmation": "[email protected]",
        etc...
    };
})(jQuery);

no need for script tag. Also you had error missing curly braces in main function of bookmarkelet.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, the syntax issues are gone this way BUT the completion of the input fields, is not performed anymore. so, I guess I must make it work while also having the script tag :)
@Eugen no you don't have to, but the the code that do that thing need to be in bookmarklet. It can be just variable with object, it need to do something. Unless this object need to be global variable then you need window.fieldData = {...}
0

You’re close, but there are some syntax issues. This should be the same as the script tag you have now:

javascript:(function(){
    var el = document.createElement('script');
    el.innerHTML = `(function ($) {
    var fieldData = {
        "ContactData.EMail": "[email protected]",
        "ContactData.EMailConfirmation": "[email protected]",
        //etc...
    };
})(jQuery);`;
    document.body.appendChild(el);
})();

2 Comments

great, your example sshould fit perfect; but, as you said, there are some syntax issue; Could you maybe modify the example and try to get rid of those syntax issues? I can't seem to match the right ones ...
the problem is I will always have either ["] either ['], that will somehow tell js that the string will end there, which is not...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.