我尝试使用javascript来显示/隐藏“玩家-api”。 YouTube视频网址上的div标记。我的想法是通过chrome扩展加载按钮,然后用户可以单击该按钮。
我收到错误说" 3未捕获的TypeError:无法读取属性' style' of null"第11行(获取player-api ID后开始弄乱显示的代码行)。
我假设问题与扩展程序有关,而实际上却无法看到player-api对其产生影响。我不知道如何解决这个问题,类似的问题并没有真正帮助我。
有人能告诉我我做错了什么以及我该如何解决它?
的Javascript
var button = document.createElement("Button");
var t = document.createTextNode("Show/Hide");
button.appendChild(t);
document.body.appendChild(button);
button.addEventListener('click', toggleVisibility, false);
function toggleVisibility() {
var e = document.getElementById('player-api');
if (e.style.display == 'block' || e.style.display == '') e.style.display = 'none';
else e.style.display = 'block';
}
HTML
<html>
<head>
<title>Player-API's Popup</title>
<!--
- JavaScript and HTML must be in separate files: see our Content Security
- Policy documentation[1] for details and explanation.
-
- [1]: https://developer.chrome.com/extensions/contentSecurityPolicy
-->
</head>
<body>
<script src="popup.js"></script>
</body>
</html>
的manifest.json
{
"manifest_version": 2,
"name": "player-api",
"description": "This extension will show/hide Youtube videos",
"version": "1.0",
"background": {
"scripts": [ "background.js" ],
"persistent": false
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"declarativeContent",
"activeTab"
],
"content_scripts": [
{
"matches": [ "*://*.youtube.com/*" ],
"js": [ "popup.js" ]
}
]
}