1

我在 Chrome API 的 getAllFrames 中有一个回调函数:

chrome.webNavigation.getAllFrames({tabId: tabArray[0].id},getAllFramesCallback);

我在使用回调函数 getAllFramesCallback 从 html 文件 tabStructure.html 中查找元素 ID 时遇到问题,该文件是使用以下方法创建的:

chrome.windows.create({url:"tabStructure.html", type:'popup', height:450, width:600, left:1500, focused:true});

tabStructure.html:

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>TabStructure</title>
  <link rel="stylesheet" href="./jquery-ui-1.10.3/themes/base/jquery-ui.css" />
  <script src="./jquery-ui-1.10.3/jquery-1.9.1.js"></script>
  <script src="./jquery-ui-1.10.3/ui/jquery-ui.js"></script>
  <link rel="stylesheet" href="./jquery-ui-1.10.3/demos/demos.css"/>
  <script src="background.js"></script>

  <script src="tabStructure.js"></script>

</head>
<body>

<div id="tabs">
  <h3 id = "tabId">Tab ID</h3>
  <ul>
    <li><a href="#frame0">Main Frame</a></li>
  </ul>
  <div id="frame0">
    <p>abc</p>
    <ul>
      <li><a href="#frame1">sub_Frame</a></li>
    </ul>
    <div id="frame1">
    </div>
  </div>
</div>

</body>
</html>

背景.js

function getAllFramesCallback(framesList){
    for ( i = 0 ; i < framesList.length ; i++){
        drawOnPage(framesList[i]);
    }

};    

function findChildList(frameId,framesList){
    for ( var i = 0 ; i< framesList.length ; i ++){
    if(framesList[i].parentFrameId == frameId){
        childList.push(framesList[i]);
    }
    }    

    return childList;
}
function drawOnPage(frameDetail){ 

    if(frameDetail.frameId == 0){
    var content = document.getElementById("tabs").innerHTML; /*this returns undefined*/
    content = ( content + "<p>Frame ID : HaHA</p>"); 
    document.getElementById("frame0").innerHTML = content;

    }
}


//listener for the messages send from popup page and content scripts
$(document).ready(function() {

     if(request.words == "tabStructure"){
        chrome.windows.getCurrent(function(win) { 
        chrome.tabs.query( {'windowId': win.id, 'active': true}, function(tabArray){ 
            if (tabArray) { 
            tabId2 = tabArray[0].id;
            chrome.webNavigation.getAllFrames({tabId: tabArray[0].id},getAllFramesCallback);
            } 
        }); 
        });
        chrome.windows.create({url:"tabStructure.html", type:'popup', height:450, width:600, left:1500, focused:true});
    }
    });

});
4

0 回答 0