栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从Chrome的扩展程序内容脚本访问iframe内容

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从Chrome的扩展程序内容脚本访问iframe内容

通常,没有直接访问其他来源

window
对象的直接方法。如果要在不同框架中的内容脚本之间 安全地
通信,则必须将消息发送到后台页面,该页面又将消息发送回选项卡。

这是一个例子:

的一部分

manifest.json

"background": {"scripts":["bg.js"]},"content_scripts": [    {"js": ["main.js"], "matches": ["<all_urls>"]},    {"js": ["sub.js"], "matches": ["<all_urls>"], "all_frames":true}]

main.js

var isTop = true;chrome.runtime.onMessage.addListener(function(details) {    alert('Message from frame: ' + details.data);});

sub.js

if (!window.isTop) { // true  or  undefined    // do something...    var data = 'test';    // Send message to top frame, for example:    chrome.runtime.sendMessage({sendBack:true, data:data});}

后台脚本“ bg.js”:

chrome.runtime.onMessage.addListener(function(message, sender) {    if (message.sendBack) {        chrome.tabs.sendMessage(sender.tab.id, message.data);    }});

一种替代方法是使用

chrome.tabs.executescript
bg.js
主内容脚本来触发的功能。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/575517.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号