Skip to content

Commit

Permalink
Startup and Preload Changes
Browse files Browse the repository at this point in the history
- Reworked startup so both betterdiscord.js and betterdiscord.asar
  can be loaded from the local ./dist/ extension folder. This makes
  it easier for users to quickly override the VFS asar file.
  NOTE: The load order is...
  1. Local betterdiscord.js
  2. Local betterdiscord.asar
  3. VFS betterdiscord.asar
- Renamed constant BD_CONFIG_PLUGINS to fit in with the rest
  of the FilePath constants.
- Added try/catch for backend IPC fetch handler.
- Added localizations for the extension options page (en/de).
- Added missing filesystem definitions to the bdPreload object.
  • Loading branch information
tsukasa committed Apr 15, 2023
1 parent f34235f commit 80d240a
Show file tree
Hide file tree
Showing 16 changed files with 280 additions and 98 deletions.
23 changes: 23 additions & 0 deletions _locales/de/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"extName": {
"message": "BDBrowser"
},
"extDesc": {
"message": "Erlaubt das Laden von BetterDiscord im Web Client. Manche Plugins funktionieren ggf. nicht."
},
"titlePersistentOptions": {
"message": "Dauerhafte Optionen"
},
"titleResetOnReloadOptions": {
"message": "Einmalige Optionen"
},
"disableBdRenderer": {
"message": "BetterDiscord Renderer nicht laden"
},
"disableBdPluginsOnReload": {
"message": "Beim Laden alle Plugins deaktivieren"
},
"deleteBdRendererOnReload": {
"message": "Beim Laden Asar aus VFS löschen"
}
}
23 changes: 23 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"extName": {
"message": "BDBrowser"
},
"extDesc": {
"message": "Allows you to use BetterDiscord on the web client. Some plugins might not be working correctly."
},
"titlePersistentOptions": {
"message": "Persistent Options"
},
"titleResetOnReloadOptions": {
"message": "Reset on Reload Options"
},
"disableBdRenderer": {
"message": "Do not load BetterDiscord Renderer"
},
"disableBdPluginsOnReload": {
"message": "Disable all Plugins on Reload"
},
"deleteBdRendererOnReload": {
"message": "Delete Asar from VFS on Reload"
}
}
14 changes: 7 additions & 7 deletions assets/chrome/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@
</head>
<body class="drac-bg-black">
<div class="drac-box drac-p-sm">
<h2 class="drac-heading drac-heading-ml drac-text-white">
Persistent Options
<h2 i18n="titlePersistentOptions" class="drac-heading drac-heading-ml drac-text-white">
#titlePersistentOptions
</h2>
<p>
<input type="checkbox" name="disableBdRenderer" id="disableBdRenderer" class="drac-switch drac-checkbox drac-switch-purple" checked=""/>
<label for="disableBdRenderer" class="drac-text drac-text-white">Do not load BetterDiscord Renderer</label>
<label i18n="disableBdRenderer" for="disableBdRenderer" class="drac-text drac-text-white">#lblDisableBdRenderer</label>
</p>
</div>
<div class="drac-box drac-p-sm">
<h2 class="drac-heading drac-heading-ml drac-text-white">
Reset on Reload Options
<h2 i18n="titleResetOnReloadOptions" id="titleResetOnReloadOptions" class="drac-heading drac-heading-ml drac-text-white">
#titleResetOnReloadOptions
</h2>
<p>
<input type="checkbox" name="disableBdPluginsOnReload" id="disableBdPluginsOnReload" class="drac-switch drac-checkbox drac-switch-purple" checked=""/>
<label for="disableBdPluginsOnReload" class="drac-text drac-text-white">Disable all Plugins on Reload</label>
<label i18n="disableBdPluginsOnReload" for="disableBdPluginsOnReload" class="drac-text drac-text-white">#lblDisableBdPluginsOnReload</label>
</p>
<p>
<input type="checkbox" name="deleteBdRendererOnReload" id="deleteBdRendererOnReload" class="drac-switch drac-checkbox drac-switch-purple" checked=""/>
<label for="deleteBdRendererOnReload" class="drac-text drac-text-white">Delete Asar from VFS on Reload</label>
<label i18n="deleteBdRendererOnReload" for="deleteBdRendererOnReload" class="drac-text drac-text-white">#lblDeleteBdRendererOnReload</label>
</p>
</div>
<script src="options.js"></script>
Expand Down
38 changes: 34 additions & 4 deletions assets/chrome/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,37 @@ const restoreOptions = () => {
);
};

