在控制台中,我可以输入document.getElementById('...')并返回值。甚至.textContent并获取我想要的字符串。Chrome扩展 - Document.getelementbyId(...)评估为null?
一旦我将它放入我的扩展中并运行它,它将评估document.getElementById('...')为null。这是怎么回事?
manifest.json的:
{
"name": "CSUF RMP",
"version": "0.1",
"manifest_version" : 2,
"description": "Displays professor ratings on icon click",
"background" : {
"scripts" : ["background.js"]
},
"browser_action": {
"default_icon": "icon16.png"
},
"content_scripts": [
{
"matches": ["https://mycsuf.fullerton.edu/*"],
"js": ["script.js"]
}
],
"permissions": ["<all_urls>", "*://*/*", "http://*/*", "https://*/*"]
}
Background.js:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {file: "script.js"});
});
我的script.js简直是我贴在上面。该脚本应该有权访问网页的DOM(因此我需要一个内容脚本)并在点击图标(因此为background.js)上运行它。
我可以让页面运行并显示警报或者其他东西,但是该行并不评估页面的dom,只是null。
'document.getElementById('...')' - 真的吗?我认为身份证不可能有'...'作为价值 –