简体   繁体   中英

Keycode not working in chrome extension

I am writing a chrome extension which should change the content-type of http responses. It is working fine. Now, I want to control it using keypress ie user should be able to bypass the default action of extension on keypress. I tried this, but it doesn't work:

background.js

var enableEXT = true;
window.onkeydown = function() 
{
    console.log('Testing hello');
    if (event.keyCode == 70)
        enableEXT = false;
    console.log(event.keyCode);
    console.log(enableEXT);
};
window.onkeyup = function() 
{
    enableEXT = true;
    console.log(event.keyCode);
};
chrome.webRequest.onHeadersReceived.addListener(function(details) {
for (var i = 0; i < details.responseHeaders.length; ++i) 
{
    if (details.responseHeaders[i].name.toLowerCase() == 'content-type' && !enableEXT)
        details.responseHeaders[i].value = 'application/xyz';
}
return {responseHeaders: details.responseHeaders};
}, {urls: ["<all_urls>"]}, ['blocking', 'responseHeaders']);

Try using a content script, injected into the web page, instead of a background script. Use this in your manifest.json:

"content_scripts" : [{
    "js" : ["myScript.js"]
}]


"permissions": [
    "tabs", "http://*/*", "https://*/*"
]

However, chrome extensions are sandboxed from other scripts in the page. However, you do have access to the DOM and you can inject your own tags. So, create a new <script> tag and append it to the DOM, and the new script (script.js) it references can include the event listeners you want.

background.js:

chrome.runtime.onConnect.addListener(function(port) {
  port.onMessage.addListener(function(msg) {
    enableEXT = msg.enableEXT;
  });
});

chrome.webRequest.onHeadersReceived.addListener(function(details) {
for (var i = 0; i < details.responseHeaders.length; ++i) 
{
    if (details.responseHeaders[i].name.toLowerCase() == 'content-type' && !enableEXT)
        details.responseHeaders[i].value = 'application/xyz';
}
return {responseHeaders: details.responseHeaders};
}, {urls: ["<all_urls>"]}, ['blocking', 'responseHeaders']);

myScript.js:

//to inject into the page
var s = document.createElement('script');
s.src = chrome.extension.getURL("script.js");
s.onload = function() {
    this.parentNode.removeChild(this);
};
(document.head||document.documentElement).appendChild(s);

//to communicate with our background
var port = chrome.runtime.connect();

window.addEventListener("message", function(event) {
    // We only accept messages from ourselves
    if (event.source != window)
      return;

    if (event.data.type && (event.data.type == "FROM_PAGE")) {
      port.postMessage(event.data);
    }
}, false);

script.js:

window.onkeydown = function() 
{
    if (event.keyCode == 70)
        window.postMessage({ enableEXT: true, "*");
};
window.onkeyup = function() 
{
    window.postMessage({ enableEXT: false, "*");
};

To communicate between your injected code, your content script, and your background page is a very awkward situation, but can be dealt with, with all the postMessage APIs...

Add a parameter named event to the event handlers you assign to the events like this:

window.onkeydown = function(event) 
{
    console.log(event.keyCode);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM