This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
tampermonkey-spellcheck.js
69 lines (59 loc) · 1.8 KB
/
tampermonkey-spellcheck.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// ==UserScript==
// @name Spell Checking toggle
// @description Enable browser plugin spell checking within website content
// @author Codeconut Ltd.
// @version 0.0.1
// @grant none
// @namespace CodeconutSpellCheck
// @include http://example.com/*
// @include http://localhost:1234/*
// ==/UserScript==
(function () {
'use strict';
class SpellCheckHelper {
/**
* Pass dependencies and bind public interface to manually trigger spell check.
*
* @param {string} nodeSelector DOM nodes where spell check will be made available (as CSS selector).
* @param {object} win window reference.
*/
constructor(nodeSelector, win) {
this.nodeSelector = nodeSelector;
this.win = win;
this.doc = win.document;
window.ccSpellCheck = () => {
this.enable();
};
}
/**
* Enable spell checking support.
* Freshly check for currently available DOM elements.
*/
enable() {
const nodeList = document.querySelectorAll(this.nodes);
Array.from(nodeList).map(this.transformElements);
this.transformBody();
}
/**
*
*/
transformBody() {
this.doc.body.contentEditable = true;
this.doc.designMode = 'on';
}
/**
* Modify desired HTML elements to make them spell-check compatible.
*/
// eslint-disable-next-line no-unused-vars
transformElements(element, _index) {
// For all spell checking tools
element.setAttribute('spellcheck', 'true');
element.setAttribute('contenteditable', 'true');
// Additionally needed by Grammarly
element.setAttribute('tabindex', '0');
}
}
// Run
const spellCheckHelper = new SpellCheckHelper('h1, h2, h3, h4, h5, h6, p, li, span', window);
spellCheckHelper.enable();
})();