简体   繁体   English

当我在Google chrome扩展程序中的内容脚本的帮助下将鼠标悬停在页面的每个iframe上时,该如何向其添加按钮?

[英]How can I add button to every iframe of the page when I hover upon it with the help of content script in google chrome extension?


I am fairly new to chrome extension and content scripts. 我对chrome扩展程序和内容脚本不熟悉。 I just wanted to ask how can I add a small button to every iframe of the page which is loaded . 我只想问一下如何在加载的页面的每个iframe中添加一个小按钮。
Here is what I have tried until now 这是我到目前为止一直尝试的
My manifest.json file is 我的manifest.json文件是

{
  "manifest_version":2,
  "name":"A name",
  "description":"Anythinng",
  "version":"1.0",

  "browser_action":{
    "default_icon":"icon.png",
    "default_popup":"popup.html"
  },
  "permissions":[
    "tabs","background","storage","notifications","webRequest", "webRequestBlocking","<all_urls>"
  ],
  "content_scripts":[
    {
      "js":["content_script.js"],
      "matches":["<all_urls>"],
      "all_frames":true,
      "run_at": "document_end"

    }
  ],
  "background":{
    "scripts":["background.js"],
    "persistent":true
  },

}

My content_script.js looks like 我的content_script.js看起来像

var body=document.getElementsByTagName('html')[0];
var div=document.createElement('div');
div.setAttribute('id',"div_qwerty_223");
var button=document.createElement('button');
button.setAttribute('id',"create-user");
button.style.cssText='background-position: right top; background-repeat: no-repeat;cursor: pointer;height: 15px;right: 0;top: 0;margin: 0;overflow: hidden;padding: 0; position: absolute;transform: scaleX(1);width: 16px;z-index: 9010;';
div.appendChild(button);
body.addEventListener('mouseover',function (event) {
    event=event.target||event.srcElement;
    if(event.nodeName==='IFRAME') {

        event.contentWindow.document.getElementsByTagName('body')[0].insertBefore(div,event.contentWindow.document.getElementsByTagName('body')[0].childNodes[0]);


    }
},false);
body.addEventListener('mouseout',function (event) {
    event=event.target||event.srcElement;
    if(event.nodeName==='IFRAME') {
        event.contentWindow.document.getElementsByTagName('body')[0].removeChild(div);
    }
},false);

It works fine for some iframes but for some iframes it shows this error 它适用于某些iframe,但对于某些iframe,则显示此错误
Uncaught SecurityError: Blocked a frame with origin " http://abcdef.com " from accessing a frame with origin " http://fedcba.com ". 未捕获到的SecurityError:阻止了源为“ http://abcdef.com ”的框架访问源为“ http://fedcba.com ”的框架。 Protocols, domains, and ports must match. 协议,域和端口必须匹配。
How can I deal with this issue and be able to add button to every Iframe upon hovering on the iframe irrespective of the origin of the iframe. 无论iframe的来源如何,如何将鼠标悬停在iframe上,如何处理该问题以及如何向每个iframe添加按钮。
A detailed solution would be much appreciated since I have read a few links but couldn't get through them well. 详细的解决方案将不胜感激,因为我已经阅读了一些链接,但无法很好地理解它们。
Thanks in advance ! 提前致谢 !

Don't try to append the button to the actual frame - just use clever positioning. 不要尝试将按钮附加到实际框架上-只需使用巧妙的定位即可。

Put the button and an iframe as siblings under the same parent - have the parent be relative-position and the button be absolute-position. 将按钮和iframe作为兄弟姐妹放在同一个父对象下-父对象为相对位置,而按钮为绝对位置。

Like so: https://jsfiddle.net/t8zujogb/ 像这样: https : //jsfiddle.net/t8zujogb/

HTML: HTML:

<div class="frame-container">
  <button class="iframe-button">
    Click me
  </button>

  <iframe src="https://test.com"></iframe>
</div>

CSS: CSS:

.frame-container {
  position: relative;
}
.iframe-button {
  display: none;
  position: absolute;
  top: 15px;
  left: 15px;
}

/* Only show the button when the parent is hovered: */

.frame-container:hover .iframe-button {
  display: initial;
}

Easy! 简单!

暂无
暂无

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

相关问题 如何在 chrome 扩展内容脚本中使用 google api? - How can I use google api in chrome extension content script? 如何在Chrome扩展程序的内容脚本中将事件监听器添加到Google表格链接? - How can I add event Listener to google sheets link in chrome extension's content script? 当页面上已有旧版本时,如何在chrome扩展内容脚本中使用jquery - How can I use jquery in a chrome extension content script when an older version is already on the page 如何通过父页面中的脚本让一个iframe的按钮作用于另一个iframe? - How do I let one iframe's button act upon another iframe with the script in parent page? 如何在后台获取chrome扩展程序的内容脚本? - How can I get chrome extension's content script to in the background? 如何确定Chrome扩展程序是否位于内容脚本的弹出窗口中? - How can I determine if a Chrome extension is in a popup from the content script? "<i>How can I fetch data in my content script?<\/i>如何在我的内容脚本中获取数据?<\/b> <i>(chrome extension)<\/i> (镀铬扩展)<\/b>" - How can I fetch data in my content script? (chrome extension) 如何以编程方式单击带有Chrome扩展程序的Google文档中的按钮? - How can I programmatically click a button in Google Docs with a Chrome Extension? 如何在Chrome扩展程序中执行脚本“新建页面”? - How can i excute script New page in chrome extension? 如何使用扩展程序在Google Chrome中加载页面? - How can I load a page in Google Chrome using an extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM