Update a greasemonkey script in chrome without reinstalling?
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?
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:
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.For each script create its own subdirectory. For example,
Script_1
,Script_2
, etc.Each subdirectory should contain its script, EG
MyScript1.user.js
, and any files used by that script.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" }
Now, in Chrome's Extension manager (URL = chrome://extensions/), Expand Developer mode.
Click the Load unpacked extension... button.
For the folder, paste in the folder for your script, For example:
C:\MyChromeScripts\Script_1
.Your script will be installed and operational.
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.
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.
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.
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
An easy way to do this is to use two files:
- A
userscript.user.js
with GM headers which loadsscript.js
. You can make it purge the cache every day (tell it to loadscript.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.
精彩评论