Inject javascript in XML file via content script











up vote
1
down vote

favorite












I know xml shouldn't really have javascript in it but I'm making a browser extension that needs to inject some code on every page to provide the user with some interesting statistics.



The problem is, I'm unable to get the injected code to run in XML documents as it looks like the script tag I create is treated as text?



The same code works fine in HTML



Here's what I have:



manifest.json:



"content_scripts": [
{
"matches": ["<all_urls>"],
"all_frames": true,
"match_about_blank": true,
"run_at": "document_start",
"js": ["scripts/content/countCalls.js"]
}
]


countCalls.js:



let injectedCode = 'console.log("Hello from injected code");';
let script = document.createElement('script');
script.appendChild(document.createTextNode(injectedCode));

let parent = document.head || document.body || document.documentElement;
let firstChild = (parent.childNodes && (parent.childNodes.length > 0)) ? parent.childNodes[0] : null;
if (firstChild) {
parent.insertBefore(script, firstChild);
} else {
parent.appendChild(script);
}


test.xml:



<?xml version="1.0"?>
<main xmlns='http://www.w3.org/1999/xhtml'>
<script>
console.log("Hello from XML");
</script>
</main>


When I load test.xml in the browser I see this:




console.log("Hello from injected code");




and in the console I just see this:




Hello from XML




So the injected code is being treated like text. Anyway around this? Otherwise intentionally adding javascript to xml will cause it to bypass the extension










share|improve this question






















  • @wOxxOm To clarify, the script inside the XML is working without any extra attributes. The problem is in the injected code. I've added the xmlns to the injected script tag but still not luck
    – Cornwell
    Nov 11 at 11:01










  • @wOxxOm sorry, I missed the createElementNS in your comment. That did the trick! Thank you. If you create an answer from it, I will accept it
    – Cornwell
    Nov 11 at 14:38















up vote
1
down vote

favorite












I know xml shouldn't really have javascript in it but I'm making a browser extension that needs to inject some code on every page to provide the user with some interesting statistics.



The problem is, I'm unable to get the injected code to run in XML documents as it looks like the script tag I create is treated as text?



The same code works fine in HTML



Here's what I have:



manifest.json:



"content_scripts": [
{
"matches": ["<all_urls>"],
"all_frames": true,
"match_about_blank": true,
"run_at": "document_start",
"js": ["scripts/content/countCalls.js"]
}
]


countCalls.js:



let injectedCode = 'console.log("Hello from injected code");';
let script = document.createElement('script');
script.appendChild(document.createTextNode(injectedCode));

let parent = document.head || document.body || document.documentElement;
let firstChild = (parent.childNodes && (parent.childNodes.length > 0)) ? parent.childNodes[0] : null;
if (firstChild) {
parent.insertBefore(script, firstChild);
} else {
parent.appendChild(script);
}


test.xml:



<?xml version="1.0"?>
<main xmlns='http://www.w3.org/1999/xhtml'>
<script>
console.log("Hello from XML");
</script>
</main>


When I load test.xml in the browser I see this:




console.log("Hello from injected code");




and in the console I just see this:




Hello from XML




So the injected code is being treated like text. Anyway around this? Otherwise intentionally adding javascript to xml will cause it to bypass the extension










share|improve this question






















  • @wOxxOm To clarify, the script inside the XML is working without any extra attributes. The problem is in the injected code. I've added the xmlns to the injected script tag but still not luck
    – Cornwell
    Nov 11 at 11:01










  • @wOxxOm sorry, I missed the createElementNS in your comment. That did the trick! Thank you. If you create an answer from it, I will accept it
    – Cornwell
    Nov 11 at 14:38













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I know xml shouldn't really have javascript in it but I'm making a browser extension that needs to inject some code on every page to provide the user with some interesting statistics.



The problem is, I'm unable to get the injected code to run in XML documents as it looks like the script tag I create is treated as text?



The same code works fine in HTML



Here's what I have:



manifest.json:



"content_scripts": [
{
"matches": ["<all_urls>"],
"all_frames": true,
"match_about_blank": true,
"run_at": "document_start",
"js": ["scripts/content/countCalls.js"]
}
]


countCalls.js:



let injectedCode = 'console.log("Hello from injected code");';
let script = document.createElement('script');
script.appendChild(document.createTextNode(injectedCode));

let parent = document.head || document.body || document.documentElement;
let firstChild = (parent.childNodes && (parent.childNodes.length > 0)) ? parent.childNodes[0] : null;
if (firstChild) {
parent.insertBefore(script, firstChild);
} else {
parent.appendChild(script);
}


