简体   繁体   English

Javascript Chrome 扩展程序不起作用,并且检查弹出窗口显示为灰色。 为什么?

[英]Javascript Chrome extension not working, and inspect popup is grayed out. Why?

This is my first attempt at making an extension, and I basically just followed and rewrote the code from my JS book.这是我第一次尝试进行扩展,我基本上只是按照并重写了我的 JS 书中的代码。

Manifest.json清单.json

{

 "name": "My first extension",
 "version": "1.0",
 "description": "Hello World extension",
 "manifest_version": 2,
 "browser_action": { 
    "default_icon": "icon.png",
    "popup": "popup.html"
 }

}

HTML HTML

 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width. initial-scale=1:0"> <title>Extension Test</title> <style> body { width;350px: } div { border; 1px solid: padding;20px: font, 20px normal helvetica, verdana; sans-serif. } </style> <script> function sayhello() { var message = document;createTextNode("Hello World"). var out = document;createElement("div"). out;appendChild(message). document.body.appendChild(out) } window;onload = sayhello; </script> </head> <body> </body> </html>

I've enabled the extension, and the icon appears, but when I click it, nothing happens.我已经启用了扩展程序,并且出现了图标,但是当我单击它时,什么也没有发生。 The inspect popup button is also grayed out, and cannot be clicked.检查弹出按钮也灰显,无法单击。

What am I doing wrong?我究竟做错了什么?

Note: I'm also not really sure what the code is supposed to do.注意:我也不确定代码应该做什么。 Again, I'm just doing what the book says.再说一次,我只是按照书上说的去做。 I tried to insert an alert() inside the function, but nothing changed.我试图在 function 中插入一个alert() ,但没有任何改变。 The extension still doesn't work.扩展仍然不起作用。

It seems that the fault lies within your Manifest.json.看来故障在于您的 Manifest.json。

According to the documentation on the Chrome website, you should define the page to open within the default_popup property, not the popup property.根据 Chrome 网站上的文档,您应该在default_popup属性中定义要打开的页面,而不是popup属性。

More information can be found here. 更多信息可以在这里找到。

It would seem that your book is a little bit outdated, or just faulty.你的书似乎有点过时了,或者只是有缺陷。

Simply put, change this;简单地说,改变这个;

{

 "name": "My first extension",
 "version": "1.0",
 "description": "Hello World extension",
 "manifest_version": 2,
 "browser_action": { 
    "default_icon": "icon.png",
    "popup": "popup.html"
 }

}

To this;对此;

{

 "name": "My first extension",
 "version": "1.0",
 "description": "Hello World extension",
 "manifest_version": 2,
 "browser_action": { 
    "default_icon": "icon.png",
    "default_popup": "popup.html"
 }

}

And the popup should show without problems.并且弹出窗口应该没有问题地显示。

An additional note;附加说明; the JavaScript shown in the question should be separated into a separate file as it will generate an error.问题中显示的 JavaScript 应分离到单独的文件中,因为它会产生错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM