Chrome扩展程序:在chrome.tabs.update之后访问DOM [英] Chrome Extension : Access DOM after chrome.tabs.update
本文介绍了Chrome扩展程序:在chrome.tabs.update之后访问DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的目标是通过id搜索 div
,如果它存在于我的网站支持的所有语言中。
My aim is to search for a div
by id if it exists in all languages my site supports.
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?
In above query is not coming. How can I parse the DOM after chrome.tabs.update ?
推荐答案
您是否尝试过使用回调?
api将函数定义为 chrome.tabs.update(integer tabId,object updateProperties,function callback)
。
Have you tried using a callback?
The api defines the function as chrome.tabs.update(integer tabId, object updateProperties, function callback)
.
也许你可以改变
chrome.tabs.update(tab.id, {active:true, url: currentTab + "?languageParam=" + languages[i]});
for
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");
}
}
);
这可能无法正常工作,但我希望这个想法很明确。
This may not work as-is, but I hope the idea is clear.
这篇关于Chrome扩展程序:在chrome.tabs.update之后访问DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文