@jeniex/utils 1.0.21 → 1.0.23

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/browser/index.cjs CHANGED
@@ -130,11 +130,11 @@ if (typeof GM !== 'undefined') {
130
130
  infoObject = GM.info;
131
131
  // eslint-disable-next-line unicorn/no-negated-condition
132
132
  }
133
- else if (typeof GM_info !== 'undefined') {
134
- infoObject = GM_info;
133
+ else if (typeof GM_info === 'undefined') {
134
+ infoObject = { script: { name: document.title } };
135
135
  }
136
136
  else {
137
- infoObject = { script: { name: document.title } };
137
+ infoObject = GM_info;
138
138
  }
139
139
  const scriptName = infoObject.script.name;
140
140
  /** The identifier of the script to be used in logging. */
@@ -286,6 +286,26 @@ function setSearchParam(key, value, fullURL) {
286
286
  }
287
287
  return updatedURL.join('');
288
288
  }
289
+ /**
290
+ * @param keys The names of the param in the search query.
291
+ * @param fullURL If provided, it will be our target, otherwise it is the current location.
292
+ */
293
+ function removeSearchParams(keys, fullURL) {
294
+ const updatedURL = [];
295
+ const { origin, pathname, search, hash } = new URL(fullURL ?? window.location.href);
296
+ const searchParams = new URLSearchParams(search);
297
+ for (const key of keys) {
298
+ searchParams.delete(key);
299
+ }
300
+ updatedURL.push(origin, pathname);
301
+ if (searchParams.toString() !== '') {
302
+ updatedURL.push('?', searchParams.toString());
303
+ }
304
+ if (hash !== '') {
305
+ updatedURL.push(hash);
306
+ }
307
+ return updatedURL.join('');
308
+ }
289
309
 
290
310
  function addStyle(css, parent = document.documentElement) {
291
311
  const style = document.createElement('style');
@@ -341,6 +361,15 @@ async function pageLoad(completely) {
341
361
  });
342
362
  }
343
363
 
