javascript - 如何让功能在子弹出窗口中工作
问题描述
我有一个 java 脚本/chrome 扩展,可以模拟来自商业网页的点击,在 chrome 控制台中运行良好。但是当我从扩展程序运行它时,我得到“onUpdate 不是函数”,它指的是弹出页面 html 中引用的回调 (onclick="return onUpdate();)。
弹出页面html
<div id="divButtons" align="left" style="position: relative; z-index: 10;">
<table border="0" cellspacing="0" cellpadding="0" width="97%" summary="">
<tbody><tr>
<td class="swlErrorMessage" id="errorStatus">Ready</td>
</tr>
<tr>
<td><font size="1"> </font></td>
</tr>
<tr align="right">
<td>
<input type="button" class="button" value="OK" name="ok" onclick="return onUpdate();" title="">
<input type="button" class="button" value="Cancel" name="cancel" onclick="return onCancel();" title="">
</td>
</tr>
</tbody></table>
我想我知道休息时间在哪里。基本上,如果我一次在控制台中运行代码,它就可以正常工作。但是,如果我手动单击链接,然后使用 chrome 扩展程序来定位窗口,它会正确执行除保存之外的所有操作,并且我在代码中注意到,当我运行 window.open 并定位名称时,这似乎中断对主页的任何引用,就像它没有对任何以前的回调的任何引用。如果我在弹出的页面上打开 chrome 控制台,再次运行 window.open 并执行代码,一切正常。
不知道为什么除了这个功能之外的所有东西都不起作用。更新()。
我已经从控制台运行它,它工作正常。
代码.js
function cfs_all(){
var x = document.getElementById('tabFrame');
var y = x.contentDocument ;
var link = y.getElementById('urlListTable');
link = link.getElementsByTagName('tr');
runLoop = async() => {
for(a=0;a<link.length;a++){
await new Promise(resolve => setTimeout(resolve, 2000))
if (link[a].children[1].innerText.includes('Allow') == true){
console.log('Yesssssssssss '+ String(a))
var rest = y.getElementsByTagName('tr')
rest = rest[a+2].getElementsByTagName('td')
//simulates the button click to open the pop up window
rest[3].getElementsByTagName('img')[0].click()
setTimeout(function(){
//connects to the popped up window
var test;
var text = "*.test.edu"
test = String(window.origin)
test = test.substring(8,)
test = test.replace(/\:.*/,'');
test = test.split('.')
//opens the window by referencing the window.name
var existingWin = window.open('', test[0]+ "_" + test[1] + "_" + test[2] + "_" + test[3] + "_urlObjDlg");
console.log(test[0]+ "." + test[1] + "." + test[2] + "." + test[3])
//clicks add, opens textbox
existingWin.document.getElementsByClassName('button')[0].click()
//adds the value to the text box
existingWin.document.getElementsByTagName('input')[9].value = text
//clicks save button
try{
existingWin.document.getElementsByClassName('button roundBtn swlEventSave')[0].click()
}
catch(err){
existingWin.document.getElementsByClassName('button roundBtn swlEventClose')[0].click()
}
//updates the page, THIS IS WHAT's NOT WORKING!!!
existingWin.onUpdate;
}, 1500);
}
else
{
console.log('Nooooooo ' + String(a))
}
}
}
runLoop()
}
cfs_all()
我可以粘贴上面的代码,它可以在控制台中运行
这是我必须粘贴在弹出窗口的 chrome 控制台中的代码,它可以工作......
function cfs_add(){
var test;
test = String(window.origin)
test = test.substring(8,)
test = test.replace(/\:.*/,'');
test = test.split('.')
var existingWin = window.open('', test[0]+ "_" + test[1] + "_" + test[2] +
"_" + test[3] + "_urlObjDlg");
console.log(existingWin)
existingWin.onUpdate()
}
cfs_add()
是一样的代码...
解决方案
推荐阅读
- java - ObjectInputStream 未读入
- selenium-webdriver - webdriver Firefox 和 selenium 错误(必须使用 Gecko 驱动)
- docker - 带有 Lustre 的 Docker Swarm 卷?
- android - Android WebView 作为 Chrome v25+
- express - 拒绝执行脚本,因为它的 MIME 类型('text/html')不可执行
- curl - 使用 POST 向服务器发送信息不适用于 curl 但适用于 netcat
- css - 可以将颜色作为值的 CSS 属性
- jquery - Fullcalendar JSON 编码和 Laravel
- swift - 如何将 Firebase 数据库快照与文本进行比较?
- python - 将图片上传到 s3 后,上传随附的缩略图