我正在为Gmail编写一个Google Chrome扩展,用于修改Compose窗口。每当我刷新页面并在页面刷新后打开Compose窗口时,它都能正常工作,但下次我打开Compose(不刷新Gmail(窗口时,内容脚本无法加载。我的内容脚本是这样开始的:
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毫秒检查一次加载是否完成(。