Inject javascript in XML file via content script
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
add a comment |
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
@wOxxOm To clarify, the script inside the XML is working without any extra attributes. The problem is in the injected code. I've added thexmlns
to the injected script tag but still not luck
– Cornwell
Nov 11 at 11:01
@wOxxOm sorry, I missed thecreateElementNS
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
add a comment |
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
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
javascript xml google-chrome-extension firefox-addon firefox-webextensions
asked Nov 11 at 10:35
Cornwell
1,71433554
1,71433554
@wOxxOm To clarify, the script inside the XML is working without any extra attributes. The problem is in the injected code. I've added thexmlns
to the injected script tag but still not luck
– Cornwell
Nov 11 at 11:01
@wOxxOm sorry, I missed thecreateElementNS
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
add a comment |
@wOxxOm To clarify, the script inside the XML is working without any extra attributes. The problem is in the injected code. I've added thexmlns
to the injected script tag but still not luck
– Cornwell
Nov 11 at 11:01
@wOxxOm sorry, I missed thecreateElementNS
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
add a comment |
1 Answer
1
active
oldest
votes
Simply create the script DOM element with an XML-compatible namespace:
let script = document.createElementNS('http://www.w3.org/1999/xhtml', 'script');
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Simply create the script DOM element with an XML-compatible namespace:
let script = document.createElementNS('http://www.w3.org/1999/xhtml', 'script');
add a comment |
Simply create the script DOM element with an XML-compatible namespace:
let script = document.createElementNS('http://www.w3.org/1999/xhtml', 'script');
add a comment |
Simply create the script DOM element with an XML-compatible namespace:
let script = document.createElementNS('http://www.w3.org/1999/xhtml', 'script');
Simply create the script DOM element with an XML-compatible namespace:
let script = document.createElementNS('http://www.w3.org/1999/xhtml', 'script');
answered Nov 11 at 16:00
wOxxOm
26.2k34461
26.2k34461
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
@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