364
+ function saveFile(blob, fileName) {
365
+ const blobURL = URL.createObjectURL(blob);
366
+ const link = document.createElement('a');
367
+ link.href = blobURL;
368
+ link.download = fileName;
369
+ link.dispatchEvent(new MouseEvent('click'));
370
+ window.setTimeout(() => window.URL.revokeObjectURL(blobURL), 1000);
371
+ }
372
+
344
373
  function createFlagMessage(flag) {
345
374
  const { message: messageHeader, values, type } = flag;
346
375
  let message = messageHeader;
@@ -446,6 +475,8 @@ exports.padZeros = padZeros;
446
475
  exports.pageLoad = pageLoad;
447
476
  exports.prompt = prompt;
448
477
  exports.remove = remove;
478
+ exports.removeSearchParams = removeSearchParams;
479
+ exports.saveFile = saveFile;
449
480
  exports.scriptName = scriptName;
450
481
  exports.setSearchParam = setSearchParam;
451
482
  exports.sleep = sleep;
@@ -100,6 +100,11 @@ declare function getSearchParam(key: string, fullURL?: string): string | undefin
100
100
  * @param fullURL If provided, it will be our target, otherwise it is the current location.
101
101
  */
102
102
  declare function setSearchParam(key: string, value: string, fullURL?: string): string;
103
+ /**
104
+ * @param keys The names of the param in the search query.
105
+ * @param fullURL If provided, it will be our target, otherwise it is the current location.
106
+ */
107
+ declare function removeSearchParams(keys: string[], fullURL?: string): string;
103
108
 
104
109
  declare const scriptName: string;
105
110
  /** The identifier of the script to be used in logging. */
@@ -120,6 +125,8 @@ declare function imageLoad(img: HTMLImageElement): Promise<void>;
120
125
  */
121
126
  declare function pageLoad(completely?: boolean): Promise<void>;
122
127
 
128
+ declare function saveFile(blob: Blob, fileName: string): void;
129
+
123
130
  interface FlagBase {
124
131
  name: string;
125
132
  message: string;
@@ -146,4 +153,4 @@ type Options = Record<string, FlagValue>;
146
153
 
147
154
  declare function getOptions(flags: Flag[]): Promise<Options>;
148
155
 
149
- export { $, $$, Flag, Fn, Nullable, Prettify, addStyle, alert, asserted, capitalize, confirm, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, logId, noop, padZeros, pageLoad, prompt, remove, scriptName, setSearchParam, sleep, tabURL, toString };
156
+ export { $, $$, Flag, Fn, Nullable, Prettify, addStyle, alert, asserted, capitalize, confirm, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, logId, noop, padZeros, pageLoad, prompt, remove, removeSearchParams, saveFile, scriptName, setSearchParam, sleep, tabURL, toString };
package/browser/index.js CHANGED
@@ -128,11 +128,11 @@ if (typeof GM !== 'undefined') {
128
128
  infoObject = GM.info;
129
129
  // eslint-disable-next-line unicorn/no-negated-condition
130
130
  }
131
- else if (typeof GM_info !== 'undefined') {
132
- infoObject = GM_info;
131
+ else if (typeof GM_info === 'undefined') {
132
+ infoObject = { script: { name: document.title } };
133
133
  }
134
134
  else {
135
- infoObject = { script: { name: document.title } };
135
+ infoObject = GM_info;
136
136
  }
137
137
  const scriptName = infoObject.script.name;
138
138
  /** The identifier of the script to be used in logging. */
@@ -284,6 +284,26 @@ function setSearchParam(key, value, fullURL) {
284
284
  }
285
285
  return updatedURL.join('');
286
286
  }
287
+ /**
288
+ * @param keys The names of the param in the search query.
289
+ * @param fullURL If provided, it will be our target, otherwise it is the current location.
290
+ */
291
+ function removeSearchParams(keys, fullURL) {
292
+ const updatedURL = [];
293
+ const { origin, pathname, search, hash } = new URL(fullURL ?? window.location.href);
294
+ const searchParams = new URLSearchParams(search);
295
+ for (const key of keys) {
296
+ searchParams.delete(key);
297
+ }
298
+ updatedURL.push(origin, pathname);
299
+ if (searchParams.toString() !== '') {
300
+ updatedURL.push('?', searchParams.toString());
301
+ }
302
+ if (hash !== '') {
303
+ updatedURL.push(hash);
304
+ }
305
+ return updatedURL.join('');
306
+ }
287
307
 
288
308
  function addStyle(css, parent = document.documentElement) {
289
309
  const style = document.createElement('style');
@@ -339,6 +359,15 @@ async function pageLoad(completely) {
339
359
  });
340
360
  }
341
361
 
362
+ function saveFile(blob, fileName) {
363
+ const blobURL = URL.createObjectURL(blob);
364
+ const link = document.createElement('a');
365
+ link.href = blobURL;
366
+ link.download = fileName;
367
+ link.dispatchEvent(new MouseEvent('click'));
368
+ window.setTimeout(() => window.URL.revokeObjectURL(blobURL), 1000);
369
+ }
370
+
342
371
  function createFlagMessage(flag) {
343
372
  const { message: messageHeader, values, type } = flag;
344
373
  let message = messageHeader;
@@ -414,4 +443,4 @@ async function getOptions(flags) {
414
443
  return options;
415
444
  }
416
445
 
417
- export { $, $$, addStyle, alert, asserted, capitalize, confirm, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, logId, noop, padZeros, pageLoad, prompt, remove, scriptName, setSearchParam, sleep, tabURL, toString };
446
+ export { $, $$, addStyle, alert, asserted, capitalize, confirm, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, logId, noop, padZeros, pageLoad, prompt, remove, removeSearchParams, saveFile, scriptName, setSearchParam, sleep, tabURL, toString };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jeniex/utils",
3
- "version": "1.0.21",
3
+ "version": "1.0.23",
4
4
  "author": "JenieX",
5
5
  "homepage": "https://github.com/JenieX/utils",
6
6
  "type": "module",