diff options
author | vantezzen <hello@vantezzen.io> | 2020-04-17 18:07:20 +0200 |
---|---|---|
committer | vantezzen <hello@vantezzen.io> | 2020-04-17 18:07:20 +0200 |
commit | d3bea9800073f864cf407bf0c42ef7c299a91852 (patch) | |
tree | c0781876d677f51293333c18cbb637dafab203c2 /src/webview | |
parent | Merge branch 'develop' (diff) | |
parent | Prepare code (diff) | |
download | ferdium-app-d3bea9800073f864cf407bf0c42ef7c299a91852.tar.gz ferdium-app-d3bea9800073f864cf407bf0c42ef7c299a91852.tar.zst ferdium-app-d3bea9800073f864cf407bf0c42ef7c299a91852.zip |
Merge branch 'develop'v5.5.0-gm
Diffstat (limited to 'src/webview')
-rw-r--r-- | src/webview/lib/RecipeWebview.js | 4 | ||||
-rw-r--r-- | src/webview/lib/Userscript.js | 132 | ||||
-rw-r--r-- | src/webview/recipe.js | 15 |
3 files changed, 148 insertions, 3 deletions
diff --git a/src/webview/lib/RecipeWebview.js b/src/webview/lib/RecipeWebview.js index 4fac21c55..add5fffa6 100644 --- a/src/webview/lib/RecipeWebview.js +++ b/src/webview/lib/RecipeWebview.js | |||
@@ -14,6 +14,10 @@ class RecipeWebview { | |||
14 | this.loopFunc(); | 14 | this.loopFunc(); |
15 | 15 | ||
16 | debug('Poll event'); | 16 | debug('Poll event'); |
17 | |||
18 | // This event is for checking if the service recipe is still actively | ||
19 | // communicating with the client | ||
20 | ipcRenderer.sendToHost('alive'); | ||
17 | }); | 21 | }); |
18 | } | 22 | } |
19 | 23 | ||
diff --git a/src/webview/lib/Userscript.js b/src/webview/lib/Userscript.js new file mode 100644 index 000000000..2043d9fff --- /dev/null +++ b/src/webview/lib/Userscript.js | |||
@@ -0,0 +1,132 @@ | |||
1 | import { ipcRenderer } from 'electron'; | ||
2 | |||
3 | export default class Userscript { | ||
4 | // Current ./lib/RecipeWebview instance | ||
5 | recipe = null; | ||
6 | |||
7 | // Current ./recipe.js instance | ||
8 | controller = null; | ||
9 | |||
10 | // Service configuration | ||
11 | config = {}; | ||
12 | |||
13 | // Ferdi and service settings | ||
14 | settings = {}; | ||
15 | |||
16 | settingsUpdateHandler = null; | ||
17 | |||
18 | constructor(recipe, controller, config) { | ||
19 | this.recipe = recipe; | ||
20 | this.controller = controller; | ||
21 | this.internal_setSettings(controller.settings); | ||
22 | this.config = config; | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * Set internal copy of Ferdi's settings. | ||
27 | * This is only used internally and can not be used to change any settings | ||
28 | * | ||
29 | * @param {*} settings | ||
30 | */ | ||
31 | // eslint-disable-next-line | ||
32 | internal_setSettings(settings) { | ||
33 | // This is needed to get a clean JS object from the settings itself to provide better accessibility | ||
34 | // Otherwise this will be a mobX instance | ||
35 | this.settings = JSON.parse(JSON.stringify(settings)); | ||
36 | |||
37 | if (typeof this.settingsUpdateHandler === 'function') { | ||
38 | this.settingsUpdateHandler(); | ||
39 | } | ||
40 | } | ||
41 | |||
42 | /** | ||
43 | * Register a settings handler to be executed when the settings change | ||
44 | * | ||
45 | * @param {function} handler | ||
46 | */ | ||
47 | onSettingsUpdate(handler) { | ||
48 | this.settingsUpdateHandler = handler; | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * Set badge count for the current service | ||
53 | * @param {*} direct Direct messages | ||
54 | * @param {*} indirect Indirect messages | ||
55 | */ | ||
56 | setBadge(direct = 0, indirect = 0) { | ||
57 | if (this.recipe && this.recipe.setBadge) { | ||
58 | this.recipe.setBadge(direct, indirect); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | /** | ||
63 | * Inject CSS files into the current page | ||
64 | * | ||
65 | * @param {...string} files | ||
66 | */ | ||
67 | injectCSSFiles(...files) { | ||
68 | if (this.recipe && this.recipe.injectCSS) { | ||
69 | this.recipe.injectCSS(...files); | ||
70 | } | ||
71 | } | ||
72 | |||
73 | /** | ||
74 | * Inject a CSS string into the page | ||
75 | * | ||
76 | * @param {string} css | ||
77 | */ | ||
78 | injectCSS(css) { | ||
79 | const style = document.createElement('style'); | ||
80 | style.textContent = css; | ||
81 | document.head.append(style); | ||
82 | } | ||
83 | |||
84 | /** | ||
85 | * Open "Find in Page" popup | ||
86 | */ | ||
87 | openFindInPage() { | ||
88 | this.controller.openFindInPage(); | ||
89 | } | ||
90 | |||
91 | /** | ||
92 | * Set or update value in storage | ||
93 | * | ||
94 | * @param {*} key | ||
95 | * @param {*} value | ||
96 | */ | ||
97 | set(key, value) { | ||
98 | window.localStorage.setItem( | ||
99 | `ferdi-user-${key}`, JSON.stringify(value), | ||
100 | ); | ||
101 | } | ||
102 | |||
103 | /** | ||
104 | * Get value from storage | ||
105 | * | ||
106 | * @param {*} key | ||
107 | * @return Value of the key | ||
108 | */ | ||
109 | get(key) { | ||
110 | return JSON.parse(window.localStorage.getItem( | ||
111 | `ferdi-user-${key}`, | ||
112 | )); | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Open a URL in an external browser | ||
117 | * | ||
118 | * @param {*} url | ||
119 | */ | ||
120 | externalOpen(url) { | ||
121 | ipcRenderer.sendToHost('new-window', url); | ||
122 | } | ||
123 | |||
124 | /** | ||
125 | * Open a URL in the current service | ||
126 | * | ||
127 | * @param {*} url | ||
128 | */ | ||
129 | internalOpen(url) { | ||
130 | window.location.href = url; | ||
131 | } | ||
132 | } | ||
diff --git a/src/webview/recipe.js b/src/webview/recipe.js index bad5a93b2..8125ec064 100644 --- a/src/webview/recipe.js +++ b/src/webview/recipe.js | |||
@@ -21,6 +21,7 @@ import ignoreList from './darkmode/ignore'; | |||
21 | import customDarkModeCss from './darkmode/custom'; | 21 | import customDarkModeCss from './darkmode/custom'; |
22 | 22 | ||
23 | import RecipeWebview from './lib/RecipeWebview'; | 23 | import RecipeWebview from './lib/RecipeWebview'; |
24 | import Userscript from './lib/Userscript'; | ||
24 | 25 | ||
25 | import spellchecker, { switchDict, disable as disableSpellchecker, getSpellcheckerLocaleByFuzzyIdentifier } from './spellchecker'; | 26 | import spellchecker, { switchDict, disable as disableSpellchecker, getSpellcheckerLocaleByFuzzyIdentifier } from './spellchecker'; |
26 | import { injectDarkModeStyle, isDarkModeStyleInjected, removeDarkModeStyle } from './darkmode'; | 27 | import { injectDarkModeStyle, isDarkModeStyleInjected, removeDarkModeStyle } from './darkmode'; |
@@ -55,6 +56,8 @@ class RecipeController { | |||
55 | 56 | ||
56 | recipe = null; | 57 | recipe = null; |
57 | 58 | ||
59 | userscript = null; | ||
60 | |||
58 | hasUpdatedBeforeRecipeLoaded = false; | 61 | hasUpdatedBeforeRecipeLoaded = false; |
59 | 62 | ||
60 | constructor() { | 63 | constructor() { |
@@ -130,7 +133,8 @@ class RecipeController { | |||
130 | const userJsModule = require(userJs); | 133 | const userJsModule = require(userJs); |
131 | 134 | ||
132 | if (typeof userJsModule === 'function') { | 135 | if (typeof userJsModule === 'function') { |
133 | userJsModule(config); | 136 | this.userscript = new Userscript(this.recipe, this, config); |
137 | userJsModule(config, this.userscript); | ||
134 | } | 138 | } |
135 | }; | 139 | }; |
136 | 140 | ||
@@ -153,6 +157,11 @@ class RecipeController { | |||
153 | debug('isDarkModeEnabled', this.settings.service.isDarkModeEnabled); | 157 | debug('isDarkModeEnabled', this.settings.service.isDarkModeEnabled); |
154 | debug('System spellcheckerLanguage', this.settings.app.spellcheckerLanguage); | 158 | debug('System spellcheckerLanguage', this.settings.app.spellcheckerLanguage); |
155 | debug('Service spellcheckerLanguage', this.settings.service.spellcheckerLanguage); | 159 | debug('Service spellcheckerLanguage', this.settings.service.spellcheckerLanguage); |
160 | debug('darkReaderSettigs', this.settings.service.darkReaderSettings); | ||
161 | |||
162 | if (this.userscript && this.userscript.internal_setSettings) { | ||
163 | this.userscript.internal_setSettings(this.settings); | ||
164 | } | ||
156 | 165 | ||
157 | if (this.settings.app.enableSpellchecking) { | 166 | if (this.settings.app.enableSpellchecking) { |
158 | debug('Setting spellchecker language to', this.spellcheckerLanguage); | 167 | debug('Setting spellchecker language to', this.spellcheckerLanguage); |
@@ -225,7 +234,8 @@ class RecipeController { | |||
225 | console.log('Injecting DarkReader'); | 234 | console.log('Injecting DarkReader'); |
226 | 235 | ||
227 | // Use darkreader instead | 236 | // Use darkreader instead |
228 | enableDarkMode({}, { | 237 | const { brightness, contrast, sepia } = this.settings.service.darkReaderSettings; |
238 | enableDarkMode({ brightness, contrast, sepia }, { | ||
229 | css: customDarkModeCss[window.location.host] || '', | 239 | css: customDarkModeCss[window.location.host] || '', |
230 | }); | 240 | }); |
231 | this.universalDarkModeInjected = true; | 241 | this.universalDarkModeInjected = true; |
@@ -322,7 +332,6 @@ new RecipeController(); | |||
322 | // Patching window.open | 332 | // Patching window.open |
323 | const originalWindowOpen = window.open; | 333 | const originalWindowOpen = window.open; |
324 | 334 | ||
325 | |||
326 | window.open = (url, frameName, features) => { | 335 | window.open = (url, frameName, features) => { |
327 | if (!url && !frameName && !features) { | 336 | if (!url && !frameName && !features) { |
328 | // The service hasn't yet supplied a URL (as used in Skype). | 337 | // The service hasn't yet supplied a URL (as used in Skype). |