-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathinjectMods.js
More file actions
161 lines (137 loc) · 5.07 KB
/
Copy pathinjectMods.js
File metadata and controls
161 lines (137 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// ==UserScript==
// @name Awesome Vivaldi Mod Loader
// @description Dynamically loads CSS and JS mods from user_mods/ directory.
// @version 2026.7.7
// @author Ryan (Acid)
// ==/UserScript==
// injectMods.js
// Injected into Vivaldi's window.html by the Awesome Vivaldi installer.
// Dynamically discovers and loads CSS/JS mods from user_mods/ at runtime.
//
// CSS: Loads Import.css, which uses @import to chain all other CSS files.
// JS: Lists user_mods/js/ via chrome.runtime.getPackageDirectoryEntry,
// loads ModConfig.js first, then the rest alphabetically.
// async=false preserves execution order.
(function () {
"use strict";
const IMPORT_CSS = "Import.css";
const CSS_DIR = "user_mods/css/";
const JS_DIR = "user_mods/js/";
const PRIORITY_JS = ["ModConfig.js"];
const LOG_PREFIX = "[AwesomeVivaldi]";
function log(msg) {
console.log(LOG_PREFIX, msg);
}
function warn(msg) {
console.warn(LOG_PREFIX, msg);
}
// ── CSS ──────────────────────────────────────────────────────────
function loadCSS() {
var link = document.createElement("link");
link.rel = "stylesheet";
link.href = CSS_DIR + IMPORT_CSS;
link.onload = function () {
log("CSS mods loaded via Import.css");
};
link.onerror = function () {
warn("Failed to load Import.css — CSS mods may not be active");
};
document.head.appendChild(link);
}
// ── JS discovery ─────────────────────────────────────────────────
function listJSFiles() {
return new Promise(function (resolve) {
if (!chrome.runtime || !chrome.runtime.getPackageDirectoryEntry) {
warn("chrome.runtime.getPackageDirectoryEntry unavailable — JS mods skipped");
resolve([]);
return;
}
chrome.runtime.getPackageDirectoryEntry(function (rootDir) {
rootDir.getDirectory(
JS_DIR,
{ create: false },
function (jsDir) {
var reader = jsDir.createReader();
var allEntries = [];
function readBatch() {
reader.readEntries(function (batch) {
if (batch.length === 0) {
var files = allEntries
.filter(function (e) {
return e.isFile && /\.js$/.test(e.name);
})
.map(function (e) {
return e.name;
})
.sort();
resolve(files);
} else {
allEntries = allEntries.concat(Array.prototype.slice.call(batch));
readBatch();
}
});
}
readBatch();
},
function () {
// Directory doesn't exist — not an error, just no JS mods installed
log("JS mods directory not found, skipping JS mods");
resolve([]);
}
);
});
});
}
// ── JS loading ───────────────────────────────────────────────────
function sortJSFiles(files) {
var priority = [];
var rest = [];
for (var i = 0; i < files.length; i++) {
if (PRIORITY_JS.indexOf(files[i]) !== -1) {
priority.push(files[i]);
} else {
rest.push(files[i]);
}
}
// Keep PRIORITY_JS order, then alphabetical for the rest
var ordered = PRIORITY_JS.filter(function (p) {
return priority.indexOf(p) !== -1;
});
return ordered.concat(rest);
}
function loadAllJS(files) {
var sorted = sortJSFiles(files);
log("Loading " + sorted.length + " JS mods: " + sorted.join(", "));
for (var i = 0; i < sorted.length; i++) {
var script = document.createElement("script");
script.src = JS_DIR + sorted[i];
script.async = false; // Preserve execution order (critical: ModConfig before AI mods)
script.onerror = function (filename) {
return function () {
warn("Failed to load JS mod: " + filename);
};
}(sorted[i]);
document.body.appendChild(script);
}
}
// ── Init ─────────────────────────────────────────────────────────
function init() {
log("Awesome Vivaldi mod loader initializing…");
// CSS is non-blocking — browser loads Import.css in parallel
loadCSS();
// JS loads sequentially via async=false to respect dependency order
listJSFiles().then(function (files) {
if (files.length > 0) {
loadAllJS(files);
} else {
log("No JS mods found");
}
});
}
// Run as soon as possible (script is in <body>, DOM is available)
if (document.body) {
init();
} else {
document.addEventListener("DOMContentLoaded", init);
}
})();