如何使用 chrome 扩展弹出窗口从新的弹出窗口定位 DOM 元素.html



我有一个工作正常的chrome扩展,我引用了一个函数,它会打开一个弹出窗口。 通常,如何从我的弹出窗口或弹出窗口.html文档中访问新弹出的html dom元素.js在同一域内(。

function cfs_policy() { 
chrome.tabs.query({active: true, currentWindow: true}, 
function(tabs) {
// query the active tab, which will be only one tab
//and inject the script in it
chrome.tabs.executeScript(tabs[0].id, {file: 
"links/cfs_policy.js"});
});
}
function d_cfs_policy() {   
chrome.tabs.query({active: true, currentWindow: true}, 
function(tabs) {
// query the active tab, which will be only one tab
//and inject the script in it
chrome.tabs.executeScript(tabs[0].id, {file: 
"links/d_cfs_policy.js"});
});
}
document.getElementById('cfs').addEventListener('click', cfs);
document.getElementById('d_cfs').addEventListener('click', d_cfs);
document.getElementById('cfs_policy').addEventListener('click', 
cfs_policy);
document.getElementById('d_cfs_policy').addEventListener('click', 
d_cfs_policy);

[片段] cfs_policy.js

function cfs_policy(){
var x = document.getElementById('tabFrame');
var y = x.contentDocument ;
var els = y.getElementsByTagName('table')[8]
els.getElementsByClassName('actionIcon editIconBtn')[0].click()
els.focus();
}
cfs_policy()

[片段] d_csf_policy.js

function d_cfs_policy(){
console.log('test')
}
d_cfs_policy()

[片段] 弹出窗口.html

<button class="collapsible" id="cfs">CFS Objects</button> 
<div class="content">
<table>
<tr>
<td>
<button id="d_cfs">Download</button>
<button class="collapsible" id="cfs_policy">Policies</button> 
<div class="content"><button id="d_cfs_policy">Download</button></div>
</td>   
</tr>
</table>
</div>  
</div>  

我实际上做了我一直想做的事情。 按名称定位窗口。 您可以在第 8 行看到,单击按钮时,它会打开一个窗口。 然后在第 14 行,我引用了同一个窗口,我可以自由地从第 15 行开始获取对象。 感谢您的帮助。 我希望读到这篇文章的人能够理解并发现它很有用。

function cfs_policy(){
var test;
var x = document.getElementById('tabFrame');
var y = x.contentDocument ;
var els = y.getElementById('profileTable')
els = els.getElementsByClassName('listItem')
for(i=0;i<els.length;i++){
els[i].getElementsByClassName('actionIcon editIconBtn')[0].click()
setTimeout(function(){
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] + "_profileObjDlg");
var lab = existingWin.document.getElementsByClassName('label')
var val = existingWin.document.getElementsByName('objName')
var sel = existingWin.document.getElementsByTagName('select')
test = lab[0].innerText + val[0].value + "n"
var p;
var cat;
for (let p = 5; p < 62; p++) {
try {
if (sel[p-1].value == 0) {cat = "Allow"}
else if (sel[p-1].value == 1) {cat = "Block"}
else if (sel[p-1].value == 2) {cat = "BWM"}
else if (sel[p-1].value == 3) {cat = "Confirm"}
else if (sel[p-1].value == 4) {cat = "Passphrase"}
test += lab[p].innerText + " : " + cat + "n"           
console.log(test)
}
catch(error) {
}
}
}, 3000);
}
}
cfs_policy()

最新更新