From 4faa7bb919c94ad7ed78430785caf1fe60c79d0f Mon Sep 17 00:00:00 2001 From: dragonwocky Date: Fri, 17 Jul 2020 18:42:55 +1000 Subject: [PATCH] loader working + enhancement:// protocol registered --- DOCUMENTATION.md | 17 ++++++++++++--- pkg/apply.js | 4 +++- pkg/loader.js | 57 ++++++++++++++++++++++++++++++++++++++---------- pkg/remove.js | 4 +++- pkg/store.js | 2 ++ 5 files changed, 67 insertions(+), 17 deletions(-) diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 2a9a742..2feb91c 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -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 | | `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 ```js @@ -112,8 +116,15 @@ module.exports.hacks = { #### 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: ``. -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. diff --git a/pkg/apply.js b/pkg/apply.js index fa8bb8a..4cd5caf 100644 --- a/pkg/apply.js +++ b/pkg/apply.js @@ -89,7 +89,9 @@ module.exports = async function ({ overwrite_version } = {}) { if (bin_script.includes('app.asar')) { await fs.outputFile( 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') ); } } diff --git a/pkg/loader.js b/pkg/loader.js index be23efc..81b373f 100644 --- a/pkg/loader.js +++ b/pkg/loader.js @@ -17,10 +17,12 @@ module.exports = async function (__file) { .slice(path.resolve(__notion, 'app').length + 1) .replace(/\\/g, '/'); - const mods = await fs.readdir(path.resolve(__dirname, '..', 'mods')), - invalid_mods = [], - loaded_mods = []; - for (let dir of mods) { + const modules = { + source: await fs.readdir(path.resolve(__dirname, '..', 'mods')), + invalid: [], + loaded: [], + }; + for (let dir of modules.source) { try { const mod = require(`../mods/${dir}/mod.js`); if ( @@ -31,22 +33,53 @@ module.exports = async function (__file) { !['extension', 'theme', 'core'].includes(mod.type) ) throw Error; - if (mod.hacks && mod.hacks[__file]) - mod.hacks[__file]((defaults) => store(mod.id, defaults)); - loaded_mods.push(mod.name); + if (mod.type === 'core' || store('mods', { [mod.id]: false })[mod.id]) { + if (mod.hacks && mod.hacks[__file]) + 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) { - 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 (loaded_mods.length) + if (modules.loaded.length) console.info( - ` enhancements loaded: ${loaded_mods.join(', ')}.` + ` enhancements loaded: ${modules.loaded.join(', ')}.` ); - if (invalid_mods.length) + if (modules.invalid.length) console.error( - ` invalid mods found: ${invalid_mods.join(', ')}.` + ` invalid mods found: ${modules.invalid.join(', ')}.` ); } }; diff --git a/pkg/remove.js b/pkg/remove.js index a437caf..2526e9e 100644 --- a/pkg/remove.js +++ b/pkg/remove.js @@ -107,7 +107,9 @@ module.exports = async function ({ overwrite_asar, delete_data } = {}) { if (!bin_script.includes('app.asar')) { await fs.outputFile( 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') ); } } diff --git a/pkg/store.js b/pkg/store.js index 898a96b..6b54397 100644 --- a/pkg/store.js +++ b/pkg/store.js @@ -15,6 +15,8 @@ module.exports = (namespace, defaults = {}) => { fs.ensureDirSync(data_folder); const getData = () => ({ ...defaults, ...getJSON(namespace) }); + fs.writeJsonSync(namespace, getData()); + return new Proxy(defaults, { get(obj, prop) { obj = getData();