chrome浏览器插件content_scripts无法调用iframe下的元素,求大神帮忙。

现在想搞一个插件,可以实现页面的自动点击,结果发现这个网页包含一个iframe的,具体以下代码:
主页面代码:

 <html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>开通业务确认</title>
<link rel="stylesheet" href="/page/swcl/css/easyui.css" type="text/css" media="screen">
<script src="/page/swcl/js/jquery.min.js"></script>
<script src="/page/cm/js/jquery.easyui.min.js"></script>
<script src="/page/swcl/platform/js/easyui-lang-zh_CN.js"></script>
<script src="/page/swcl/sunshine/js/clipboard.min.js"></script>
<script src="/page/swcl/sunshine/js/migu.js"></script>
<script>
var urlPrefix = "";
var errorMsg="";
var memberType=parseInt("0");
var netId="ba1b41f7e0634a2a997c8d071651704d";
var authId="e9419cd15c484e28af5f8774abec49b1";

var openBizOrder = {};
openBizOrder.netId="ba1b41f7e0634a2a997c8d071651704d";
openBizOrder.authId="e9419cd15c484e28af5f8774abec49b1";
openBizOrder.productionId="698039020020006005";
openBizOrder.price=parseInt("500");
openBizOrder.channelId="014Z020";
openBizOrder.orderId="0120520171116299428725";

var openBizOrderDesc="欢迎开通中国移动咪咕音乐企业彩铃功能业务";

var isMutiOrder = "false";
if(isMutiOrder=="true"){
    var nextOpenBizOrder = {};
    nextOpenBizOrder.netId="";
    nextOpenBizOrder.authId="";
    nextOpenBizOrder.productionId="";
    nextOpenBizOrder.price=parseInt("");
    nextOpenBizOrder.channelId="";
    nextOpenBizOrder.orderId="";

    var nextOpenBizOrderDesc="";
}

var h5AppUrl = "";

</script>
<script src="/page/swcl/sunshine/js/openBizConfirm2.js"></script>
<script id="h5AppUrl" data="" type="text/javascript"></script>
</head>
<body>


<div id="payDetailTag" style="background: rgb(255, 255, 255); position: fixed; z-index: 1000; left: 0px; top: 0px; display: block; -webkit-box-align: center; align-items: center; width: 100%; height: 100%;"><div style="width: 100%; display: -webkit-box; display: -ms-flexbox; display: -webkit-flex; display: -moz-flex; -webkit-box-pack: center;  -ms-flex-pack: center; -webkit-justify-content: center; -moz-justify-content: center; justify-content: center; text-align: center; height: 100%"><iframe id="frame" src="http://223.111.8.142:8080/migunetsdk/webJs/frame.html?netId=ba1b41f7e0634a2a997c8d071651704d&amp;umark=Common&amp;client=WAP" name="__migu_web_pay_frame" class="miguframe" style="position: absolute; top: 0; left: 0; border: 0; width: 100%; height: 100%;"></iframe></div><div id="payInfo" style="display: block;position:fixed;top: 0;left: 0;z-index: -1;text-align: center;width: 100%;height: 100%;background: #dedede;font-family: 微软雅黑;color: rgb(78, 78, 78);line-height:45px;font-size:1.525em;">欢迎开通中国移动咪咕音乐企业彩铃功能业务</div></div></body></html>

iframe页面的代码

 <html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline' 'unsafe-eval' http://wsdkdl.migu.cn:8080"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><meta content="width=device-width,initial-scale=1.0,minimum-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"><link rel="stylesheet" type="text/css" href="http://wsdkdl.migu.cn:8080/music/Common/style.css?v=3d1thl8Ih4sFTxA3_IbMQ1"></head><body><script>var __url_res='http://wsdkdl.migu.cn:8080/music/Common';var __url_bin='http://wsdkdl.migu.cn:8080/ba1b41f7e0634a2a997c8d071651704d/1510773427830/netsdk_b.bin?v=3d1thl8Ih4sFTxA3_IbMQ1';var __s='e3e9adea218c4d4f9d535fc966d6170a';window.parent.postMessage('{"msgtype": "init2", "__s":"'+__s+'"}', '*');</script><script crossorigin="" src="http://wsdkdl.migu.cn:8080/ba1b41f7e0634a2a997c8d071651704d/1510773427830/netsdk_b.js?v=3d1thl8Ih4sFTxA3_IbMQ1"></script>
<div id="payLoginTag" class="payLoginTag"><div class="payLogin"><div class="payLogo"><img src="http://wsdkdl.migu.cn:8080/music/Common/img/pay_logo_ear.gif"></div></div></div><div class="payDetail onkey" id="payDetail"><div class="close"><a href="javascript:;"><img src="http://wsdkdl.migu.cn:8080/music/Common/img/closeNavBtn_m.png" id="close"></a></div><div class="content"><div class="payName">包月费用:<span id="fee">¥5.00</span></div><div class="notice"><span id="notice">请输入手机号码及验证码,完成支付</span></div><div class="payInfo"><p>支付号码:<span class="payPhone" id="payPhone">183****7771</span></p></div><div class="okPay"><a href="javascript:;" id="okPay">确认支付</a></div><div class="footer onekeyFooter"><div class="footerImg"><img src="http://wsdkdl.migu.cn:8080/music/Common/img/bottom.png" id="discribe" class="top"></div><div class="logo"><img src="http://wsdkdl.migu.cn:8080/music/Common/img/migu-logo.png" class="migulogo" id="logo"></div><div class="clear"></div><div class="describe" id="detailInfo"><p>本支付由中国移动咪咕文化提供,付费金额将从您的话费中扣除。</p></div></div></div></div></body></html>

我要提取到iframe中的一个a元素,
现在在content_scripts的js中直接写var okPay = document.getElementById("okPay");得到的却是null。在mainifest.json中也配置了js在iframe中应用,{
"matches": ["http://211.137.107.18:8888/cm/*","http://223.111.8.142:8080/migunetsdk/webJs/*"],
"js": ["js/myscript2.js"],
"all_frames":true
}。希望大神们能帮忙看看,怎么解决。

2个回答

没咋明白 应该是权限的问题把

jzlkf
jzlkf 已经配置"all_frames":true,这个了。现在就是在iframe下面取a元素的时候就是null
2 年多之前 回复
chenxiansheng888
小刀有话说 回复jzlkf: http://blog.allenm.me/2010/11/chrome-extension-cannot-read-iframe/ 试试这个
2 年多之前 回复
jzlkf
jzlkf 权限配置已经在mainifest中配置了,"permissions": [ "tabs", "https://*/", "http://*/*", "webRequest", "webRequestBlocking" ]
2 年多之前 回复

这个是跨域访问,chrome不允许的
一种解决思路是把content脚本除了注入到主页面,同时也注册另一个content脚本到iframe指向的页面,并且在两个脚本间进行消息传递
另一种思路更粗暴些,可以搜索一下“chrome 设置 跨域“,直接设置允许跨域访问

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问