How to prevent @require from caching external js scripts

I'm currently trying to figure out how I can include the javascript that I have on my web server in my / greasemonkey script and make it reload the script every time the username is called.

I am editing a script on my web server and I really don't want to reinstall custom text every time I make changes to the included script.

Is there any way to solve this problem? I was looking for an answer, but so far no luck.

So my pointer will look like this:

// ==UserScript== // @id HET // @name SettingsHandler // @version 1.0 // @namespace HET // @require http://urltoscript/scripts/he/lib.js // @run-at document-end // ==/UserScript== 

and my external script looks like this:

 alert('got it'); 

Still very easy to test. This setup works, but only for the first time, and when I change my lib.js script, then the custom text is still reading the old one. Is there a way to prevent the use of usercript from caching an external script? Or is there another metatag that can help me?

Thanks in advance, Dave

+7
source share
4 answers

You don't know how to do this with GM / userscript directives, but you can easily add a script yourself and add a timestamp to the URL to prevent browser caching:

 var remoteScript = document.createElement('script'); remoteScript.src = 'http://domain.com/path/to/script.js?ts='+(+new Date()); remoteScript.onload = init; document.body.appendChild(remoteScript); function init() { ... do stuff } 
+4
source

You can add the following to your .htaccess file:

 <FilesMatch "filename.js"> Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" Header set Pragma "no-cache" Header set Expires "0" </FilesMatch> 

And delete it when you finish development.

0
source

Here is the only real answer https://github.com/Tampermonkey/tampermonkey/issues/475

Option 4 is recommended. However, they load asynchronously, so the download order may vary.

There are several ways to relieve your pain. :)

  1. You can increase the version number before saving the script and all external resources will be rebooted.
  2. After setting "Setup Mode" to "Advanced", you can configure the external update interval. Note: "Always" means "after using the resource." So you may need to execute / load the page twice.
  3. If you use Tampermonkey Beta (Chrome or Firefox) you can edit the external resource in place (because there is now an edit button, except for the delete button).
  4. Copy resources and store them in place. Once you have enabled "Local File Access" in the Chrome Extension Control Page or the Tampermonkey settings page (if you use Firefox) you can @require them through the local file: // URI.
0
source

The answer from Rob M. does not work for me, because the location of the Tampermonkey script and the destination site where it was entered may differ. For example, in my case, I also have a locally working web server for development in the IDE using Tampermonkey for Firefox without having to access the file system for Tampermonkey from a browser.

This script must be embedded on a third-party site, for example example.com, where it applies modifications. Thus, the browser will block this script, since it is in a different domain than example.com.

During development, I would like my cached script to apply the changes immediately. To work around this, load the contents of the scripts using 'GM.xmlHttpRequest. In addition, the GET parameter with the current timestamp acts as a cache buffer:

 let url = 'http://localhost/myscript.js?ts=${(+new Date())}' GM.xmlHttpRequest({ method: "GET", url: url, onload: function(response) { let remoteScript = document.createElement('script') remoteScript.id = 'tm-dev-script' remoteScript.innerHTML = response.responseText document.body.appendChild(remoteScript) } }) 

Please note that since GM.xmlHttpRequest can bypass the same origin policy, access must be explicitly indicated in the header of your script:

 // @grant GM.xmlHttpRequest 
0
source

All Articles