@sv443-network/userutils 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @sv443-network/userutils
2
2
 
3
+ ## 0.5.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 18d4a10: make npm bundle smaller
8
+
3
9
  ## 0.5.1
4
10
 
5
11
  ### Patch Changes
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@sv443-network/userutils",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "Library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, modify the DOM more easily and more ",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
8
8
  "scripts": {
9
9
  "lint": "tsc && eslint .",
10
- "build-common": "tsup lib/index.ts --format cjs,esm,iife --dts --clean --treeshake",
10
+ "build-common": "tsup lib/index.ts --format cjs,esm --dts --clean --treeshake",
11
+ "build-iife": "tsup lib/index.ts --format cjs,esm,iife --dts --clean --treeshake --minify",
11
12
  "build": "npm run build-common -- --minify",
12
13
  "dev": "npm run build-common -- --sourcemap --watch",
13
14
  "publish-package": "npm run build && changeset publish"
@@ -39,9 +40,14 @@
39
40
  "tsup": "^7.2.0",
40
41
  "typescript": "^5.1.6"
41
42
  },
42
- "browserslist": [
43
- "last 1 version",
44
- "> 1%",
45
- "not dead"
43
+ "files": [
44
+ "/dist/index.js",
45
+ "/dist/index.mjs",
46
+ "/dist/index.d.ts",
47
+ "/dist/index.d.mts",
48
+ "/package.json",
49
+ "/README.md",
50
+ "/CHANGELOG.md",
51
+ "/LICENSE.txt"
46
52
  ]
47
53
  }
