-
Notifications
You must be signed in to change notification settings - Fork 6
/
inject.js
49 lines (46 loc) · 1.8 KB
/
inject.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
/**
* This is a script to inject prosemirror-dev-toolkit into any live editor instance
*
* Basically what you do is inspect the element ProseMirror editor is mounted to,
* a div with .ProseMirror class is a good guess. Then you just copy-paste this script
* to the browser console and change the `viewOrSelector` parameter as needed.
*
* If there is a strict CSP enabled you might need to disable it. I've used this:
* https://chrome.google.com/webstore/detail/disable-content-security/ieelmcmcagommplceebfedjlakkhpden
*/
;(async viewOrSelector => {
function getEditorView(selector) {
const el = document.querySelector(selector)
const oldFn = el.pmViewDesc.updateChildren
const childWithSelectNode = Array.from(el.children).find(
child => child.pmViewDesc && child.pmViewDesc.selectNode !== undefined
)
if (childWithSelectNode === undefined) {
console.error(
'Failed to find a ProseMirror child NodeViewDesc with selectNode function (which is strange)'
)
} else {
childWithSelectNode.pmViewDesc.selectNode()
childWithSelectNode.pmViewDesc.deselectNode()
}
return new Promise((res, rej) => {
el.pmViewDesc.updateChildren = (view, pos) => {
el.pmViewDesc.updateChildren = oldFn
res(view)
return Function.prototype.bind.apply(oldFn, view, pos)
}
})
}
const editorView =
typeof viewOrSelector === 'string' ? await getEditorView(viewOrSelector) : viewOrSelector
if (editorView) {
fetch('https://unpkg.com/prosemirror-dev-toolkit/dist/bundle.umd.min.js')
.then(response => response.text())
.then(data => {
eval(data)
window.applyDevTools(editorView, { buttonPosition: 'bottom-right' })
})
} else {
console.error('No EditorView found or provided')
}
})('.ProseMirror')