簡體   English   中英

如何使用Chrome擴展程序popup.html從新的彈出窗口中定位DOM元素

[英]How to target DOM elements from new popup window, using chrome extension popup.html

我有一個chrome擴展程序,可以正常工作,我引用了一個函數,它會彈出一個彈出窗口。 通常,如何從popup.js或popup.html文檔訪問新彈出的html dom元素(在同一域中)。

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()

[摘要] popup.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行開始獲取objs。 謝謝您的幫助。 我希望閱讀此書的人可以理解並發現它有用。

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM