Google chrome extension 我的Chrome扩展如何监听Gmail中打开的Compose窗口?
我正在为Gmail编写一个Google Chrome扩展,它修改了Compose窗口。每当我刷新页面并在页面刷新后打开撰写窗口时,它都可以正常工作,但是下次我打开撰写窗口(没有Gmail刷新)时,内容脚本无法加载。我的内容脚本是这样开始的:Google chrome extension 我的Chrome扩展如何监听Gmail中打开的Compose窗口?,google-chrome-extension,gmail,Google Chrome Extension,Gmail,我正在为Gmail编写一个Google Chrome扩展,它修改了Compose窗口。每当我刷新页面并在页面刷新后打开撰写窗口时,它都可以正常工作,但是下次我打开撰写窗口(没有Gmail刷新)时,内容脚本无法加载。我的内容脚本是这样开始的: "content_scripts": [ {"run_at": "document_end", "matches": ["https://mail.google.com/mail/*"], "js": ["updateContent.js", "colo
"content_scripts": [
{"run_at": "document_end",
"matches": ["https://mail.google.com/mail/*"],
"js": ["updateContent.js", "colorHeaders.js", "renderContent.js", "sendOptions.js",
"base64.js", "jsaes.js"]
}
],
updateContent.js:
document.addEventListener('DOMContentLoaded', addUpdateButton, true);
addUpdateButton();
.
.
.
很明显,我不想听DOMContentLoaded,那么我听什么呢?我的清单的相关部分如下所示:
"content_scripts": [
{"run_at": "document_end",
"matches": ["https://mail.google.com/mail/*"],
"js": ["updateContent.js", "colorHeaders.js", "renderContent.js", "sendOptions.js",
"base64.js", "jsaes.js"]
}
],
我通过将以下代码添加到我的一个内容脚本中解决了我的问题:
// Listen for Compose button-click
var composeButton = document.getElementsByClassName('T-I J-J5-Ji T-I-KE L3');
composeButton[0].addEventListener("click", addUpdateButton);
我在一个函数的底部添加了代码,该函数只有在页面加载完成后才会被调用(我每200毫秒检查一次加载是否完成)。使用类似于库的方法,或者自己使用MutationObserver观察DOM。@wOxxOm感谢您的建议。我会调查的。