简体   繁体   English

在Chrome扩展程序中插入图片?

[英]Inject image in Chrome extension?

I want to make a Chrome extension that injects an image (that is packed with the extension) onto a webpage. 我想制作一个Chrome扩展程序,将图像(与扩展程序一起包装)注入到网页中。 I want it to be in a fixed position and overlaid on top of everything on the page. 我希望它处于固定位置并覆盖在页面上所有内容的顶部。 What is the most efficient way to do this---with Javascript and CSS (and maybe html?)? 使用Javascript和CSS(甚至是html),最有效的方法是什么?

Code I have so far: 我到目前为止的代码:

manifest.json manifest.json

 {
    "manifest_version": 2,

  "name": "OVERLAY IMAGE",
  "description": "blahblah",
  "version": "1.0",

  "icons": { "16": "icon16.png",
             "48": "icon48.png",
            "128": "icon128.png" },

   "web_accessible_resources": [
       "pic.jpg"
   ],

  "content_scripts": [{
       "matches": ["<all_urls>"],
       "js": ["content.js"],
       "css":["css.css"]
    }],

  "permissions": [
       "activeTab"
   ]

}

content.js content.js

Not totally sure what to do here after I load the URL. 加载网址后,我不太确定该怎么做。

var imgURL = chrome.extension.getURL('pic.jpg');

css.css css.css

I'm guessing I need to use some sort of id to make the image fixed. 我猜我需要使用某种ID来固定图像。

#someImage img{
   position:fixed;
}

So I basically have all the parts, I think, I'm just not sure how to put it all together? 所以我想我基本上拥有所有部分,只是不确定如何将它们放在一起? Am I missing anything important? 我错过任何重要的东西吗? Any help would be appreciated, thank you! 任何帮助,将不胜感激,谢谢!

The next steps would be to make an <img> element with a src of imgURL then add that to the DOM. 下一步将是使用imgURLsrc创建一个<img>元素,然后将其添加到DOM中。 Then you will add some css to both center it and make sure it is on top of everything. 然后,您将添加一些css使其居中,并确保它位于所有内容的顶部。 Depending on the size or use of that image you may want to add some sort of dimming to the rest of the page. 根据图像的大小或用途,您可能需要在页面的其余部分添加某种暗淡效果。 So for example: 因此,例如:

CSS 的CSS

  #someName img{
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  #someName{
    background-color: rgba(240,240,240,0.7);
    width: 100%;
    height: 100%;
    position:fixed;
    top:0;
    left:0;
  }

JS JS

var div = document.createElement("DIV");
div.id = "someName";
var img = document.createElement("IMG");
img.src = imgURL;
div.appendChild(img);
document.body.appendChild(div);

In this example we are making a div that has a fixed position and covers the whole frame. 在此示例中,我们将制作一个具有固定位置并覆盖整个框架的div I gave it a translucent background color. 我给它提供了半透明的背景色。 Then we make and img with the src set to our local url. 然后我们将src设置为我们的本地url并进行img In our css we make the image centered by setting it to fixed then having the starting point be half way down the page and half across. 在我们的CSS中,我们将图像设置为固定,然后使起点位于页面的一半下方,跨过一半,从而使图像居中。 The transform is to account for the size of the image to actually center it. 转换是要考虑将图像实际居中的大小。 Next we add the img to the div and the div to the body and call it a day. 接下来,我们将img添加到div ,并将divbody并将其命名为day。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM