10000 fix: update example script · Sv443/Userscript.ts@e45fcd7 · GitHub
[go: up one dir, main page]

Skip to content

Commit e45fcd7

Browse files
committed
fix: update example script
1 parent c79d7df commit e45fcd7

File tree

1 file changed

+75
-20
lines changed

1 file changed

+75
-20
lines changed

dist/EXAMPLE.user.js

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,79 @@
1919
// ==/UserScript==
2020

2121
// this code was built in production mode with `npm run build-prod`
22-
// usually it's all in a single line, I just made it a little more readable
23-
24-
function e() {
25-
const e = `:root{--myscript-bg-col:#282c34;--myscript-fg-col:#fff}#my-example-element{background-color:var(--myscript-bg-col);color:var(--myscript-fg-col);font-family:Segoe UI,San Francisco,sans-serif;padding:10px}`;
26-
e.match(/^{{.+}}$/) || function(e) {
27-
let n = document.createElement("style");
28-
n.innerHTML = e, document.head.appendChild(n)
29-
}(e),
30-
function() {
31-
const e = document.createElement("div");
32-
e.innerHTML = '\x3c!--\n## 0.2.0\n- ...\n\n<br>\n--\x3e <h2 id="010">0.1.0</h2> <ul> <li>Added base template</li> </ul> ', document.body.appendChild(e);
33-
const n = document.querySelector("div");
34-
n && (n.innerHTML = '<div id="my-example-element"> You may import any HTML file in code and it will automatically be converted to an HTML string. Using it is as easy as creating an element and assigning its innerHTML this string. </div> ')
35-
}()
22+
23+
var __webpack_exports__ = {};
24+
25+
;// CONCATENATED MODULE: ./node_modules/@sv443-network/userutils/dist/index.mjs
26+
var h=Object.defineProperty,y=Object.defineProperties;var g=Object.getOwnPropertyDescriptors;var p=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var f=(t,e,n)=>e in t?h(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,c=(t,e)=>{for(var n in e||(e={}))w.call(e,n)&&f(t,n,e[n]);if(p)for(var n of p(e))v.call(e,n)&&f(t,n,e[n]);return t},b=(t,e)=>y(t,g(e));var T=(t,e,n)=>new Promise((r,o)=>{var i=s=>{try{a(n.next(s));}catch(m){o(m);}},u=s=>{try{a(n.throw(s));}catch(m){o(m);}},a=s=>s.done?r(s.value):Promise.resolve(s.value).then(i,u);a((n=n.apply(t,e)).next());});function S(t,e,n){return Math.max(Math.min(t,n),e)}function A(t,e,n,r,o){return Number(e)===0&&Number(r)===0?t*(o/n):(t-e)*((o-r)/(n-e))+r}function d(...t){let e,n;if(typeof t[0]=="number"&&typeof t[1]=="number")[e,n]=t;else if(typeof t[0]=="number"&&typeof t[1]!="number")e=0,n=t[0];else throw new TypeError(`Wrong parameter(s) provided - expected: "number" and "number|undefined", got: "${typeof t[0]}" and "${typeof t[1]}"`);if(e=Number(e),n=Number(n),isNaN(e)||isNaN(n))throw new TypeError(`Parameters "min" and "max" can't be NaN`);if(e>n)throw new TypeError(`Parameter "min" can't be bigger than "max"`);return Math.floor(Math.random()*(n-e+1))+e}function H(t){return x(t)[0]}function x(t){if(t.length===0)return [void 0,void 0];let e=d(t.length-1);return [t[e],e]}function I(t){let[e,n]=x(t);if(n!==void 0)return t.splice(n,1),e}function P(t){let e=[...t];if(t.length===0)return t;for(let n=e.length-1;n>0;n--){let r=Math.floor(d(0,1e4)/1e4*(n+1));[e[n],e[r]]=[e[r],e[n]];}return e}function O(){try{return unsafeWindow}catch(t){return window}}function j(t,e){var n;return (n=t.parentNode)==null||n.insertBefore(e,t.nextSibling),e}function R(t,e){let n=t.parentNode;if(!n)throw new Error("Element doesn't have a parent node");return n.replaceChild(e,t),e.appendChild(t),e}function F(t){let e=document.createElement("style");e.innerHTML=t,document.head.appendChild(e);}function W(t,e=!1){let n=t.map(r=>new Promise((o,i)=>{let u=new Image;u.src=r,u.addEventListener("load",()=>o(u)),u.addEventListener("error",a=>e&&i(a));}));return Promise.allSettled(n)}function $(t){let e=document.createElement("a");Object.assign(e,{className:"userutils-open-in-new-tab",target:"_blank",rel:"noopener noreferrer",href:t}),e.style.display="none",document.body.appendChild(e),e.click(),setTimeout(e.remove,50);}function L(t,e,n){typeof Error.stackTraceLimit=="number"&&Error.stackTraceLimit<1e3&&(Error.stackTraceLimit=1e3),function(r){element.__proto__.addEventListener=function(...o){if(!(o[0]===e&&n()))return r.apply(this,o)};}(t.__proto__.addEventListener);}function B(t,e){return L(O(),t,e)}function q(t,e=1){let n=new(window.AudioContext||window.webkitAudioContext),r={mediaElement:t,amplify:o=>{r.gain.gain.value=o;},getAmpLevel:()=>r.gain.gain.value,context:n,source:n.createMediaElementSource(t),gain:n.createGain()};return r.source.connect(r.gain),r.gain.connect(n.destination),r.amplify(e),r}function z(t,e){return (Array.isArray(e)||e instanceof NodeList)&&(e=e.length),`${t}${e===1?"":"s"}`}function U(t){return new Promise(e=>{setTimeout(e,t);})}function D(t,e=300){let n;return function(...r){clearTimeout(n),n=setTimeout(()=>t.apply(this,r),e);}}function J(n){return T(this,arguments,function*(t,e={}){let{timeout:r=1e4}=e,o=new AbortController,i=setTimeout(()=>o.abort(),r),u=yield fetch(t,b(c({},e),{signal:o.signal}));return clearTimeout(i),u})}var l=new Map;function V(t,e){let n=[];l.has(t)&&(n=l.get(t)),n.push(e),l.set(t,n),E(t,n);}function X(t){return l.delete(t)}function E(t,e){let n=[];if(e.forEach((r,o)=>{try{let i=r.all?document.querySelectorAll(t):document.querySelector(t);(i!==null&&i instanceof NodeList&&i.length>0||i!==null)&&(r.listener(i),r.continuous||n.push(o));}catch(i){console.error(`Couldn't call listener for selector '${t}'`,i);}}),n.length>0){let r=e.filter((o,i)=>!n.includes(i));r.length===0?l.delete(t):l.set(t,r);}}function Y(t={}){new MutationObserver(()=>{for(let[n,r]of l.entries())E(n,r);}).observe(document.body,c({subtree:!0,childList:!0},t));}function Z(){return l}
27+
28+
29+
30+
;// CONCATENATED MODULE: ./changelog.md
31+
// Module
32+
var code = "<!--\n## 0.2.0\n- ...\n\n<br>\n--> <h2 id=\"010\">0.1.0</h2> <ul> <li>Initial release</li> </ul> ";
33+
// Exports
34+
/* harmony default export */ const changelog = (code);
35+
;// CONCATENATED MODULE: ./src/example.html
36+
// Module
37+
var example_code = "<div id=\"my-example-element\"> You may import any HTML file in code and it will automatically be converted to an HTML string. Using it is as easy as creating an element and assigning its innerHTML this string. </div> ";
38+
// Exports
39+
/* harmony default export */ const example = (example_code);
40+
;// CONCATENATED MODULE: ./src/example.ts
41+
// all CSS files imported like this will be included in the final bundle,
42+
// and will be automatically minified and inserted into the site's <head> (by the addGlobalStyle() in onDomLoad() in `index.ts`)
43+
44+
// markdown is automatically parsed and converted to an HTML string which can be assigned to an element's innerHTML property
45+
46+
// HTML is imported as a simple string that can also be assigned to an element's innerHTML property
47+
48+
/** Gets called after DOMContentLoaded is emitted in `index.ts` */
49+
function insertExampleElements() {
50+
// add a custom element with its contents set to an imported markdown file
51+
const changelogElement = document.createElement("div");
52+
// innerHTML is usually very unsafe but in this case we know the source can be trusted
53+
changelogElement.innerHTML = changelog;
54+
document.body.appendChild(changelogElement);
55+
// replace the content of the first div found in the document with the contents of example.html
56+
const randomElement = document.querySelector("div");
57+
if (randomElement)
58+
randomElement.innerHTML = example;
59+
}
60+
61+
;// CONCATENATED MODULE: ./src/index.ts
62+
63+
64+
/**
65+
* Called whenever the script is initialized, depending on the value of `@run-at` inside the userscript header.
66+
* Note: if you set `@run-at` to something like `document-end`, the `DOMContentLoaded` event may not be called depending on the userscript extension. In this case you may remove the onDomLoad() function and modify the DOM directly in init().
67+
*/
68+
function init() {
69+
const buildNbr = "c79d7df";
70+
const buildNbrText = !buildNbr.match(/^{{.+}}$/) ? `-${buildNbr}` : "";
71+
// watermark in the console based on values grabbed out of the userscript header
72+
console.log(`${GM.info.script.name} (${GM.info.script.version}${buildNbrText}) - ${GM.info.script.namespace}`);
73+
document.addEventListener("DOMContentLoaded", onLoad);
74+
}
75+
/** In here you can freely insert or delete elements as the DOM is now guaranteed to be modifiable */
76+
function onLoad() {
77+
// this string gets replaced with the minified bundle of all imported CSS files by the script in src/tools/post-build.ts
78+
const globalStyle = `:root {
79+
--myscript-bg-col: #282c34;
80+
--myscript-fg-col: white;
81+
}
82+
83+
#my-example-element {
84+
padding: 10px;
85+
background-color: var(--myscript-bg-col);
86+
color: var(--myscript-fg-col);
87+
font-family: "Segoe UI", "San Francisco", sans-serif;
88+
}
89+
90+
`;
91+
// if no css file is imported anywhere, no bundle is emitted and so addGlobalStyle has to be skipped
92+
if (!globalStyle.match(/^{{.+}}$/))
93+
F(globalStyle);
94+
// go to this function's definition in `example.ts` for an example on how to import HTML, CSS and markdown
95+
insertExampleElements();
3696
}
37-
Object.defineProperty, Object.defineProperties, Object.getOwnPropertyDescriptors, Object.getOwnPropertySymbols, Object.prototype.hasOwnProperty, Object.prototype.propertyIsEnumerable, new Map,
38-
function() {
39-
const n = "62946e1",
40-
t = n.match(/^{{.+}}$/) ? "" : `-${n}`;
41-
console.log(`${GM.info.script.name} (${GM.info.script.version}${t}) - ${GM.info.script.namespace}`), document.addEventListener("DOMContentLoaded", e)
42-
}();
97+
init();

0 commit comments

Comments
 (0)
0