aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview
diff options
context:
space:
mode:
authorLibravatar Vijay Raghavan Aravamudhan <vraravam@users.noreply.github.com>2021-08-08 00:01:37 +0000
committerLibravatar GitHub <noreply@github.com>2021-08-08 05:31:37 +0530
commit97d51a7763b14c92ee71ff9a012311dd9498d803 (patch)
treebd36005031ecb1148f27aa541e7a92a5e3aa4c0c /src/webview
parent5.6.1-nightly.17 [skip ci] (diff)
downloadferdium-app-97d51a7763b14c92ee71ff9a012311dd9498d803.tar.gz
ferdium-app-97d51a7763b14c92ee71ff9a012311dd9498d803.tar.zst
ferdium-app-97d51a7763b14c92ee71ff9a012311dd9498d803.zip
refactor: path-references refactoring and using 'import' instead of 'require' (#1752)
* refactor references to 'userData' and 'appData' directories to move hardcoding into single location * convert to es6 for lower memory usage as per https://codesource.io/the-difference-between-import-and-require-in-javascript/
Diffstat (limited to 'src/webview')
-rw-r--r--src/webview/darkmode.js10
-rw-r--r--src/webview/lib/RecipeWebview.js8
-rw-r--r--src/webview/recipe.js20
3 files changed, 18 insertions, 20 deletions
diff --git a/src/webview/darkmode.js b/src/webview/darkmode.js
index ab629435c..7435d6404 100644
--- a/src/webview/darkmode.js
+++ b/src/webview/darkmode.js
@@ -1,7 +1,7 @@
1/* eslint no-bitwise: ["error", { "int32Hint": true }] */ 1/* eslint no-bitwise: ["error", { "int32Hint": true }] */
2 2
3import path from 'path'; 3import { join } from 'path';
4import fs from 'fs-extra'; 4import { pathExistsSync, readFileSync } from 'fs-extra';
5 5
6const debug = require('debug')('Ferdi:DarkMode'); 6const debug = require('debug')('Ferdi:DarkMode');
7 7
@@ -10,9 +10,9 @@ const chars = [...'abcdefghijklmnopqrstuvwxyz'];
10const ID = [...Array(20)].map(() => chars[Math.random() * chars.length | 0]).join``; 10const ID = [...Array(20)].map(() => chars[Math.random() * chars.length | 0]).join``;
11 11
12export function injectDarkModeStyle(recipePath) { 12export function injectDarkModeStyle(recipePath) {
13 const darkModeStyle = path.join(recipePath, 'darkmode.css'); 13 const darkModeStyle = join(recipePath, 'darkmode.css');
14 if (fs.pathExistsSync(darkModeStyle)) { 14 if (pathExistsSync(darkModeStyle)) {
15 const data = fs.readFileSync(darkModeStyle); 15 const data = readFileSync(darkModeStyle);
16 const styles = document.createElement('style'); 16 const styles = document.createElement('style');
17 styles.id = ID; 17 styles.id = ID;
18 styles.innerHTML = data.toString(); 18 styles.innerHTML = data.toString();
diff --git a/src/webview/lib/RecipeWebview.js b/src/webview/lib/RecipeWebview.js
index 305e79882..96caa125e 100644
--- a/src/webview/lib/RecipeWebview.js
+++ b/src/webview/lib/RecipeWebview.js
@@ -1,5 +1,5 @@
1import { ipcRenderer } from 'electron'; 1import { ipcRenderer } from 'electron';
2import { exists, pathExistsSync, readFile } from 'fs-extra'; 2import { exists, pathExistsSync, readFileSync } from 'fs-extra';
3 3
4const debug = require('debug')('Ferdi:Plugin:RecipeWebview'); 4const debug = require('debug')('Ferdi:Plugin:RecipeWebview');
5 5
@@ -55,9 +55,8 @@ class RecipeWebview {
55 injectCSS(...files) { 55 injectCSS(...files) {
56 files.forEach(async (file) => { 56 files.forEach(async (file) => {
57 if (pathExistsSync(file)) { 57 if (pathExistsSync(file)) {
58 const data = await readFile(file, 'utf8');
59 const styles = document.createElement('style'); 58 const styles = document.createElement('style');
60 styles.innerHTML = data; 59 styles.innerHTML = readFileSync(file, 'utf8');
61 60
62 document.querySelector('head').appendChild(styles); 61 document.querySelector('head').appendChild(styles);
63 62
@@ -69,8 +68,7 @@ class RecipeWebview {
69 injectJSUnsafe(...files) { 68 injectJSUnsafe(...files) {
70 Promise.all(files.map(async (file) => { 69 Promise.all(files.map(async (file) => {
71 if (await exists(file)) { 70 if (await exists(file)) {
72 const data = await readFile(file, 'utf8'); 71 return readFileSync(file, 'utf8');
73 return data;
74 } 72 }
75 debug('Script not found', file); 73 debug('Script not found', file);
76 return null; 74 return null;
diff --git a/src/webview/recipe.js b/src/webview/recipe.js
index 598c3eb9a..a45c34002 100644
--- a/src/webview/recipe.js
+++ b/src/webview/recipe.js
@@ -1,8 +1,8 @@
1/* eslint-disable import/first */ 1/* eslint-disable import/first */
2import { contextBridge, ipcRenderer } from 'electron'; 2import { contextBridge, ipcRenderer } from 'electron';
3import path from 'path'; 3import { join } from 'path';
4import { autorun, computed, observable } from 'mobx'; 4import { autorun, computed, observable } from 'mobx';
5import fs from 'fs-extra'; 5import { pathExistsSync, readFileSync } from 'fs-extra';
6import { debounce } from 'lodash'; 6import { debounce } from 'lodash';
7 7
8// For some services darkreader tries to use the chrome extension message API 8// For some services darkreader tries to use the chrome extension message API
@@ -189,7 +189,7 @@ class RecipeController {
189 189
190 loadRecipeModule(event, config, recipe) { 190 loadRecipeModule(event, config, recipe) {
191 debug('loadRecipeModule'); 191 debug('loadRecipeModule');
192 const modulePath = path.join(recipe.path, 'webview.js'); 192 const modulePath = join(recipe.path, 'webview.js');
193 debug('module path', modulePath); 193 debug('module path', modulePath);
194 // Delete module from cache 194 // Delete module from cache
195 delete require.cache[require.resolve(modulePath)]; 195 delete require.cache[require.resolve(modulePath)];
@@ -214,15 +214,15 @@ class RecipeController {
214 const styles = document.createElement('style'); 214 const styles = document.createElement('style');
215 styles.innerHTML = screenShareCss; 215 styles.innerHTML = screenShareCss;
216 216
217 const userCss = path.join(recipe.path, 'user.css'); 217 const userCss = join(recipe.path, 'user.css');
218 if (fs.existsSync(userCss)) { 218 if (pathExistsSync(userCss)) {
219 const data = await fs.readFile(userCss); 219 const data = readFileSync(userCss);
220 styles.innerHTML += data.toString(); 220 styles.innerHTML += data.toString();
221 } 221 }
222 document.querySelector('head').appendChild(styles); 222 document.querySelector('head').appendChild(styles);
223 223
224 const userJs = path.join(recipe.path, 'user.js'); 224 const userJs = join(recipe.path, 'user.js');
225 if (fs.existsSync(userJs)) { 225 if (pathExistsSync(userJs)) {
226 const loadUserJs = () => { 226 const loadUserJs = () => {
227 // eslint-disable-next-line 227 // eslint-disable-next-line
228 const userJsModule = require(userJs); 228 const userJsModule = require(userJs);
@@ -308,11 +308,11 @@ class RecipeController {
308 debug('Enable dark mode'); 308 debug('Enable dark mode');
309 309
310 // Check if recipe has a darkmode.css 310 // Check if recipe has a darkmode.css
311 const darkModeStyle = path.join( 311 const darkModeStyle = join(
312 this.settings.service.recipe.path, 312 this.settings.service.recipe.path,
313 'darkmode.css', 313 'darkmode.css',
314 ); 314 );
315 const darkModeExists = fs.pathExistsSync(darkModeStyle); 315 const darkModeExists = pathExistsSync(darkModeStyle);
316 316
317 debug('darkmode.css exists? ', darkModeExists ? 'Yes' : 'No'); 317 debug('darkmode.css exists? ', darkModeExists ? 'Yes' : 'No');
318 318