Skip to content

Instantly share code, notes, and snippets.

@jpcaparas
Last active December 16, 2024 23:54
Show Gist options
  • Save jpcaparas/e8257fca97e2fad44a43c34668810244 to your computer and use it in GitHub Desktop.
Save jpcaparas/e8257fca97e2fad44a43c34668810244 to your computer and use it in GitHub Desktop.
Tampermonkey: Load an external script and CSS
// ==UserScript==
// @name HackerNews
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://news.ycombinator.com/
// @require https://bennettfeely.com/ztext/js/ztext.min.js
// @resource REMOTE_CSS http://127.0.0.1:8080/style.css
// @grant GM_xmlhttpRequest
// @grant GM_getResourceText
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
// Load remote JS
GM_xmlhttpRequest({
method : "GET",
// from other domain than the @match one (.org / .com):
url : "https://bennettfeely.com/ztext/js/ztext.min.js",
onload : (ev) =>
{
let e = document.createElement('script');
e.innerText = ev.responseText;
document.head.appendChild(e);
}
});
// Load remote CSS
// @see https://github.com/Tampermonkey/tampermonkey/issues/835
const myCss = GM_getResourceText("REMOTE_CSS");
GM_addStyle(myCss);
// document.querySelector('.hnname').setAttribute('data-z', true)
// document.querySelector('.hnname').setAttribute('data-z-layers', 3);
setTimeout(function() {
var ztxt = new Ztextify(".hnname", {
depth: "30px",
layers: 8,
fade: true,
direction: "forwards",
event: "pointer",
eventRotation: "35deg"
});
}, 3000);
})();
@Tao309
Copy link

Tao309 commented Mar 31, 2023

let e = document.createElement('script');
e.type = 'module';
e.innerText = ev.responseText;
document.head.appendChild(e);

@iamqiz
Copy link

iamqiz commented Jan 1, 2024

thanks !

@sleaze
Copy link

sleaze commented Aug 13, 2024

In case it helps others (or my future self... yeah, I've run into this numerous times):

If you get an error such as:

Uncaught (in promise) ReferenceError: GM_getResourceText is not defined

You very probably have a stray:

// @grant none

Somewhere in the top header of your greasemonkey / tampermonkey script.

@ismael1222
Copy link

In case it helps others (or my future self... yeah, I've run into this numerous times):

If you get an error such as:

Uncaught (in promise) ReferenceError: GM_getResourceText is not defined

You very probably have a stray:

// @grant none

Somewhere in the top header of your greasemonkey / tampermonkey script.

OMG THANKS im not even related to this repo or anything but i got this error and i idnt knw the souion lol, i was overthinking it, really thanks for saying that ( i had a stray grant none :( )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment