Within my background script, I am injecting my contentScript using the following method:
chrome.webRequest.onHeadersReceived.addListener(function(details){
if(isPDF(details))
{
chrome.tabs.executeScript(details.tabId, {file: "content.js", runAt: "document_end"}, function(result){
if(chrome.runtime.lastError)
{
console.log(chrome.runtime.lastError.message);
}
});
return {
responseHeaders: [{
name: 'X-Content-Type-Options',
value: 'nosniff'
}, {
name: 'X-Frame-Options',
value: 'deny'
}]
};
}
}, {
urls: ['*://*/*'],
types: ['main_frame']
}, ['blocking', 'responseHeaders']);
The content.js
file contains the following code snippet:
function reqHandler()
{
alert("hello");
var imgData = "" + this.responseText;
//document.write("" + imgData);
document.body.innerHTML = "";
var img = document.createElement("img");
img.setAttribute("src", 'data:image/jpeg;base64,' + "" + imgData);
document.body.appendChild(img);
}
//debugger;
var PDFdata = document.body.innerHTML;
document.body.innerHTML = "";
var xhr = new XMLHttpRequest();
xhr.onload = reqHandler;
xhr.open("GET", "http://localhost:81/getImage.php", true);
xhr.send();
Despite running into an issue while executing a cross-origin XMLHTTPRequest in the above script. My reqHandler
function fails to execute after completing the XHR request.
The embedded alert message inside is not visible. What could possibly be causing this problem?
UPDATE Below is the manifest file pertaining to the extension:
{
"manifest_version": 2,
"name": "My Extension",
"version": "1.0",
"background": {
"scripts": ["background.js"],
"persistent": true
},
"permissions": [
"webRequest",
"<all_urls>",
"webRequestBlocking",
"tabs",
"webNavigation"
]
}