@@ -1,75 +0,0 @@
1
- ## UserUtils
2
- Library with various utilities for userscripts - register listeners for when CSS selectors exist, intercept events, modify the DOM more easily and more.
3
- Contains builtin TypeScript declarations. Webpack compatible and supports ESM and CJS.
4
- Licensed under the [MIT license.](https://github.com/Sv443-Network/UserUtils/blob/main/LICENSE.txt)
5
-
6
- If you like using this library, please consider [supporting the development ❤️](https://github.com/sponsors/Sv443)
7
-
8
- <br>
9
-
10
- ### Full documentation [on GitHub](https://github.com/Sv443-Network/UserUtils#readme)
11
-
12
- <br>
13
-
14
- ## Features:
15
- - DOM:
16
- - [onSelector()](https://github.com/Sv443-Network/UserUtils#onselector) - call a listener once a selector is found in the DOM
17
- - [initOnSelector()](https://github.com/Sv443-Network/UserUtils#initonselector) - needs to be called once to be able to use `onSelector()`
18
- - [getSelectorMap()](https://github.com/Sv443-Network/UserUtils#getselectormap) - returns all currently registered selectors, listeners and options
19
- - [getUnsafeWindow()](https://github.com/Sv443-Network/UserUtils#getunsafewindow) - get the unsafeWindow object or fall back to the regular window object
20
- - [insertAfter()](https://github.com/Sv443-Network/UserUtils#insertafter) - insert an element as a sibling after another element
21
- - [addParent()](https://github.com/Sv443-Network/UserUtils#addparent) - add a parent element around another element
22
- - [addGlobalStyle()](https://github.com/Sv443-Network/UserUtils#addglobalstyle) - add a global style to the page
23
- - [preloadImages()](https://github.com/Sv443-Network/UserUtils#preloadimages) - preload images into the browser cache for faster loading later on
24
- - [openInNewTab()](https://github.com/Sv443-Network/UserUtils#openinnewtab) - open a link in a new tab
25
- - [interceptEvent()](https://github.com/Sv443-Network/UserUtils#interceptevent) - conditionally intercepts events registered by `addEventListener()` on any given EventTarget object
26
- - [interceptWindowEvent()](https://github.com/Sv443-Network/UserUtils#interceptwindowevent) - conditionally intercepts events registered by `addEventListener()` on the window object
27
- - [amplifyMedia()](https://github.com/Sv443-Network/UserUtils#amplifymedia) - amplify an audio or video element's volume past the maximum of 100%
28
- - Math:
29
- - [clamp()](https://github.com/Sv443-Network/UserUtils#clamp) - constrain a number between a min and max value
30
- - [mapRange()](https://github.com/Sv443-Network/UserUtils#maprange) - map a number from one range to the same spot in another range
31
- - [randRange()](https://github.com/Sv443-Network/UserUtils#randrange) - generate a random number between a min and max boundary
32
- - Misc:
33
- - [autoPlural()](https://github.com/Sv443-Network/UserUtils#autoplural) - automatically pluralize a string
34
- - [pauseFor()](https://github.com/Sv443-Network/UserUtils#pausefor) - pause the execution of a function for a given amount of time
35
- - [debounce()](https://github.com/Sv443-Network/UserUtils#debounce) - call a function only once, after a given amount of time
36
- - [fetchAdvanced()](https://github.com/Sv443-Network/UserUtils#fetchadvanced) - wrapper around the fetch API with a timeout option
37
- - Arrays:
38
- - [randomItem()](https://github.com/Sv443-Network/UserUtils#randomitem) - returns a random item from an array
39
- - [randomItemIndex()](https://github.com/Sv443-Network/UserUtils#randomitemindex) - returns a tuple of a random item and its index from an array
40
- - [takeRandomItem()](https://github.com/Sv443-Network/UserUtils#takerandomitem) - returns a random item from an array and mutates it to remove the item
41
- - [randomizeArray()](https://github.com/Sv443-Network/UserUtils#randomizearray) - returns a copy of the array with its items in a random order
42
-
43
- <br><br>
44
-
45
- ## Installation:
46
- - If you are using a bundler like webpack, you can install this package using npm:
47
- ```
48
- npm i @sv443-network/userutils
49
- ```
50
-
51
- Then, import it in your script as usual:
52
- ```ts
53
- import { addGlobalStyle } from "@sv443-network/userutils";
54
- // or
55
- import * as userUtils from "@sv443-network/userutils";
56
- ```
57
-
58
- Shameless plug: I also have a [webpack-based template for userscripts in TypeScript](https://github.com/Sv443/Userscript.ts) that you can use to get started quickly.
59
-
60
- <br>
61
-
62
- - If you are not using a bundler, you can include the latest release from GreasyFork by adding this directive to the userscript header:
63
- ```
64
- // @require https://greasyfork.org/scripts/472956-userutils/code/UserUtils.js
65
- ```
66
-
67
- Then, access the functions on the global variable `UserUtils`:
68
- ```ts
69
- UserUtils.addGlobalStyle("body { background-color: red; }");
70
-
71
- // or using object destructuring:
72
-
73
- const { clamp } = UserUtils;
74
- console.log(clamp(1, 5, 10); // 5
75
- ```
@@ -1,33 +0,0 @@
1
- (function (exports) {
2
- 'use strict';
3
-
4
- 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=(n,e,t)=>e in n?h(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,m=(n,e)=>{for(var t in e||(e={}))w.call(e,t)&&f(n,t,e[t]);if(p)for(var t of p(e))v.call(e,t)&&f(n,t,e[t]);return n},b=(n,e)=>y(n,g(e));var x=(n,e,t)=>new Promise((r,o)=>{var i=s=>{try{a(t.next(s));}catch(l){o(l);}},u=s=>{try{a(t.throw(s));}catch(l){o(l);}},a=s=>s.done?r(s.value):Promise.resolve(s.value).then(i,u);a((t=t.apply(n,e)).next());});function A(n,e,t){return Math.max(Math.min(n,t),e)}function N(n,e,t,r,o){return Number(e)===0&&Number(r)===0?n*(o/t):(n-e)*((o-r)/(t-e))+r}function d(...n){let e,t;if(typeof n[0]=="number"&&typeof n[1]=="number")[e,t]=n;else if(typeof n[0]=="number"&&typeof n[1]!="number")e=0,t=n[0];else throw new TypeError(`Wrong parameter(s) provided - expected: "number" and "number|undefined", got: "${typeof n[0]}" and "${typeof n[1]}"`);if(e=Number(e),t=Number(t),isNaN(e)||isNaN(t))throw new TypeError(`Parameters "min" and "max" can't be NaN`);if(e>t)throw new TypeError(`Parameter "min" can't be bigger than "max"`);return Math.floor(Math.random()*(t-e+1))+e}function H(n){return T(n)[0]}function T(n){if(n.length===0)return [void 0,void 0];let e=d(n.length-1);return [n[e],e]}function I(n){let[e,t]=T(n);if(t!==void 0)return n.splice(t,1),e}function C(n){let e=[...n];if(n.length===0)return n;for(let t=e.length-1;t>0;t--){let r=Math.floor(d(0,1e4)/1e4*(t+1));[e[t],e[r]]=[e[r],e[t]];}return e}function L(){try{return unsafeWindow}catch(n){return window}}function j(n,e){var t;return (t=n.parentNode)==null||t.insertBefore(e,n.nextSibling),e}function R(n,e){let t=n.parentNode;if(!t)throw new Error("Element doesn't have a parent node");return t.replaceChild(e,n),e.appendChild(n),e}function F(n){let e=document.createElement("style");e.innerHTML=n,document.head.appendChild(e);}function W(n,e=!1){let t=n.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(t)}function $(n){let e=document.createElement("a");Object.assign(e,{className:"userutils-open-in-new-tab",target:"_blank",rel:"noopener noreferrer",href:n}),e.style.display="none",document.body.appendChild(e),e.click(),setTimeout(e.remove,50);}function M(n,e,t){typeof Error.stackTraceLimit=="number"&&Error.stackTraceLimit<1e3&&(Error.stackTraceLimit=1e3),function(r){element.__proto__.addEventListener=function(...o){if(!(o[0]===e&&t()))return r.apply(this,o)};}(n.__proto__.addEventListener);}function q(n,e){return M(L(),n,e)}function G(n,e=1){let t=new(window.AudioContext||window.webkitAudioContext),r={mediaElement:n,amplify:o=>{r.gain.gain.value=o;},getAmpLevel:()=>r.gain.gain.value,context:t,source:t.createMediaElementSource(n),gain:t.createGain()};return r.source.connect(r.gain),r.gain.connect(t.destination),r.amplify(e),r}function B(n,e){return (Array.isArray(e)||e instanceof NodeList)&&(e=e.length),`${n}${e===1?"":"s"}`}function U(n){return new Promise(e=>{setTimeout(e,n);})}function D(n,e=300){let t;return function(...r){clearTimeout(t),t=setTimeout(()=>n.apply(this,r),e);}}function J(t){return x(this,arguments,function*(n,e={}){let{timeout:r=1e4}=e,o=new AbortController,i=setTimeout(()=>o.abort(),r),u=yield fetch(n,b(m({},e),{signal:o.signal}));return clearTimeout(i),u})}var c=new Map;function V(n,e){let t=[];c.has(n)&&(t=c.get(n)),t.push(e),c.set(n,t),E(n,t);}function X(n){return c.delete(n)}function E(n,e){let t=[];if(e.forEach((r,o)=>{try{let i=r.all?document.querySelectorAll(n):document.querySelector(n);(i!==null&&i instanceof NodeList&&i.length>0||i!==null)&&(r.listener(i),r.continuous||t.push(o));}catch(i){console.error(`Couldn't call listener for selector '${n}'`,i);}}),t.length>0){let r=e.filter((o,i)=>!t.includes(i));r.length===0?c.delete(n):c.set(n,r);}}function Y(n={}){new MutationObserver(()=>{for(let[t,r]of c.entries())E(t,r);}).observe(document.body,m({subtree:!0,childList:!0},n));}function Z(){return c}
5
-
6
- exports.addGlobalStyle = F;
7
- exports.addParent = R;
8
- exports.amplifyMedia = G;
9
- exports.autoPlural = B;
10
- exports.clamp = A;
11
- exports.debounce = D;
12
- exports.fetchAdvanced = J;
13
- exports.getSelectorMap = Z;
14
- exports.getUnsafeWindow = L;
15
- exports.initOnSelector = Y;
16
- exports.insertAfter = j;
17
- exports.interceptEvent = M;
18
- exports.interceptWindowEvent = q;
19
- exports.mapRange = N;
20
- exports.onSelector = V;
21
- exports.openInNewTab = $;
22
- exports.pauseFor = U;
23
- exports.preloadImages = W;
24
- exports.randRange = d;
25
- exports.randomItem = H;
26
- exports.randomItemIndex = T;
27
- exports.randomizeArray = C;
28
- exports.removeOnSelector = X;
29
- exports.takeRandomItem = I;
30
-
31
- return exports;
32
-
33
- })({});