1
- // TODO: Add polish (pl)
2
-
3
1
// #region Console
4
2
const dbg = ( ...msg ) => {
5
3
const dt = new Date ( ) ;
@@ -61,7 +59,8 @@ function safeSelf() {
61
59
createElementNS : g . document . createElementNS . bind ( g . document ) ,
62
60
createTextNode : g . document . createTextNode . bind ( g . document ) ,
63
61
setTimeout : g . setTimeout ,
64
- clearTimeout : g . clearTimeout
62
+ clearTimeout : g . clearTimeout ,
63
+ trustedTypes : g . trustedTypes
65
64
} ;
66
65
userjs . safeSelf = safe ;
67
66
return safe ;
@@ -581,9 +580,12 @@ const iconSVG = {
581
580
}
582
581
svgElem . setAttributeNS ( null , k , v ) ;
583
582
}
584
- if ( typeof iconSVG [ type ] . html === 'string' ) {
585
- svgElem . innerHTML = iconSVG [ type ] . html ;
586
- }
583
+ try {
584
+ if ( typeof iconSVG [ type ] . html === 'string' ) {
585
+ svgElem . innerHTML = iconSVG [ type ] . html ;
586
+ }
587
+ // eslint-disable-next-line no-unused-vars
588
+ } catch ( ex ) { /* empty */ }
587
589
if ( container ) {
588
590
container . appendChild ( svgElem ) ;
589
591
return svgElem ;
@@ -726,7 +728,7 @@ const Network = {
726
728
if ( Number . isNaN ( bytes ) ) return '0 Bytes' ;
727
729
const k = 1024 ;
728
730
const dm = decimals < 0 ? 0 : decimals ;
729
- const i = Math . floor ( Math . pow ( bytes ) / Math . log ( k ) ) ;
731
+ const i = Math . floor ( Math . log ( bytes ) / Math . log ( k ) ) ;
730
732
return `${ parseFloat ( ( bytes / Math . pow ( k , i ) ) . toFixed ( dm ) ) } ${ Network . sizes [ i ] } ` ;
731
733
} ,
732
734
sizes : [ 'Bytes' , 'KB' , 'MB' , 'GB' , 'TB' , 'PB' , 'EB' , 'ZB' , 'YB' ] ,
@@ -843,6 +845,7 @@ const sleazyRedirect = () => {
843
845
/ \/ \/ ( [ ^ . ] + \. ) ? ( g r e a s y f o r k | s l e a z y f o r k ) \. o r g / ,
844
846
'//$1' + otherSite + '.org'
845
847
) ;
848
+ info ( `Redirecting to "${ str } "` ) ;
846
849
if ( isFN ( winLocation . assign ) ) {
847
850
winLocation . assign ( str ) ;
848
851
} else {
@@ -958,7 +961,7 @@ class initContainer {
958
961
*/
959
962
async inject ( callback , doc ) {
960
963
if ( this . checkBlacklist ( this . host ) ) {
961
- err ( ' Blacklisted website' ) ;
964
+ err ( ` Blacklisted " ${ this . host } "` ) ;
962
965
this . remove ( ) ;
963
966
return ;
964
967
}
@@ -1055,6 +1058,9 @@ class initContainer {
1055
1058
checkBlacklist ( str ) {
1056
1059
str = str || this . host ;
1057
1060
let blacklisted = false ;
1061
+ if ( / a c c o u n t s * \. g o o g l e \. / . test ( this . webpage . host ) ) {
1062
+ blacklisted = true ;
1063
+ }
1058
1064
for ( const b of normalizeTarget ( cfg . blacklist ) ) {
1059
1065
if ( typeof b === 'string' ) {
1060
1066
if ( b . startsWith ( 'userjs-' ) ) {
@@ -1575,9 +1581,6 @@ function primaryFN() {
1575
1581
elem . remove ( ) ;
1576
1582
}
1577
1583
}
1578
- if ( container . oldBlacklist ) {
1579
- delete container . oldBlacklist ;
1580
- }
1581
1584
container . unsaved = true ;
1582
1585
container . rebuild = true ;
1583
1586
rebuildCfg ( ) ;
@@ -1785,7 +1788,7 @@ function primaryFN() {
1785
1788
1786
1789
checkBlacklist ( str ) {
1787
1790
if ( container . checkBlacklist ( str ) ) {
1788
- this . showError ( ' Blacklisted' ) ;
1791
+ this . showError ( ` Blacklisted " ${ str } "` ) ;
1789
1792
timeoutFrame ( ) ;
1790
1793
return true ;
1791
1794
}
@@ -2031,6 +2034,7 @@ function primaryFN() {
2031
2034
}
2032
2035
Object . assign ( obj , {
2033
2036
code_data : code ,
2037
+ code_meta : { } ,
2034
2038
code_size : [ Network . format ( code . length ) ] ,
2035
2039
code_match : [ ] ,
2036
2040
code_grant : [ ] ,
@@ -2040,6 +2044,24 @@ function primaryFN() {
2040
2044
const afSet = new Set ( ) ;
2041
2045
const meta = parse_meta ( code ) ;
2042
2046
const applies_to_names = calculate_applies_to_names ( code ) ;
2047
+
2048
+ if ( translate ) {
2049
+ for ( const lng of language . cache ) {
2050
+ if ( meta [ `name:${ lng } ` ] ) {
2051
+ Object . assign ( obj , {
2052
+ name : meta [ `name:${ lng } ` ] ,
2053
+ translated : true
2054
+ } ) ;
2055
+ }
2056
+ if ( meta [ `description:${ lng } ` ] ) {
2057
+ Object . assign ( obj , {
2058
+ description : meta [ `description:${ lng } ` ] ,
2059
+ translated : true
2060
+ } ) ;
2061
+ }
2062
+ }
2063
+ }
2064
+
2043
2065
for ( const [ key , value ] of Object . entries ( meta ) ) {
2044
2066
if ( / g r a n t / . test ( key ) ) {
2045
2067
for ( const v of normalizeTarget ( value , false ) ) {
@@ -2058,32 +2080,11 @@ function primaryFN() {
2058
2080
}
2059
2081
}
2060
2082
Object . assign ( obj , {
2083
+ code_meta : meta ,
2061
2084
code_match : applies_to_names ,
2062
2085
code_grant : [ ...grantSet ] ,
2063
2086
antifeatures : [ ...afSet ]
2064
2087
} ) ;
2065
- if ( translate ) {
2066
- const headers = code . match ( / \/ \/ \s * @ [ \w ] [ \s \S ] + / g) ;
2067
- if ( isNull ( headers ) ) {
2068
- return code ;
2069
- }
2070
- for ( const lng of language . cache ) {
2071
- const findName = new RegExp ( `//\\s*@name:${ lng } \\s*(.*)` , 'gi' ) . exec ( headers [ 0 ] ) ;
2072
- const findDesc = new RegExp ( `//\\s*@description:${ lng } \\s*(.*)` , 'gi' ) . exec ( headers [ 0 ] ) ;
2073
- if ( ! isNull ( findName ) ) {
2074
- Object . assign ( obj , {
2075
- name : findName [ 1 ] ,
2076
- translated : true
2077
- } ) ;
2078
- }
2079
- if ( ! isNull ( findDesc ) ) {
2080
- Object . assign ( obj , {
2081
- description : findDesc [ 1 ] ,
2082
- translated : true
2083
- } ) ;
2084
- }
2085
- }
2086
- }
2087
2088
return code ;
2088
2089
} ;
2089
2090
const template = {
@@ -2291,6 +2292,12 @@ function primaryFN() {
2291
2292
} ) ;
2292
2293
if ( engine ) {
2293
2294
tr . dataset . engine = engine ;
2295
+ // if (engine.includes('fork')) {
2296
+ // fdaily.dataset.command = 'open-tab';
2297
+ // fdaily.dataset.webpage = `${ujs.url}/stats`;
2298
+ // fupdated.dataset.command = 'open-tab';
2299
+ // fupdated.dataset.webpage = `${ujs.url}/versions`;
2300
+ // }
2294
2301
if ( ! engine . includes ( 'fork' ) && cfg . recommend . others && goodUserJS . includes ( ujs . url ) ) {
2295
2302
tr . dataset . good = 'upsell' ;
2296
2303
}
@@ -2316,7 +2323,7 @@ function primaryFN() {
2316
2323
eframe . append ( scriptInstall ) ;
2317
2324
ratings . append ( fratings , fgood , fok , fbad ) ;
2318
2325
jsInfo . append ( ftotal , ratings , fver , fcreated ) ;
2319
- mkList ( 'Size' , {
2326
+ mkList ( i18n$ ( 'code_size' ) , {
2320
2327
list : ujs . code_size ,
2321
2328
type : 'size' ,
2322
2329
root : jsInfo
@@ -2358,10 +2365,6 @@ function primaryFN() {
2358
2365
if ( isEmpty ( host ) ) {
2359
2366
host = container . host ;
2360
2367
}
2361
- if ( container . oldBlacklist ) {
2362
- MUJS . makeError ( { message : i18n$ ( 'legacy' ) , cause : 'Blacklist outdated' , notify : true } ) ;
2363
- return ;
2364
- }
2365
2368
if ( MUJS . checkBlacklist ( host ) ) {
2366
2369
return ;
2367
2370
}
@@ -2409,6 +2412,7 @@ function primaryFN() {
2409
2412
}
2410
2413
createjs ( ujs , engine ) ;
2411
2414
} ;
2415
+ const arr = [ ] ;
2412
2416
for ( const engine of engines ) {
2413
2417
const cEngine = cache [ `${ engine . name } ` ] ;
2414
2418
if ( ! isEmpty ( cEngine ) ) {
@@ -2418,16 +2422,24 @@ function primaryFN() {
2418
2422
MUJS . updateCounter ( cEngine . length , engine ) ;
2419
2423
continue ;
2420
2424
}
2425
+ /**
2426
+ * @param { import("../typings/UserJS.d.ts").GSFork } dataQ
2427
+ */
2421
2428
const forkFN = async ( dataQ ) => {
2422
2429
if ( ! dataQ ) {
2423
- MUJS . showError ( 'Invalid data received from the server, TODO fix this ' ) ;
2430
+ MUJS . showError ( 'Invalid data received from the server, check internet connection ' ) ;
2424
2431
return ;
2425
2432
}
2426
- const data = dataQ . query ;
2427
- if ( ! Array . isArray ( data ) ) {
2433
+
2434
+ const data = ( Array . isArray ( dataQ . query ) ? dataQ . query : [ ] ) . filter ( ( d ) => ! d . deleted ) ;
2435
+ if ( isBlank ( data ) ) {
2428
2436
return ;
2429
2437
}
2430
2438
const hideData = [ ] ;
2439
+ /**
2440
+ * @param {import("../typings/UserJS.d.ts").GSForkQuery } d
2441
+ * @returns {boolean }
2442
+ */
2431
2443
const inUserLanguage = ( d ) => {
2432
2444
const dlocal = d . locale . split ( '-' ) [ 0 ] ?? d . locale ;
2433
2445
if ( language . cache . includes ( dlocal ) ) {
@@ -2437,9 +2449,6 @@ function primaryFN() {
2437
2449
return false ;
2438
2450
} ;
2439
2451
const filterLang = data . filter ( ( d ) => {
2440
- if ( d . deleted ) {
2441
- return false ;
2442
- }
2443
2452
if ( cfg . filterlang && ! inUserLanguage ( d ) ) {
2444
2453
return false ;
2445
2454
}
@@ -2448,7 +2457,7 @@ function primaryFN() {
2448
2457
let finalList = filterLang ;
2449
2458
const hds = [ ] ;
2450
2459
for ( const ujs of hideData ) {
2451
- await reqCode ( ujs ) ;
2460
+ await reqCode ( ujs , true ) ;
2452
2461
if ( ujs . translated ) {
2453
2462
hds . push ( ujs ) ;
2454
2463
}
@@ -2543,16 +2552,17 @@ function primaryFN() {
2543
2552
MUJS . showError ( ex ) ;
2544
2553
}
2545
2554
} ;
2555
+ let netFN ;
2546
2556
if ( engine . name . includes ( 'fork' ) ) {
2547
- Network . req ( `${ engine . url } /scripts/by-site/${ host } .json` )
2557
+ netFN = Network . req ( `${ engine . url } /scripts/by-site/${ host } .json` )
2548
2558
. then ( forkFN )
2549
2559
. catch ( MUJS . showError ) ;
2550
2560
} else if ( / g i t h u b / gi. test ( engine . name ) ) {
2551
2561
if ( isEmpty ( engine . token ) ) {
2552
2562
MUJS . showError ( `"${ engine . name } " requires a token to use` ) ;
2553
2563
continue ;
2554
2564
}
2555
- Network . req (
2565
+ netFN = Network . req (
2556
2566
`${ engine . url } "// ==UserScript=="+${ host } + "// ==/UserScript=="+in:file+language:js&per_page=30` ,
2557
2567
'GET' ,
2558
2568
'json' ,
@@ -2585,15 +2595,22 @@ function primaryFN() {
2585
2595
} )
2586
2596
. catch ( MUJS . showError ) ;
2587
2597
} else {
2588
- Network . req ( `${ engine . url } ${ host } ` , 'GET' , 'document' )
2598
+ netFN = Network . req ( `${ engine . url } ${ host } ` , 'GET' , 'document' )
2589
2599
. then ( customFN )
2590
2600
. catch ( ( error ) => {
2591
2601
MUJS . showError ( `Engine: "${ engine . name } "` , error ) ;
2592
2602
} ) ;
2593
2603
}
2604
+ if ( netFN ) {
2605
+ arr . push ( netFN ) ;
2606
+ }
2594
2607
}
2595
2608
urlBar . placeholder = i18n$ ( 'search_placeholder' ) ;
2596
2609
urlBar . value = '' ;
2610
+ Promise . allSettled ( arr ) . then ( ( ) => {
2611
+ tabhead . rows [ 0 ] . cells [ 2 ] . dispatchEvent ( new MouseEvent ( 'click' ) ) ;
2612
+ tabhead . rows [ 0 ] . cells [ 2 ] . dispatchEvent ( new MouseEvent ( 'click' ) ) ;
2613
+ } ) ;
2597
2614
} catch ( ex ) {
2598
2615
MUJS . showError ( ex ) ;
2599
2616
}
@@ -2955,11 +2972,9 @@ function primaryFN() {
2955
2972
dom . cl . add ( th , 'mujs-pointer' ) ;
2956
2973
ael ( th , 'click' , ( ) => {
2957
2974
/** [Stack Overflow Reference](https://stackoverflow.com/questions/14267781/sorting-html-table-with-javascript/53880407#53880407) */
2958
- const table = th . closest ( 'table' ) ;
2959
- const tbody = table . querySelector ( 'tbody' ) ;
2960
- Array . from ( tbody . querySelectorAll ( 'tr' ) )
2975
+ Array . from ( tabbody . querySelectorAll ( 'tr' ) )
2961
2976
. sort ( comparer ( Array . from ( th . parentNode . children ) . indexOf ( th ) , ( this . asc = ! this . asc ) ) )
2962
- . forEach ( ( tr ) => tbody . appendChild ( tr ) ) ;
2977
+ . forEach ( ( tr ) => tabbody . appendChild ( tr ) ) ;
2963
2978
} ) ;
2964
2979
}
2965
2980
makecfg ( ) ;
@@ -2987,24 +3002,8 @@ const loadDOM = (onDomReady) => {
2987
3002
once : true
2988
3003
} ) ;
2989
3004
} ;
2990
- // /**
2991
- // * @type { import("../typings/UserJS.d.ts").setObj }
2992
- // */
2993
- // const setObj = (objA = {}, objB = {}) => {
2994
- // objA = objA || {};
2995
- // objB = objB || {};
2996
- // for (const [key, value] of Object.entries(objA)) {
2997
- // if (!hasOwn(objB, key)) {
2998
- // objB[key] = value;
2999
- // } else if (typeof value === 'object') {
3000
- // setObj(value, objB[key]);
3001
- // }
3002
- // }
3003
- // return objB;
3004
- // };
3005
3005
const init = async ( ) => {
3006
3006
const stored = await StorageSystem . getValue ( 'Config' , defcfg ) ;
3007
- // cfg = setObj(defcfg, stored);
3008
3007
cfg = {
3009
3008
...defcfg ,
3010
3009
...stored
@@ -3013,12 +3012,15 @@ const init = async () => {
3013
3012
loadDOM ( ( doc ) => {
3014
3013
try {
3015
3014
if ( window . location === null ) {
3016
- err ( '"window.location" is null, reload the webpage or use a different one' ) ;
3017
- return ;
3015
+ throw new Error ( '"window.location" is null, reload the webpage or use a different one' ) ;
3018
3016
}
3019
3017
if ( doc === null ) {
3020
- err ( '"doc" is null, reload the webpage or use a different one' ) ;
3021
- return ;
3018
+ throw new Error ( '"doc" is null, reload the webpage or use a different one' ) ;
3019
+ }
3020
+ if ( window . trustedTypes && window . trustedTypes . createPolicy ) {
3021
+ window . trustedTypes . createPolicy ( 'default' , {
3022
+ createHTML : ( string ) => string
3023
+ } ) ;
3022
3024
}
3023
3025
sleazyRedirect ( ) ;
3024
3026
container . inject ( primaryFN , doc ) ;
0 commit comments