Skip to content

Instantly share code, notes, and snippets.

@Fevol
Last active January 8, 2023 23:35
Show Gist options
  • Save Fevol/9ec53ef916414737b63eac1ea2a5318b to your computer and use it in GitHub Desktop.
Save Fevol/9ec53ef916414737b63eac1ea2a5318b to your computer and use it in GitHub Desktop.
Electron spellchecker API issue
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<link href="./styles.css" rel="stylesheet">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>
// Modules to control application life and create native browser window
const {app, BrowserWindow, session, ipcMain} = require('electron')
const path = require('path')
function createWindow () {
session.defaultSession.setSpellCheckerLanguages(['en-US'])
ipcMain.on('update-spellchecker-languages', (event, languages) => {
session.defaultSession.setSpellCheckerLanguages(languages)
console.log("Updated spellchecker languages: ", session.defaultSession.getSpellCheckerLanguages())
});
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 450,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
spellcheck: true,
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
const { contextBridge, ipcRenderer, webFrame, ipcMain } = require('electron');
// Test helpers
const test = {
assert: (ok, ...logs) => {
if (!ok) test.fail(...logs)
},
fail: (...logs) => test.done(false, ...logs),
done: (success = true, ...logs) => {
if (!success) logs.unshift(new Error('test failed'))
ipcRenderer.send('test-done', success, ...logs)
},
isWordMisspelled: (str) => webFrame.isWordMisspelled(str),
getSpellCheckerSuggestions: (str) => webFrame.getWordSuggestions(str),
setSpellCheckerLanguages: (languages) => ipcRenderer.send('update-spellchecker-languages', languages),
};
contextBridge.exposeInMainWorld('test', test);
window.addEventListener('DOMContentLoaded', () => {
const platform_information = document.createElement('div');
platform_information.style.marginTop = '20px';
document.body.appendChild(platform_information);
platform_information.innerText = `platform: ${process.platform} ${process.arch}\nelectron: ${process.versions.electron}\nnode: ${process.versions.node}\nchrome: ${process.versions.chrome}`;
})
/**
* This file is loaded via the <script> tag in the index.html file and will
* be executed in the renderer process for that window. No Node.js APIs are
* available in this process because `nodeIntegration` is turned off and
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
* to expose Node.js functionality from the main process.
*/
let element = document.createElement('textarea');
element.spellcheck = true;
element.value = 'ths is an incorrctly spelld englsh sentense\ndt is een fuot gesplde nederlands zin\nund ein faelsch geschrjebener deutschez Satz';
element.style = "min-width: 400px; min-height: 100px; margin-bottom: 10px;";
document.body.appendChild(element);
element.focus();
let selected_text = '';
let text_selection = document.createElement('div');
document.body.appendChild(text_selection);
text_selection.innerText = "selection: ";
let selection_is_misspelled = document.createElement('div');
document.body.appendChild(selection_is_misspelled);
selection_is_misspelled.innerText = "misspelled: ";
let spellchecker_suggestions = document.createElement('div');
document.body.appendChild(spellchecker_suggestions);
spellchecker_suggestions.innerText = "suggestions: ";
function update_spellchecker() {
text_selection.innerText = "selection: " + selected_text;
selection_is_misspelled.innerText = "misspelled: " + window.test.isWordMisspelled(selected_text);
spellchecker_suggestions.innerText = "suggestions: " + window.test.getSpellCheckerSuggestions(selected_text) || 'no suggestions';
}
element.onmouseup = function() {
selected_text = element.value.substring(element.selectionStart, element.selectionEnd);
update_spellchecker();
}
let spellchecker_languages = ['en-US'];
let current_spellchecker_languages = document.createElement('div');
current_spellchecker_languages.style.marginTop = '20px';
// Spellchecker languages: currently selected and selectable
document.body.appendChild(current_spellchecker_languages);
current_spellchecker_languages.innerText = "current spellchecker languages: ";
update_languages();
available_spellchecker_languages = document.createElement('select');
let option = document.createElement('option');
option.innerText = "add language";
option.value = "";
available_spellchecker_languages.appendChild(option);
document.body.appendChild(available_spellchecker_languages);
const all_spellchecker_languages = ['af', 'pt', 'nl', 'en-US', 'en-GB', 'fr', 'de', 'es', 'it'];
for (let i = 0; i < all_spellchecker_languages.length; i++) {
let option = document.createElement('option');
available_spellchecker_languages.appendChild(option);
option.value = all_spellchecker_languages[i];
option.innerText = all_spellchecker_languages[i];
}
available_spellchecker_languages.onchange = function() {
if (!spellchecker_languages.includes(available_spellchecker_languages.value)) {
spellchecker_languages.push(available_spellchecker_languages.value);
window.test.setSpellCheckerLanguages(spellchecker_languages);
update_languages();
// Wait 0.3 ms for spellchecker to update
setTimeout(function() { update_spellchecker() }, 300);
}
available_spellchecker_languages.value = "";
}
function update_languages() {
current_spellchecker_languages.innerHTML = "current spellchecker languages: ";
for (let i = 0; i < spellchecker_languages.length; i++) {
let language = document.createElement('span');
language.innerText = spellchecker_languages[i];
language.style = "margin-right: 10px; cursor: pointer; text-decoration: underline;";
language.onclick = function() {
spellchecker_languages.splice(i, 1);
window.test.setSpellCheckerLanguages(spellchecker_languages);
update_languages();
// Wait 0.3 ms for spellchecker to update
setTimeout(function() { update_spellchecker() }, 300);
}
current_spellchecker_languages.appendChild(language);
}
}
@Fevol
Copy link
Author

Fevol commented Jan 8, 2023

Related to issue: electron/electron#28684
Demo of issue below:

electron-spellchecker-bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment