1

I'm trying to write a chrome extension that generates simulated keyboard events.

This question explains how to generate the keyboard event, but when I run it on my content script it generates the following error:

error: Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".

What do I do to fix this?

Here's my manifest.xml

{
  "name": "2048 forever",
  "description": "SOLVES EVERYTHING",
  "version": "2.0",
  "permissions": [
    "activeTab"
  ],
  "web_accessible_resources": ["keyboard.js"],
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "browser_action": {
    "default_title": "Solve the things"
  },
  "manifest_version": 2
}

background.js contains:

chrome.browserAction.onClicked.addListener(function(tab) {
    console.log('Turning ' + tab.url + ' red!');

    chrome.tabs.executeScript({
        code: 'document.body.style.backgroundColor="red"'
    });

    triggerKeyEvent(document.body, 38);
});


function triggerKeyEvent(element, charCode) {
    // We cannot pass object references, so generate an unique selector
    var attribute = 'robw_' + Date.now();
    element.setAttribute(attribute, '');
    var selector = element.tagName + '[' + attribute + ']';

    var s = document.createElement('script');
    s.textContent = '(' + function(charCode, attribute, selector) {
        // Get reference to element...
        var element = document.querySelector(selector);
        element.removeAttribute(attribute);

        // Create KeyboardEvent instance
        var event = document.createEvent('KeyboardEvents');
        event.initKeyboardEvent(
            /* type         */ 'keydown',
            /* bubbles      */ true,
            /* cancelable   */ false,
            /* view         */ window,
            /* keyIdentifier*/ '',
            /* keyLocation  */ 0,
            /* ctrlKey      */ false,
            /* altKey       */ false,
            /* shiftKey     */ false,
            /* metaKey      */ false,
            /* altGraphKey  */ false
        );
        // Define custom values
        // This part requires the script to be run in the page's context
        var getterCode = {get: function() {return charCode}};
        var getterChar = {get: function() {return String.fromCharCode(charCode)}};
        Object.defineProperties(event, {
            charCode: getterCode,
            which: getterChar,
            keyCode: getterCode, // Not fully correct
            key: getterChar,     // Not fully correct
            char: getterChar
        });

        element.dispatchEvent(event);
    } + ')(' + charCode + ', "' + attribute + '", "' + selector + '")';

    console.log('text content: ' + s.textContent);

    (document.head||doc.documentElement).appendChild(s);
    s.parentNode.removeChild(s);
}
Community
  • 1
  • 1
Dash
  • 17,188
  • 6
  • 48
  • 49
  • Can you post your `manifest.json` – 0xcaff Mar 13 '14 at 18:14
  • Yup, just added it. I have tried injecting the script as a second file but I then don't have access to document. – Dash Mar 13 '14 at 19:14
  • `manifest.xml`? That might be the reason it is not working! – 0xcaff Mar 13 '14 at 19:19
  • Where is your code? In `background.js`? How is this a content script? – 0xcaff Mar 13 '14 at 19:20
  • 2
    The reason you are getting this particular error message is because the linked answer creates a ` – abraham Mar 13 '14 at 20:24
  • I don't know if it's a content script, actually. This is my first extension. I put the code in background.js. – Dash Mar 14 '14 at 00:29
  • The other problem is that the script is injected into the background, which isn't the desired outcome. – Teepeemm Nov 01 '15 at 20:41

0 Answers0