loader working + enhancement:// protocol registered

This commit is contained in:
dragonwocky 2020-07-17 18:42:55 +10:00
parent 75cd042bd5
commit 4faa7bb919
Signed by: dragonwocky
GPG Key ID: C7A48B7846AA706D
5 changed files with 67 additions and 17 deletions

View File

@ -21,6 +21,10 @@ each directory in the `mods` folder is considered a module, with the entry point
| `mod.js` | **required:** describes the module and contains functional javascript | | `mod.js` | **required:** describes the module and contains functional javascript |
| `styles.css` | **optional:** css file automatically inserted into each app window via the `enhancement://` protocol | | `styles.css` | **optional:** css file automatically inserted into each app window via the `enhancement://` protocol |
> a module that with the primary function of being a hack should be categorised as an extension,
> while a module that with the primary function of adding styles should be categorised as a theme
> in the `mod.js` `type` setting.
### mod.js ### mod.js
```js ```js
@ -112,8 +116,15 @@ module.exports.hacks = {
#### the `enhancement://` protocol #### the `enhancement://` protocol
to be documented any files within the `mods` folder can be used via the `enhancement://` protocol.
## styling for example, accessing an image file within the frameless mod: `<img src="enhancement://frameless/minimise.svg">`.
to be documented ## styles.css
styles can be used for custom element insertions, generally hiding/re-spacing elements,
and particularly: colour theming.
the enhancer has been designed with theming in mind, so as much of notion's colours
and typography as possible (both for the light and dark themes) have been mapped out
using css variables - check these out in the `mods/core/theme.css` and `mods/dark+/styles.css` files.

View File

@ -89,7 +89,9 @@ module.exports = async function ({ overwrite_version } = {}) {
if (bin_script.includes('app.asar')) { if (bin_script.includes('app.asar')) {
await fs.outputFile( await fs.outputFile(
bin_path, bin_path,
bin_script.replace('electron app.asar\n', 'electron app\n') bin_script
.replace('electron app.asar\n', 'electron app\n')
.replace('electron6 app.asar\n', 'electron6 app\n')
); );
} }
} }

View File

@ -17,10 +17,12 @@ module.exports = async function (__file) {
.slice(path.resolve(__notion, 'app').length + 1) .slice(path.resolve(__notion, 'app').length + 1)
.replace(/\\/g, '/'); .replace(/\\/g, '/');
const mods = await fs.readdir(path.resolve(__dirname, '..', 'mods')), const modules = {
invalid_mods = [], source: await fs.readdir(path.resolve(__dirname, '..', 'mods')),
loaded_mods = []; invalid: [],
for (let dir of mods) { loaded: [],
};
for (let dir of modules.source) {
try { try {
const mod = require(`../mods/${dir}/mod.js`); const mod = require(`../mods/${dir}/mod.js`);
if ( if (
@ -31,22 +33,53 @@ module.exports = async function (__file) {
!['extension', 'theme', 'core'].includes(mod.type) !['extension', 'theme', 'core'].includes(mod.type)
) )
throw Error; throw Error;
if (mod.hacks && mod.hacks[__file]) if (mod.type === 'core' || store('mods', { [mod.id]: false })[mod.id]) {
mod.hacks[__file]((defaults) => store(mod.id, defaults)); if (mod.hacks && mod.hacks[__file])
loaded_mods.push(mod.name); mod.hacks[__file]((defaults) => store(mod.id, defaults));
if (
__file === 'renderer/preload.js' &&
(await fs.pathExists(
path.resolve(__dirname, '..', 'mods', dir, 'styles.css')
))
) {
document.addEventListener('readystatechange', (event) => {
if (document.readyState !== 'complete') return false;
const style = document.createElement('link');
style.rel = 'stylesheet';
style.href = `enhancement://${dir}/styles.css`;
document.querySelector('head').appendChild(style);
});
}
}
modules.loaded.push(mod.name);
} catch (err) { } catch (err) {
invalid_mods.push(dir); modules.invalid.push(dir);
} }
} }
if (__file === 'main/main.js') {
require('electron')
.session.fromPartition('persist:notion')
.protocol.registerFileProtocol('enhancement', (req, callback) => {
callback({
path: path.resolve(
__dirname,
'..',
'mods',
req.url.slice('enhancement://'.length)
),
});
});
}
if (__file === 'renderer/preload.js') { if (__file === 'renderer/preload.js') {
if (loaded_mods.length) if (modules.loaded.length)
console.info( console.info(
`<notion-enhancer> enhancements loaded: ${loaded_mods.join(', ')}.` `<notion-enhancer> enhancements loaded: ${modules.loaded.join(', ')}.`
); );
if (invalid_mods.length) if (modules.invalid.length)
console.error( console.error(
`<notion-enhancer> invalid mods found: ${invalid_mods.join(', ')}.` `<notion-enhancer> invalid mods found: ${modules.invalid.join(', ')}.`
); );
} }
}; };

View File

@ -107,7 +107,9 @@ module.exports = async function ({ overwrite_asar, delete_data } = {}) {
if (!bin_script.includes('app.asar')) { if (!bin_script.includes('app.asar')) {
await fs.outputFile( await fs.outputFile(
bin_path, bin_path,
bin_script.replace('electron app\n', 'electron app.asar\n') bin_script
.replace('electron app\n', 'electron app.asar\n')
.replace('electron6 app\n', 'electron6 app.asar\n')
); );
} }
} }

View File

@ -15,6 +15,8 @@ module.exports = (namespace, defaults = {}) => {
fs.ensureDirSync(data_folder); fs.ensureDirSync(data_folder);
const getData = () => ({ ...defaults, ...getJSON(namespace) }); const getData = () => ({ ...defaults, ...getJSON(namespace) });
fs.writeJsonSync(namespace, getData());
return new Proxy(defaults, { return new Proxy(defaults, {
get(obj, prop) { get(obj, prop) {
obj = getData(); obj = getData();