'tampermonkey script loading local files

I created a tool using HTML, CSS and JS. I would like this tool to be accessible with TamperMonkey. I found a TamperMonkey script where the creator did not add his code directly into the script but leveraged on the @require lines to link his web hosted code files.

I am trying to reproduce this, but instead of adding https links I would like to start with my local files.

Here is his code (anonymized):

// ==UserScript==
// @name         name
// @version      version
// @author       author
// @description  description
// @match        URL where the tool will be used
// @require      https://hiscodelink1.js?raw=1
// @require      https://hiscodelink2.js?raw=1
// @require      https://hiscodelink3.class.js?raw=1

// ==/UserScript==
// See https://hiscodelink1.js?raw=1

Here is mine (anonymized):

// ==UserScript==
// @name         name
// @version      version
// @author       author
// @description  description
// @match        URL where the tool will be used (using the same to test)
// @require      /Users/myname/Desktop/Tools/mycode1.js
// @require      /Users/myname/Desktop/Tools/mycode2.js
// @require      /Users/myname/Desktop/Tools/mycode3.class.js

// ==/UserScript==
// See /Users/myname/Desktop/Tools/mycode1.js

Since it doesn't work with my own code, I am first trying to get his own tool to work but with my local links. And despite using the same @match URL and same code content, it still doesn't work. Note that I updated the path to all files everywhere and constantly.

I have tried writing my local URL this way: /Users/myname/Desktop/Tools/mycode1.js

And this way: file:///Users/myname/Desktop/Tools/mycode1.js

I have given access to my local files to TamperMonkey.

What am I doing wrong?

Thanks a lot



Solution 1:[1]

the require for local files should look something like:

// @require file://C:\path\to\userscript.user.js

for mac

// @require file:///path/to/userscript.user.js

If you can't get local files to work you can host you code from a different website instead of locally? I find that much easier for version control and so it won't have to take up my computer file space.

For example you could use something like github or pastebin.

For the //@require you would do something like the following(github):

// @require https://cdn.jsdelivr.net/gh/name/repo-name@latest/codefilename.min.js

although I think you can just do:

// @require https://raw.githubusercontent.com/name/repo/main/filename

or // @require https://github.com/name/repo/main/filename

For Pastebin:

// @require https://pastebin.com/raw/thepartofthelink

For the hosting from different websites option if you don't want to use // @require and all that stuff you can do something like // @grant GM_xmlhttpRequest

GM_xmlhttpRequest({ method : "GET",

url : "https://cdn.jsdelivr.net/gh/name/repo-name@latest/codefilename.min.js",

onload : (ev) => { let e = document.createElement('script');e.innerText = ev.responseText;document.head.appendChild(e); }});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LightLord