手动将用户脚本添加到 Google Chrome [英] Manually adding a Userscript to Google Chrome

查看:20
本文介绍了手动将用户脚本添加到 Google Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上找到了许多手动添加的教程,而不是安装"用户脚本.他们都告诉我要执行相同的步骤:

  • 创建目录 C:UsersBlablaAppDataLocalGoogleChromeUser DataDefaultUser Scripts
  • 在那里放置一个 .js 文件,其中包含用户脚本
  • 使用参数 --enable-user-scripts 启动 Chrome

我这样做了 - 但我的演示脚本没有做任何事情:

//==用户脚本==//@name 测试//@description 测试//@include http://example.com/*//@版本 1.0//==/用户脚本==警报(0);

我做错了什么?

解决方案

最好的办法是安装 .

<小时>

控制脚本和名称:

默认情况下,Chrome 会在


<小时><小时>

1 文件夹默认为:

<前>视窗 XP:Chrome : %AppData%..Local SettingsApplication DataGoogleChromeUser DataDefaultExtensionsChromium:%AppData%..Local SettingsApplication DataChromiumUser DataDefaultExtensions视窗 Vista/7/8:Chrome : %LocalAppData%GoogleChromeUser DataDefaultExtensionsChromium:%LocalAppData%ChromiumUser DataDefaultExtensionsLinux:铬:~/.config/google-chrome/Default/Extensions/Chromium:~/.config/chromium/Default/Extensions/Mac OS X:Chrome : ~/Library/Application Support/Google/Chrome/Default/Extensions/Chromium:~/Library/Application Support/Chromium/Default/Extensions/

虽然您可以通过使用 --user-data-dir= 选项运行 Chrome 来更改它.

Instead of "installing" User-Scripts I found many tutorials on the web to add it manually. All of them told me to do the same steps:

  • Make the directory C:UsersBlablaAppDataLocalGoogleChromeUser DataDefaultUser Scripts
  • Place a .js file there, which contains the User-Script
  • Start Chrome with the parameter --enable-user-scripts

I did so - but my demo script does not do anything:

// ==UserScript==
// @name           Test
// @description    Test
// @include        http://example.com/*
// @version        1.0
// ==/UserScript==

alert(0);

What am I doing wrong?

解决方案

The best thing to do is to install the Tampermonkey extension.

This will allow you to easily install Greasemonkey scripts, and to easily manage them. Also it makes it easier to install userscripts directly from sites like OpenUserJS, MonkeyGuts, etc.

Finally, it unlocks most all of the GM functionality that you don't get by installing a GM script directly with Chrome. That is, more of what GM on Firefox can do, is available with Tampermonkey.


But, if you really want to install a GM script directly, it's easy a right pain on Chrome these days...

Chrome After about August, 2014:

You can still drag a file to the extensions page and it will work... Until you restart Chrome. Then it will be permanently disabled. See Continuing to "protect" Chrome users from malicious extensions for more information. Again, Tampermonkey is the smart way to go. (Or switch browsers altogether to Opera or Firefox.)

Chrome 21+ :

Chrome is changing the way extensions are installed. Userscripts are pared-down extensions on Chrome but. Starting in Chrome 21, link-click behavior is disabled for userscripts. To install a user script, drag the **.user.js* file into the Extensions page (chrome://extensions in the address input).

Older Chrome versions:

Merely drag your **.user.js* files into any Chrome window. Or click on any Greasemonkey script-link.

You'll get an installation warning:

Click Continue.


You'll get a confirmation dialog:

Click Add.


Notes:

  1. Scripts installed this way have limitations compared to a Greasemonkey (Firefox) script or a Tampermonkey script. See Cross-browser user-scripting, Chrome section.


Controlling the Script and name:

By default, Chrome installs scripts in the Extensions folder1, full of cryptic names and version numbers. And, if you try to manually add a script under this folder tree, it will be wiped the next time Chrome restarts.

To control the directories and filenames to something more meaningful, you can:

  1. Create a directory that's convenient to you, and not where Chrome normally looks for extensions. For example, Create: C:MyChromeScripts.

  2. For each script create its own subdirectory. For example, HelloWorld.

  3. In that subdirectory, create or copy the script file. For example, Save this question's code as: HelloWorld.user.js.

  4. You must also create a manifest file in that subdirectory, it must be named: manifest.json.

    For our example, it should contain:

    {
        "manifest_version": 2,
        "content_scripts": [ {
            "exclude_globs":    [  ],
            "include_globs":    [ "*" ],
            "js":               [ "HelloWorld.user.js" ],
            "matches":          [   "https://stackoverflow.com/*",
                                    "https://stackoverflow.com/*"
                                ],
            "run_at": "document_end"
        } ],
        "converted_from_user_script": true,
        "description":  "My first sensibly named script!",
        "name":         "Hello World",
        "version":      "1"
    }
    

    The manifest.json file is automatically generated from the meta-block by Chrome, when an user script is installed. The values of @include and @exclude meta-rules are stored in include_globs and exclude_globs, @match (recommended) is stored in the matches list. "converted_from_user_script": true is required if you want to use any of the supported GM_* methods.

  5. Now, in Chrome's Extension manager (URL = chrome://extensions/), Expand "Developer mode".

  6. Click the Load unpacked extension... button.

  7. For the folder, paste in the folder for your script, In this example it is: C:MyChromeScriptsHelloWorld.

  8. Your script is now installed, and operational!

  9. If you make any changes to the script source, hit the Reload link for them to take effect:




1 The folder defaults to:

Windows XP:
  Chrome  : %AppData%..Local SettingsApplication DataGoogleChromeUser DataDefaultExtensions
  Chromium: %AppData%..Local SettingsApplication DataChromiumUser DataDefaultExtensions

Windows Vista/7/8:
  Chrome  : %LocalAppData%GoogleChromeUser DataDefaultExtensions
  Chromium: %LocalAppData%ChromiumUser DataDefaultExtensions

Linux:
  Chrome  : ~/.config/google-chrome/Default/Extensions/
  Chromium: ~/.config/chromium/Default/Extensions/

Mac OS X:
  Chrome  : ~/Library/Application Support/Google/Chrome/Default/Extensions/
  Chromium: ~/Library/Application Support/Chromium/Default/Extensions/

Although you can change it by running Chrome with the --user-data-dir= option.

这篇关于手动将用户脚本添加到 Google Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