javascript 如何在 chrome 中安装用户脚本(添加的脚本无法运行)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19298913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:04:00  来源:igfitidea点击:

How to install a userscript in chrome (added scripts cannot run)?

javascriptgoogle-chromegoogle-chrome-devtoolsuserscripts

提问by static

I'm trying to install the basic userscript from http://pastebin.com/9CXXYYBX. I open the page with extensions list in Chrome, drag&drop my extension (copied from the pastebin link above) there and accept the permissions. So, because of the line // @include http://userscripts.org/*the userscript should run only on the userscripts.org, but it doesn't.

我正在尝试从http://pastebin.com/9CXXYYBX安装基本用户脚本。我在 Chrome 中打开带有扩展程序列表的页面,将我的扩展程序(从上面的 pastebin 链接复制)拖放到那里并接受权限。因此,由于这一行// @include http://userscripts.org/*,userscript 应该只在 userscripts.org 上运行,但事实并非如此。

I have Chrome 27so as I know the userscripts should work without Greasemonkeyinstalled.

Chrome 27知道用户脚本应该在没有Greasemonkey安装的情况下工作。

What could be wrong and how to check if the userscripts are working (how to debug them so)?

可能有什么问题以及如何检查用户脚本是否正常工作(如何调试它们)?

回答by Brock Adams

The question is not clear. Are you having trouble installing the script, or running it, or just what it reports.

问题不清楚。您是否在安装或运行脚本时遇到问题,或者只是遇到了它报告的问题。

Anyway, that scriptworks fine for me.

无论如何,该脚本对我来说很好用。

To install it:

要安装它:

  1. Downloadthe file from pastebin to your local machine.
  2. Renamethe file. It comes from pastebin named crossvrowser_userscript_pattern.txt(sic), but userscripts must end in .user.js. So, rename the file to crossbrowser_userscript_pattern.user.js.
  3. Install crossbrowser_userscript_pattern.user.jsby dragging it to the extension page, per this question and answers.
  1. 将文件从 pastebin下载到本地计算机。
  2. 重命名文件。它来自名为crossvrowser_userscript_pattern.txt(sic) 的 pastebin,但用户脚本必须以.user.js. 因此,将文件重命名为crossbrowser_userscript_pattern.user.js.
  3. crossbrowser_userscript_pattern.user.js根据此问题和答案,通过将其拖到扩展页面进行安装。


Permission report:


权限报告:

Chrome will report that the userscript can operate on all web sites, but Chrome lies about scripts that use @include. It's not a problem here.

Chrome 会报告 userscript 可以在所有网站上运行,但 Chrome 会谎报使用@include. 这不是问题。


Running the script:


运行脚本:

After installing that script, it runs just as expected. When going to a page on userscripts.org, the script alerts, "Hello from the userscript," just like it should.

安装该脚本后,它会按预期运行。当转到userscripts.org的页面时,脚本会提醒“来自用户脚本的你好”,就像它应该的那样。


If the script doesn't run:


如果脚本没有运行:

Go to the extensions page (chrome://extensions/), does the script show up? You should see something like:

转到扩展页面 ( chrome://extensions/),是否显示脚本?你应该看到类似的东西:

Script installed

已安装脚本


Then when you visit a suitable pageyou will see the alert:


然后,当您访问合适的页面时,您将看到警报:

Script's alert

脚本的警报





You can also open Chrome's console (CtrlShiftI) and switch to the userscript's scope:

您还可以打开 Chrome 的控制台 ( CtrlShiftI) 并切换到用户脚本的范围

Switch to script scope

切换到脚本范围

Note that the id (pfnbaeafniclcjhfkndoploalomdmgkc) is the same as that listed on the extensions page.

请注意,id ( pfnbaeafniclcjhfkndoploalomdmgkc) 与扩展页面上列出的相同。



You can then view the script source live, add breakpoints and debugit by following the directions in this answer.

然后,您可以按照本答案中的说明实时查看脚本源代码、添加断点并对其进行调试



Debugging OP's script in action

正在调试 OP 的脚本