更新 Chrome 中的 Greasemonkey 脚本而不重新安装?

发布于 2024-09-26 17:18:13 字数 79 浏览 2 评论 0原文

我只是希望能够保存、刷新页面,并像在 Firefox 中一样显示我的更改。每次都必须将其拖过来并安装,这很烦人。

有什么想法吗?

I just want to be able to save, refresh the page, and have my changes show up like I do in Firefox. Having to drag it over and install it every time gets annoying.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

隱形的亼 2024-10-03 17:18:13

是的,有一种方法可以做到这一点。使用这个精彩答案中采用的方法(^ _^),您可以就地编辑脚本,只需点击“重新加载”链接即可使更改生效。

此方法还有一个额外的好处,即 Chrome 的扩展程序文件夹1不会因难以辨认的文件夹和文件名而变得混乱,并且您可以轻松找到要调整的脚本。

以下是如何设置您的工作环境以允许轻松更新脚本:

  1. 创建一个您方便的目录,并且不是 Chrome 通常查找扩展程序的目录 1..  例如,创建:C:\MyChromeScripts\
    这将是您将来所有脚本的驻留位置。

  2. 为每个脚本创建自己的子目录。  例如,Script_1Script_2 等。

  3. 每个子目录都应包含其脚本、EG MyScript1.user.js 以及任何文件由该脚本使用。

  4. 现在秘密武器...您还必须在该子目录中创建一个清单文件,并且它必须命名为:manifest.json

    示例内容至少:

    <前><代码>{
    “清单版本”:2,
    “内容脚本”:[ {
    “排除_globs”:[],
    “include_globs”:[“*”],
    "js": [ "MyScript1.user.js" ],
    “匹配”:[“https://stackoverflow.com/*”,
    “https://stackoverflow.com/*”
    ]
    }],
    "description": "此脚本执行 X、Y 和 Z。",
    "name": "我的脚本编号 1",
    “版本”:“1”
    }

  5. 现在,在 Chrome 的扩展程序管理器 (URL = chrome://extensions/) 中,展开开发者模式

  6. 单击加载解压的扩展...按钮。

  7. 对于文件夹,请粘贴脚本的文件夹,例如:
    C:\MyChromeScripts\Script_1

  8. 您的脚本将被安装并可运行。

  9. 保持“扩展”页面打开。现在,当您保存对 *.js 文件的任何更改时,请点击重新加载链接以使它们立即生效。

  10. 享受吧!


1 扩展文件夹默认为:

Windows XP:
C:\Documents and Settings\***{username}***\Local Settings\Application Data\Google\Chrome\User Data\Default

Windows Vista:
C:\Users\***{username}***\AppData\Local\Google\Chrome\User Data\Default

Windows 7:
C:\Users\***{username}***\AppData\Local\Google\Chrome\User Data\Default

尽管您可以通过使用 --user-data-dir= 选项运行 Chrome 来更改它。

Yes, there is a way to do this. Using the method adopted from this brilliant answer (^_^), you can edit scripts -- in place -- and just hit a "Reload" link for the changes to take effect.

This method has the added bonus that Chrome's Extensions folder1 doesn't get cluttered with indecipherable folder and file names, and you can easily find the script you want to tweak.

Here's how to set up your work environment to allow easy script updating:

  1. Create a directory that's convenient to you, and not where Chrome normally looks for extensions1.   For example, Create: C:\MyChromeScripts\.
    This will be where all your scripts will reside in the future.

  2. For each script create its own subdirectory.   For example, Script_1, Script_2, etc.

  3. Each subdirectory should contain its script, EG MyScript1.user.js, and any files used by that script.

  4. Now for the secret sauce... You must also create a manifest file in that subdirectory, and it must be named: manifest.json.

    Sample contents, at a minimum:

    {
        "manifest_version": 2,
        "content_scripts": [ {
            "exclude_globs":    [  ],
            "include_globs":    [ "*" ],
            "js":               [ "MyScript1.user.js" ],
            "matches":          [   "https://stackoverflow.com/*",
                                    "https://stackoverflow.com/*"
                                ]
        } ],
        "description":  "This script does X, Y, and Z.",
        "name":         "My script number 1",
        "version":      "1"
    }
    

  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, For example:
    C:\MyChromeScripts\Script_1.

  8. Your script will be installed and operational.

  9. Keep the Extensions page open. Now, when you save any changes to your *.js file, Hit the Reload link for them to take effect immediately.

  10. Enjoy!


1 The extensions folder defaults to:

Windows XP:
C:\Documents and Settings\***{username}***\Local Settings\Application Data\Google\Chrome\User Data\Default

Windows Vista:
C:\Users\***{username}***\AppData\Local\Google\Chrome\User Data\Default

Windows 7:
C:\Users\***{username}***\AppData\Local\Google\Chrome\User Data\Default

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

洋洋洒洒 2024-10-03 17:18:13

如果您有可以导入更改后的文件的服务器,则可以这样做。

像这样,

// a function that loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://server.url/Custom/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

function addScript(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://server.url/Custom/Custom script.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

// load jQuery and execute the init function
addJQuery(addScript);

这首先加载 jQuery,然后加载我的自定义脚本,当我运行网页时,它总是重新加载整个脚本。

也许这对你也有帮助。

You can do that if you have somewhere server where you can import your changed file.

Like this

// a function that loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://server.url/Custom/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

function addScript(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://server.url/Custom/Custom script.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

// load jQuery and execute the init function
addJQuery(addScript);

This loads first jQuery then my custom script, when i run the webpage it always reloads the whole script.

Maybe this helps you too.

请止步禁区 2024-10-03 17:18:13

如果您不想重新安装脚本,可以重新启动chrome。
这应该会使您的更改生效。

我同意这很烦人

If you don't want to reinstall the script, you can restart chrome.
This should make your changes to take effect.

I agree this is very annoying

冰火雁神 2024-10-03 17:18:13

一个简单的方法是使用两个文件:

  • 一个 userscript.user.js ,其中包含加载 script.js 的 GM 标头。您可以让它每天清除缓存(告诉它加载添加了随机查询字符串的 script.js
  • 包含实际脚本的 script.js

例如,我已经在此处完成了该操作

//#!userscript.user.js

// ==UserScript==
//GM headers
// ==/UserScript==


(function(){
     var d=new Date();
     var script = document.createElement("script");
     script.type = "text/javascript";
     script.src="script.js?purge="+d.getMonth()+""+d.getDay(); //appends a new query string every day. This evades browser cache
     document.body.appendChild(script); //you can append to `head` as well, I used body here sine I needed jQuery already loaded
})();

该脚本加载 script.js?purge=xyz,其中 xyz 取决于日期。清除查询字符串可确保脚本仅缓存一天而不是更多 - 到第二天,它将重新加载。对于开发用途,"script.js?purge="+Math.random() 更好。如果脚本位于您的本地计算机上,则您无需担心自己的缓存。

现在,如果您对 script.js 进行更改,到明天每个人都将使用更新的版本。

An easy way to do this is to use two files:

  • A userscript.user.js with GM headers which loads script.js. You can make it purge the cache every day (tell it to load script.js with a random query string added)
  • script.js which contains the actual script.

For example, I've done that here

//#!userscript.user.js

// ==UserScript==
//GM headers
// ==/UserScript==


(function(){
     var d=new Date();
     var script = document.createElement("script");
     script.type = "text/javascript";
     script.src="script.js?purge="+d.getMonth()+""+d.getDay(); //appends a new query string every day. This evades browser cache
     document.body.appendChild(script); //you can append to `head` as well, I used body here sine I needed jQuery already loaded
})();

The script loads script.js?purge=xyz, where xyz depends on the date. The purge query string makes sure that the script is only cached for a day and not more--by next day, it will be loaded afresh. For development uses, "script.js?purge="+Math.random() is better. If the script is on your local machine though, you need not worry about the cache for yourself.

Now, if you make a change to script.js, by tomorrow everyone will be using the updated version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文