Chrome扩展程序:在chrome.tabs.update之后访问DOM

时间:2014-05-05 00:33:48

标签: google-chrome google-chrome-extension

我的目标是在我的网站支持的所有语言中都存在div ID。

manifest.json

{
"manifest_version": 2,

"name": "Hello World!",
"description": "This extension is to check if link is not visible when it should not :)",
"version": "1.0",

 "browser_action": {
  "default_icon": "icon.png",
  "default_popup": "popup.html"
 },

"content_scripts": [ {
    "js": [ "jquery-1.11.1.min.js"],
    "matches": [ "http://*/*", "https://*/*"]
}],

  "permissions": [
    "tabs",
    "http://*/*",
    "https://*/*"
]

 }

popup.html -

 <html>
 <script type="text/javascript" src="jquery-1.11.1.min.js"></script>
 <style>

   table{background:#CCC;border:1px solid #000;}
   table td{padding:15px;border:1px solid #DDD;}
 </style>
 <body>
 <input type="hidden" id="currentLink"/>
 <hr />

    Checking ...

  <div id="dynamictable"></div>
   <script type="text/javascript" src="currentUrl.js"></script>
  </body>
  </html>

currentUrl.js -

    chrome.tabs.getSelected(null, function(tab) {
      var currentTab = (tab.url);


      var languages = [ "en", "fr" ];  
      $('#dynamictable').append('<table></table>');
     var table = $('#dynamictable').children();    

     var i;
     for (i = 0; i < locales.length; ++i) {

       chrome.tabs.update(tab.id, {active:true, url:  currentTab + "?languageParam=" + languages[i]});



var query = document.querySelector("div#header");

    if (query) {
         alert("This page has a friendly div");
     }

    table.append("<tr><td>" + languages[i] + "</td><td>"  + "</td></tr>");
    }

   });

上面查询未来。如何在chrome.tabs.update之后解析DOM?

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用回调? api将函数定义为chrome.tabs.update(integer tabId, object updateProperties, function callback)

也许你可以改变

chrome.tabs.update(tab.id, {active:true, url:  currentTab + "?languageParam=" + languages[i]});

代表

chrome.tabs.update(
    tab.id,
    {active:true, url:  currentTab + "?languageParam=" + languages[i]},
    function() { // this is the callback you may want to use.
        var query = document.querySelector("div#header");
        if (query) {
            alert("This page has a friendly div");
        }
    }
);

这可能无法正常工作,但我希望这个想法很明确。