Javascript 无法确定是否已在chrome extension的body中设置了div元素
在我的chrome扩展中,我得到了一个div,我将它添加到当前选项卡的主体中。我正在收听chrome.tabs.onUpdate。如果调用此事件,我将在content_脚本中执行一个脚本。在这个函数中,我将使用jQueryJavascript 无法确定是否已在chrome extension的body中设置了div元素,javascript,jquery,html,google-chrome,google-chrome-extension,Javascript,Jquery,Html,Google Chrome,Google Chrome Extension,在我的chrome扩展中,我得到了一个div,我将它添加到当前选项卡的主体中。我正在收听chrome.tabs.onUpdate。如果调用此事件,我将在content_脚本中执行一个脚本。在这个函数中,我将使用jQuery$(document.ready(…)等待文档准备就绪。 我尝试访问 $("#div").length 有时返回1,有时返回0。如果它不在身体里,就应该把它加到身体里 出于某些奇怪的原因,在每个页面重新加载时调用onUpdate事件两次。实际上,我没有办法安全地检查div是否
$(document.ready(…)
等待文档准备就绪。
我尝试访问
$("#div").length
有时返回1,有时返回0。如果它不在身体里,就应该把它加到身体里
出于某些奇怪的原因,在每个页面重新加载时调用onUpdate事件两次。实际上,我没有办法安全地检查div是否已经添加
编辑:我的代码:
inject.js:
var crendentials;
var mailInfo;
function doLogin(username, password)
{
function verifyLogin(username, password)
{
$.get("www.example.com&login&username=" + username + "&password=" + password,
function (data)
{
if (!isNaN($.trim(data)))
{
crendentials = new Array();
crendentials[0] = username;
crendentials[1] = password;
chrome.storage.sync.set({ key: "example_toolbar", value: username + ";;;" + password});
chrome.storage.sync.set({ key: "example_toolbar_verified", value: 1});
}
else
{
chrome.storage.sync.set({ key: "example_toolbar_verified", value: 0});
}
}
);
}
if (typeof username === "undefined" || username === null || username === ""
|| typeof password === "undefined" || password === null || password === "")
{
var crentest;
chrome.storage.sync.get("example_toolbar",
function (value)
{
console.log("value von doLogin()", value.example_toolbar);
crentest = value.example_toolbar;
}
);
if (typeof crentest !== "undefined" && crentest !== null && crentest !== "")
{
chrome.storage.sync.get("example_toolbar",
function (value)
{
crendentials = value.example_toolbar.split(";;;");
}
);
}
}
else
{
verifyLogin(username, password);
}
}
function getMailInfos(callback)
{
if (typeof mailInfo === "undefined")
{
$.get("http://www.example.com&mailapi=showmails",
function (data)
{
mailInfo = new Array();
var infos = data.split("|");
mailInfo.unread = infos[0];
mailInfo.total = infos[1];
callback();
}
);
}
else
{
callback();
}
}
function getMails(callback)
{
if (typeof mailInfo === "undefined")
{
getMailInfos(function ()
{
callback(mailInfo.unread);
}
);
}
else
{
callback(mailInfo.unread);
}
}
function renderText(textstr)
{
var divText = document.createElement("div");
addClass(divText, "toolbarText");
var text = document.createTextNode(textstr);
divText.appendChild(text);
toolbar.appendChild(divText);
}
var mailAmountDiv;
function renderMail(callback)
{
var mailIco = document.createElement("div");
addClass(mailIco, "mailIcon");
mailAmountDiv = document.createElement("div");
mailAmountDiv.id = "mails_unread";
addClass(mailAmountDiv, "mailAmount");
mailIco.appendChild(mailAmountDiv);
getMails(function (value)
{
var mailAmount = document.createTextNode(value);
mailAmountDiv.appendChild(mailAmount);
toolbar.appendChild(mailIco);
renderPlaceholderSmall();
renderText(translations.notAttended + ": " + getNotRead());
}
);
}
function createToolbar(change)
{
$(document).ready(function()
{
console.log("ist vorhanden: ", $("#Example-Toolbar").length);
if ($("#Example-Toolbar").length > 0)
{
// already created, stop working here
return;
}
doLogin();
toolbar = document.createElement("div");
toolbar.id = "Example-Toolbar";
renderMail(function()
{
renderPlaceholderLarge();
renderSearch();
renderPlaceholderSmall();
renderRightIcons();
$("body").css({"margin-top": "23px", "z-index": -1});
//document.body.insertBefore(toolbar, document.body.childNodes[0]);
$(toolbar).prependTo("body");
}
);
}
);
}
background.js:
function tabListener(tabId, changeInfo, tab)
{
// exec script on current focused tabId
chrome.tabs.executeScript(tabId,
{
code: 'createToolbar();'
}
);
}
chrome.tabs.onUpdated.addListener(tabListener);
manifest.json(仅必要部分):
你能分享你的HTML和javascript代码吗?或者可能创建一个jsfiddle@somecomments:1。您正在访问的每个网页中注入您的文件。2.在每个未更新的事件中,您都在调用
createToolbar()
。请注意,在页面加载的“生命周期”中,会触发许多未更新的事件(例如,状态:加载、状态:完成、获取favicon等)。您需要检查changeInfo
(例如如果(changeInfo.status&&(changeInfo.status==“complete”){…}
)。3.最重要的一点是:在寻求有关您的代码的帮助时,请提供!谢谢,这很有效!:-)@那么,你的问题解决了吗?或者我们应该进一步研究吗?我的问题解决了,谢谢!
"background": {
"scripts": [
"background.js"
]
},
"permissions": [
"tabs",
"http://*/",
"https://*/",
"file:///*/*",
"storage"
],
"content_scripts": [{
"matches": ["*://*/*"],
"css": ["jquery-ui.css", "style.css"],
"js": ["jquery-2.0.3.min.js", "jquery-ui.min.js", "inject.js"],
"run_at": "document_start"
}]