Skip to content

Instantly share code, notes, and snippets.

@trulysinclair
Last active December 4, 2024 18:29
Show Gist options
  • Save trulysinclair/871762b37b84d247d33f28080c2b20c9 to your computer and use it in GitHub Desktop.
Save trulysinclair/871762b37b84d247d33f28080c2b20c9 to your computer and use it in GitHub Desktop.
Simple Spellcheck for Views
// A BrowserView can be used to embed additional web content into a BrowserWindow.
// It is like a child window, except that it is positioned relative to its owning
// window. It is meant to be an alternative to the webview tag.
//
// For more info, see:
// https://electronjs.org/docs/api/browser-view
// In the main process.
const { BrowserView, BrowserWindow, app, Menu, MenuItem } = require('electron/main')
app.whenReady().then(() => {
let win = new BrowserWindow({
width: 800, height: 600, webPreferences: {
nodeIntegration: true
}
})
win.on('closed', () => {
win = null
})
const view = new BrowserView({
webPreferences: {
spellcheck: true
}
})
view.webContents.session.setSpellCheckerLanguages(['en-US']);
win.setBrowserView(view)
view.setBounds({ x: 0, y: 0, width: 800, height: 600 })
view.webContents.loadURL('https://quilljs.com/playground/snow')
view.webContents.on('context-menu', (event, params) => {
const menu = new Menu()
// Add each spelling suggestion
for (const suggestion of params.dictionarySuggestions) {
menu.append(new MenuItem({
label: suggestion,
click: () => view.webContents.replaceMisspelling(suggestion)
}))
}
// Allow users to add the misspelled word to the dictionary
if (params.misspelledWord) {
menu.append(
new MenuItem({
label: 'Add to dictionary',
click: () => view.webContents.session.addWordToSpellCheckerDictionary(params.misspelledWord)
})
)
}
menu.popup()
})
console.log(view.webContents.session.availableSpellCheckerLanguages)
})
{
"name": "berserk-show-telephone-b5qdg",
"productName": "berserk-show-telephone-b5qdg",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "trulysinclair",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "32.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment