Update a greasemonkey script in chrome without reinstalling?

佐手、 提交于 2019-12-03 06:10:04

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/3884794/update-a-greasemonkey-script-in-chrome-without-reinstalling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!