Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 542
Joren Avatar asked Oct 07 '10 18:10

Joren


2 Answers

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.

like image 97
Brock Adams Avatar answered Oct 24 '22 16:10

Brock Adams


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.

like image 38
Greegko Avatar answered Oct 24 '22 16:10

Greegko