test.xml:



<?xml version="1.0"?>
<main xmlns='http://www.w3.org/1999/xhtml'>
<script>
console.log("Hello from XML");
</script>
</main>


When I load test.xml in the browser I see this:




console.log("Hello from injected code");




and in the console I just see this:




Hello from XML




So the injected code is being treated like text. Anyway around this? Otherwise intentionally adding javascript to xml will cause it to bypass the extension










share|improve this question













I know xml shouldn't really have javascript in it but I'm making a browser extension that needs to inject some code on every page to provide the user with some interesting statistics.



The problem is, I'm unable to get the injected code to run in XML documents as it looks like the script tag I create is treated as text?



The same code works fine in HTML



Here's what I have:



manifest.json:



"content_scripts": [
{
"matches": ["<all_urls>"],
"all_frames": true,
"match_about_blank": true,
"run_at": "document_start",
"js": ["scripts/content/countCalls.js"]
}
]


countCalls.js:



let injectedCode = 'console.log("Hello from injected code");';
let script = document.createElement('script');
script.appendChild(document.createTextNode(injectedCode));

let parent = document.head || document.body || document.documentElement;
let firstChild = (parent.childNodes && (parent.childNodes.length > 0)) ? parent.childNodes[0] : null;
if (firstChild) {
parent.insertBefore(script, firstChild);
} else {
parent.appendChild(script);
}


test.xml:



<?xml version="1.0"?>
<main xmlns='http://www.w3.org/1999/xhtml'>
<script>
console.log("Hello from XML");
</script>
</main>


When I load test.xml in the browser I see this:




console.log("Hello from injected code");




and in the console I just see this:




Hello from XML




So the injected code is being treated like text. Anyway around this? Otherwise intentionally adding javascript to xml will cause it to bypass the extension







javascript xml google-chrome-extension firefox-addon firefox-webextensions






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 10:35









Cornwell

1,69433553




1,69433553












  • @wOxxOm To clarify, the script inside the XML is working without any extra attributes. The problem is in the injected code. I've added the xmlns to the injected script tag but still not luck
    – Cornwell
    Nov 11 at 11:01










  • @wOxxOm sorry, I missed the createElementNS in your comment. That did the trick! Thank you. If you create an answer from it, I will accept it
    – Cornwell
    Nov 11 at 14:38


















  • @wOxxOm To clarify, the script inside the XML is working without any extra attributes. The problem is in the injected code. I've added the xmlns to the injected script tag but still not luck
    – Cornwell
    Nov 11 at 11:01










  • @wOxxOm sorry, I missed the createElementNS in your comment. That did the trick! Thank you. If you create an answer from it, I will accept it
    – Cornwell
    Nov 11 at 14:38
















@wOxxOm To clarify, the script inside the XML is working without any extra attributes. The problem is in the injected code. I've added the xmlns to the injected script tag but still not luck
– Cornwell
Nov 11 at 11:01




@wOxxOm To clarify, the script inside the XML is working without any extra attributes. The problem is in the injected code. I've added the xmlns to the injected script tag but still not luck
– Cornwell
Nov 11 at 11:01












@wOxxOm sorry, I missed the createElementNS in your comment. That did the trick! Thank you. If you create an answer from it, I will accept it
– Cornwell
Nov 11 at 14:38




@wOxxOm sorry, I missed the createElementNS in your comment. That did the trick! Thank you. If you create an answer from it, I will accept it
– Cornwell
Nov 11 at 14:38












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Simply create the script DOM element with an XML-compatible namespace:



let script = document.createElementNS('http://www.w3.org/1999/xhtml', 'script');





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247884%2finject-javascript-in-xml-file-via-content-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Simply create the script DOM element with an XML-compatible namespace:



    let script = document.createElementNS('http://www.w3.org/1999/xhtml', 'script');





    share|improve this answer

























      up vote
      2
      down vote



      accepted










      Simply create the script DOM element with an XML-compatible namespace:



      let script = document.createElementNS('http://www.w3.org/1999/xhtml', 'script');





      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        Simply create the script DOM element with an XML-compatible namespace:



        let script = document.createElementNS('http://www.w3.org/1999/xhtml', 'script');





        share|improve this answer












        Simply create the script DOM element with an XML-compatible namespace:



        let script = document.createElementNS('http://www.w3.org/1999/xhtml', 'script');






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 16:00









        wOxxOm

        25.8k34461




        25.8k34461






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247884%2finject-javascript-in-xml-file-via-content-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Jo Brand

            Thiostrepton