[英]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.