Albi Hoti
Albi Hoti

Reputation: 351

Google Chrome simple code injection

on chrome extension, im trying to add a an iframe on all websites,

currently my manifesjt.json

    {
  "name": "Testinjection",
  "version": "1.0.0",
  "manifest_version": 2,
  "description": "Testinjection",
  "icons": {
    "48" : "sample-48.png",
    "128" : "sample-128.png"
  },
  "content_scripts": [
    {
      "matches": [ "http://*/*", "https://*/*" ],
      "js" : ["contentscript.js"]
    }
  ]
}

and contentscript.js:

document.write("<iframe src=\"http:\/\/www.ask.com\" width=\"100%\" height=\"40px\"><\/iframe>");

The problem is that this code is replacing the current visited page and not adding this code to top,

in which way can i do this?

Upvotes: 0

Views: 127

Answers (1)

Blender
Blender

Reputation: 298364

Using document.write after your page has loaded will overwrite the page. You should construct an element instead:

var iframe = document.createElement('iframe');
iframe.src = 'http://www.ask.com/';
iframe.width = '100%';
iframe.height = '40px';
iframe.style.position = 'absolute';  // Add this

document.body.insertBefore(iframe, document.body.firstChild);

Demo: http://jsfiddle.net/XrqvG/

Upvotes: 1

Related Questions