33 Written by: Someone on StackOverflow - https://stackoverflow.com/a/950146
44*/
55
6- function loadPackage ( file ) {
7- var script = document . createElement ( "script" ) ;
8- script . src = `pkgs/${ file } ` ;
9- document . head . appendChild ( script ) ;
6+ function loadPackage ( packagePath ) {
7+ const splitPath = packagePath . split ( ':' , 2 ) ;
8+ const scriptPath = `pkgs/${ splitPath [ 0 ] } / ${ splitPath [ 1 ] } ` ;
9+ // console.log(`result: ${splitPath}`)
1010
11- console . log ( `[Package Injector] Imported '${ file } '` )
12- } ;
11+ var script = document . createElement ( "script" ) ;
12+ script . src = scriptPath ;
13+ document . head . appendChild ( script ) ;
14+
15+ console . log ( `[Package Injector] Imported '${ scriptPath } '` ) ;
16+ }
17+
18+ function loadLibrary ( libraryPath ) {
19+ const splitPath = libraryPath . split ( ':' , 2 ) ;
20+ const scriptPath = `libs/${ splitPath [ 0 ] } /${ splitPath [ 1 ] } ` ;
21+ // console.log(`result: ${libraryPath}`)
1322
14- function loadLibrary ( file ) {
1523 var script = document . createElement ( "script" ) ;
16- script . src = `libs/ ${ file } ` ;
24+ script . src = scriptPath ;
1725 document . head . appendChild ( script ) ;
1826
19- console . log ( `[Library Injector] Imported '${ file } '` )
20- } ;
27+ console . log ( `[Library Injector] Imported '${ libraryPath } '` )
28+ }
29+
30+ function loadPromisedPackage ( packagePath ) {
31+ return new Promise ( ( resolve , reject ) => {
32+ const splitPath = packagePath . split ( ':' , 2 ) ;
33+ const scriptPath = `pkgs/${ splitPath [ 0 ] } /${ splitPath [ 1 ] } ` ;
34+
35+ var script = document . createElement ( "script" ) ;
36+ script . src = `scriptPath` ;
37+
38+ script . onload = ( ) => {
39+ console . log ( `[Package Injector] Imported '${ scriptPath } '` ) ;
40+ resolve ( ) ;
41+ } ;
42+
43+ script . onerror = ( ) => {
44+ console . error ( `[Package Injector] Failed to import '${ scriptPath } '` ) ;
45+ reject ( new Error ( `[Package Injector] Failed to load package: '${ scriptPath } '` ) ) ;
46+ } ;
47+
48+ document . head . appendChild ( script ) ;
49+ } ) ;
50+ }
51+
52+ function loadPromisedLibrary ( file ) {
53+ return new Promise ( ( resolve , reject ) => {
54+ var script = document . createElement ( "script" ) ;
55+ script . src = `libs/${ file } ` ;
56+
57+ script . onload = ( ) => {
58+ console . log ( `[Library Injector] Imported '${ file } '` ) ;
59+ resolve ( ) ;
60+ } ;
61+
62+ script . onerror = ( ) => {
63+ console . error ( `[Library Injector] Failed to import '${ file } '` ) ;
64+ reject ( new Error ( `[Library Injector] Failed to load library: ${ file } ` ) ) ;
65+ } ;
66+
67+ document . head . appendChild ( script ) ;
68+ } ) ;
69+ }
70+
71+
2172
2273function loadJS ( file ) {
2374 var script = document . createElement ( "script" ) ;
@@ -27,34 +78,66 @@ function loadJS(file) {
2778 console . log ( `[Javascript Injector] Imported '${ file } '` )
2879} ;
2980
30- function unloadPackage ( file ) {
31- const script = document . querySelector ( `script[src="pkgs/${ file } "]` ) ;
81+ function unloadPackage ( packagePath ) {
82+ const splitPath = packagePath . split ( ':' , 2 ) ;
83+ const scriptPath = `pkgs/${ splitPath [ 0 ] } /${ splitPath [ 1 ] } ` ;
84+
85+ const script = document . querySelector ( `script[src="${ scriptPath } "]` ) ;
3286 if ( script ) {
3387 script . remove ( ) ;
34- console . log ( `[Package Injector] Unloaded '${ file } '.` ) ;
88+ console . log ( `[Package Injector] Unloaded '${ scriptPath } '.` ) ;
3589 } else {
36- console . error ( `[Package Injector] Can't locate '${ file } ' for unloading.` ) ;
90+ console . error ( `[Package Injector] Can't locate '${ scriptPath } ' for unloading.` ) ;
3791 }
3892}
3993
4094function loadCSS ( file ) {
4195 var link = document . createElement ( 'link' ) ;
4296 link . rel = 'stylesheet' ;
4397 link . type = 'text/css' ;
44- link . href = file ;
98+ link . href = `stylesheets/ ${ file } ` ;
4599
46100 document . head . appendChild ( link ) ;
47101 console . log ( `[CSS Injector] Imported '${ file } '` )
48102}
49103
104+ function loadRawPathCSS ( file ) {
105+ var link = document . createElement ( 'link' ) ;
106+ link . rel = 'stylesheet' ;
107+ link . type = 'text/css' ;
108+ link . href = file ;
109+
110+ document . head . appendChild ( link ) ;
111+ console . log ( `[CSS Injector] Imported '${ file } ' from Raw Path` )
112+ }
113+
50114// Generate by ChatGTP
51- // However I still can't figure it out
52115function unloadCSS ( file ) {
53- const link = document . querySelector ( `link[href="${ file } "]` ) ;
116+ const link = document . querySelector ( `link[href="stylesheets/ ${ file } "]` ) ;
54117 if ( link ) {
55118 link . remove ( ) ;
56119 console . log ( `[CSS Injector] Removed CSS file: ${ file } ` ) ;
57120 } else {
58121 console . error ( `[CSS Injector] CSS file not found: ${ file } ` ) ;
59122 }
123+ }
124+
125+ function loadTheme ( file ) {
126+ var link = document . createElement ( 'link' ) ;
127+ link . rel = 'stylesheet' ;
128+ link . type = 'text/css' ;
129+ link . href = `themes/${ file } ` ;
130+
131+ document . head . appendChild ( link ) ;
132+ console . log ( `[Theme Manager] Imported '${ file } '` )
133+ }
134+
135+ function unloadTheme ( file ) {
136+ const link = document . querySelector ( `link[href="themes/${ file } "]` ) ;
137+ if ( link ) {
138+ link . remove ( ) ;
139+ console . log ( `[Theme Manager] Removed Theme: ${ file } ` ) ;
140+ } else {
141+ console . error ( `[Theme Manager] Theme not found: ${ file } ` ) ;
142+ }
60143}
0 commit comments