document.addEventListener('DOMContentLoaded', restoreOptions);
document.getElementById('disableBdRenderer').addEventListener('change', saveOptions);
document.getElementById('disableBdPluginsOnReload').addEventListener('change', saveOptions);
document.getElementById('deleteBdRendererOnReload').addEventListener('change', saveOptions);
/**
* Loads the i18n messages for the options page.
* The i18n messages are loaded from the _locales folder.
*/
const loadI18n = () => {
let elements = document.querySelectorAll("[i18n]");

elements.forEach(element => {
const i18nElement = element.getAttribute("i18n");
const i18nMessage = chrome.i18n.getMessage(i18nElement);

if (i18nMessage) {
element.innerText = i18nMessage;
}
});
}

/**
* Operations to perform once options page has loaded.
*/
const onContentLoaded = () => {
loadI18n();
restoreOptions();
}

const initialize = () => {
document.addEventListener("DOMContentLoaded", onContentLoaded);

document.getElementById("disableBdRenderer").addEventListener("change", saveOptions);
document.getElementById("disableBdPluginsOnReload").addEventListener("change", saveOptions);
document.getElementById("deleteBdRendererOnReload").addEventListener("change", saveOptions);
}

initialize();
25 changes: 15 additions & 10 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,23 @@ function registerEvents() {
options: data.options
}
}, (response) => {
if (response.error) {
if(!data.url.startsWith(chrome.runtime.getURL("")))
console.error("BdBrowser Backend MAKE_REQUESTS failed:", data.url, response.error);
try {
if (response.error) {
if(!data.url.startsWith(chrome.runtime.getURL("")))
console.error("BdBrowser Backend MAKE_REQUESTS failed:", data.url, response.error);
ipcMain.reply(event, undefined);
}
else
{
// Response body comes in as a normal array, so requires
// another round of casting into Uint8Array for the buffer.
response.body = new Uint8Array(response.body).buffer;
ipcMain.reply(event, response);
}
} catch(error) {
Logger.error("Backend", "MAKE_REQUESTS failed:", error, data.url, response);
ipcMain.reply(event, undefined);
}
else
{
// Response body comes in as a normal array, so requires
// another round of casting into Uint8Array for the buffer.
response.body = new Uint8Array(response.body).buffer;
ipcMain.reply(event, response);
}
}
);
});
Expand Down
2 changes: 1 addition & 1 deletion common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const IPCEvents = {
export const FilePaths = {
BD_ASAR_PATH: "AppData/BetterDiscord/data/betterdiscord.asar",
BD_ASAR_VERSION_PATH: "AppData/BetterDiscord/data/bd-asar-version.txt",
BD_CONFIG_PLUGINS: "AppData/BetterDiscord/data/&1/plugins.json"
BD_CONFIG_PLUGINS_PATH: "AppData/BetterDiscord/data/&1/plugins.json"
}

export default {
Expand Down
21 changes: 13 additions & 8 deletions dist/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const IPCEvents = {
const FilePaths = {
BD_ASAR_PATH: "AppData/BetterDiscord/data/betterdiscord.asar",
BD_ASAR_VERSION_PATH: "AppData/BetterDiscord/data/bd-asar-version.txt",
BD_CONFIG_PLUGINS: "AppData/BetterDiscord/data/&1/plugins.json"
BD_CONFIG_PLUGINS_PATH: "AppData/BetterDiscord/data/&1/plugins.json"
};
/* harmony default export */ const constants = ({
IPCEvents,
Expand Down Expand Up @@ -289,14 +289,19 @@ function registerEvents() {
options: data.options
}
}, response => {
if (response.error) {
if (!data.url.startsWith(chrome.runtime.getURL(""))) console.error("BdBrowser Backend MAKE_REQUESTS failed:", data.url, response.error);
try {
if (response.error) {
if (!data.url.startsWith(chrome.runtime.getURL(""))) console.error("BdBrowser Backend MAKE_REQUESTS failed:", data.url, response.error);
ipcMain.reply(event, undefined);
} else {
// Response body comes in as a normal array, so requires
// another round of casting into Uint8Array for the buffer.
response.body = new Uint8Array(response.body).buffer;
ipcMain.reply(event, response);
}
} catch (error) {
Logger.error("Backend", "MAKE_REQUESTS failed:", error, data.url, response);
ipcMain.reply(event, undefined);
} else {
// Response body comes in as a normal array, so requires
// another round of casting into Uint8Array for the buffer.
response.body = new Uint8Array(response.body).buffer;
ipcMain.reply(event, response);
}
});
});
Expand Down
Loading

0 comments on commit 80d240a

Please sign in to comment.