0

I'm working on a google extension where I want to change the background-color of youtube.com by pressing a button in my popup.html and then running my function.

Here is my code:

The JavaScript file ms.js:

function color1(){
document.getElementById("body-container").style.backgroundColor="blue";}

The HTML popup.html file:

<!DOCTYPE html>
<html>
<head>
<script src="ms.js">
</script>
</head>
<body>
<p> TEST </p>
<input type="button" value="blue" id="btn1" onclick="color1"/>
</body>
</html>

Here is my manifest.json file:

{

"name": "my extension test",

"version": "1.0",

"permissions": [
    "activeTab",
    "pageCapture"
],

"manifest_version": 2,

"description": "Test extension :D",

"browser_action": {
"default_icon": "ikon.png",
"default_popup": "popup.html"
},

"web_accessible_resources": [
"script/ms.js"
],

"content_scripts": [
    {
      "matches": ["https://www.youtube.com/*"],
      "js": ["ms.js"]
    }
  ]

}
1
  • Check this SO question if it can help you :) Commented Apr 29, 2016 at 3:35

1 Answer 1

1

Updated

  1. Content Scripts(injected through manifest.json) is not needed, so we remove content_scripts part
  2. We decide to use Programming injection, the only needed permission is activeTab, so we remove activeTab and pageCapture. If you want your scripts to be executed in background tab(not active tab), then you could replace activeTab permission with host permission.
  3. Web Accessible Resources is supposed to be used when we expect some resources to be used in the context of a web page, content scripts themselves don't need to be whitelisted, so we remove web_accessible_resources part
  4. JavaScript is executed when it's found in the document, if you include it inside <head> tag, the <body> is not constructed then you could find the element, so we put that in the bottom of <body>
  5. Inline Scripts won't be executed by default, so we move this event binding logic to external scripts.

Take a look at chrome.tabs.executeScript, for such minor feature, you could inject JavaScript code into a page.

manifest.json

{
    "name": "my extension test",
    "version": "1.0",
    "manifest_version": 2,
    "description": "Test extension :D",
    "browser_action": {
        "default_popup": "popup.html"
    },
    "permissions": [
        "activeTab"
    ]
}

popup.html

<!DOCTYPE html>
<html>

<head>
</head>

<body>
    <p> TEST </p>
    <input type="button" value="blue" id="btn1" />
    <script src="ms.js">

    </script>
</body>

</html>

ms.js

document.getElementById("btn1").addEventListener("click", function() {
    chrome.tabs.executeScript({"code": 'document.getElementById("body-container").style.backgroundColor = "blue";'});
}, false);
Sign up to request clarification or add additional context in comments.

3 Comments

Code dump correcting multiple errors/misconceptions, but no explanation.
I disagree with replacing "activeTab" with a host permission, but at least it's now a valuable answer.
@Xan, provided more info about activeTab and host permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.