Christian
Christian

Reputation: 3393

Chrome extension: appending DIV to BODY element within content script not working

Within me Google Chrome extension I would like to inject some HTML code into website by appending a DIV to the BODY element. My current test content script for this looks as follows:

window.onload = function() {
  $('body').css('background-color', 'blue'); // just for testing
  $('body').append("<div>hello world</div>");
};

The background color does change to blue. However, appending the DIV seems not. At least I cannot find anything on the page or in the page source code. What am I missing here?

Upvotes: 0

Views: 3083

Answers (1)

Reyraa
Reyraa

Reputation: 4274

you don't need to add window.onload because content scripts load when the page is already loaded, see this page. remember to add permissions in manifist correctly:

"permissions": [
   "activeTab",
   "tabs",
   "http://*/*", "https://*/*"
],
"content_scripts": [
  {
    "matches": ["<all_urls>"],
    "js": ["js/contentscript.js"]
  }
]

I assumed that your content script will load in all tabs and it loades the content script from js folder.

Upvotes: 4

Related Questions