Reputation: 107
I was wondering if it is possible in anyway to inject a javascript file instead of just code statements.
for example (what works):
chrome.devtools.inspectedWindow.reload({
ignoreCache: true,
injectedScript: 'console.log("Hello World!")'
});
but I want to see if I can inject a .js file (doesn't work):
chrome.devtools.inspectedWindow.reload({
ignoreCache: true,
injectedScript: 'file.js'
});
Upvotes: 1
Views: 329
Reputation: 6791
I think that is not or not yet possible to do. But there are other workarounds like inject the file using the content.js
or using the background.js
chrome.tabs.executeScript(). You might want to check using a content script js file and background.js
Upvotes: 1
Reputation: 77601
No, this API doesn't allow it, but it's not hard to write a wrapper that does:
file.js
using XHR.reload
with the response as injectedScript
.For example:
function reloadWithFile(scriptFile) {
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function(evt) {
chrome.devtools.inspectedWindow.reload({
ignoreCache: true,
injectedScript: xhr.responseText
});
});
xhr.open("GET", chrome.runtime.getURL(scriptFile));
xhr.send();
}
Injecting multiple files by concatenating them is left as an exercise (Promises may be helpful).
Upvotes: 2