1

我做了一个扩展,需要在每一页上放置一个固定的图像。内容脚本是:

imgTop = chrome.extension.getURL('top.png');
var top = document.createElement("img");
top.src=imgTop;
top.position='fixed';
top.zIndex='2353445';
top.top='200';
top.left='20';
document.getElementsByTagName('body')[0].appendChild(top);

我得到错误:Uncaught Error: NotFoundError: DOM Exception 8

我尝试过但不起作用的方法:

  • 添加样式,例如top.style.position.
  • 使用document.body.

我知道我可以使用innerHTML,但它真的很乱......

知道有什么问题吗?或者另一种注入图像的方法?

4

2 回答 2

0

The problem here seems to be that your variable is named top, which Chrome is confusing with the window.top property. Change the variable name to something else and it should work, like this:

var topImage = document.createElement('img');
topImage.src = chrome.extension.getURL('top.png');
topImage.style.position = 'fixed';
topImage.style.zIndex = '2353445';
topImage.style.top = '200px';
topImage.style.left = '20px';
document.body.appendChild(topImage);

Some other things: Style properties are accessed through element.style.property, and if you're doing things to the body just use document.body. Also, no need to have a separate variable for the image URL, just call it directly for the topImage.src like here, especially if you're only calling it just once.

于 2013-02-25T23:12:27.410 回答
0

您是否将 top.png 添加到 manifest.json 中?

您需要定义 web_accessible_resources 属性并列出资源(以及这些资源的可选内容安全策略)。

chrome 扩展文档网址:https ://developer.chrome.com/extensions/manifest.html#web_accessible_resources

于 2013-02-26T03:38:56.833 回答