Skip to content

Commit

Permalink
feat(bookmaklet): base engine
Browse files Browse the repository at this point in the history
  • Loading branch information
ncarlier committed Jan 31, 2021
1 parent 923c322 commit e93a894
Show file tree
Hide file tree
Showing 8 changed files with 8,060 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
release/
dist/
node_modules/
ui/build/
TODO.md
_gen/
.env
.vscode
.cache
19 changes: 19 additions & 0 deletions bookmarklet/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,css}]
charset = utf-8

# 2 space indentation
[*.{js,css,yml,json,html,svg,jsx,tsx}]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions bookmarklet/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/**
28 changes: 28 additions & 0 deletions bookmarklet/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
],
"plugins": ["@typescript-eslint", "prettier"],
"env": {
"browser": true,
"es6": true
},
"rules": {
"prettier/prettier": ["error", {
"singleQuote": true,
"semi" : false,
"printWidth": 120
}],
"no-console": "off",
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/interface-name-prefix": 0,
"@typescript-eslint/explicit-member-accessibility": 0,
"@typescript-eslint/explicit-function-return-type": 0
},
"settings": {
},
"parser": "@typescript-eslint/parser"
}
119 changes: 119 additions & 0 deletions bookmarklet/bookmarklet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
export {}

declare global {
interface Window {
readflowBookmarklet: ReadflowBookmarklet
}
}

const trapMouseEvent = function (callback: (el: HTMLElement) => void) {
return function (ev: MouseEvent) {
ev.cancelBubble = true
ev.stopPropagation()
const el = this as HTMLElement
callback(el)
}
}

const setRemoveStyle = function (el: HTMLElement) {
el.style.background = 'orange'
el.style.border = '2px dashed red'
el.style.cursor = 'crosshair'
}

const setKeepStyle = function (el: HTMLElement) {
el.style.background = 'greenyellow'
el.style.border = '2px dashed green'
el.style.cursor = 'crosshair'
}

const unsetMouseOverStyle = function (el: HTMLElement) {
el.style.background = 'initial'
el.style.border = 'initial'
el.style.cursor = 'initial'
}

type HistoryItem = {
parent: Node
element: HTMLElement | ChildNode
placeholder: Text
}

class ReadflowBookmarklet {
private doc: Document
private history: HistoryItem[]
private alt: boolean

constructor() {
this.doc = document
this.history = []
this.alt = false
}

private clickElement(el: HTMLElement) {
unsetMouseOverStyle(el)
if (this.alt) {
this.doc.body.childNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE && node.nodeName !== 'SCRIPT') {
const placeholder = this.doc.createTextNode('')
this.history.push({
parent: this.doc.body,
element: node,
placeholder,
})
node.parentNode.replaceChild(placeholder, node)
}
})
this.doc.body.appendChild(el)
} else {
const placeholder = this.doc.createTextNode('')
this.history.push({
parent: el.parentNode,
element: el,
placeholder,
})
el.parentNode.replaceChild(placeholder, el)
}
}

private undo() {
if (this.history.length) {
const item = this.history.pop()
item.parent.replaceChild(item.element, item.placeholder)
}
}

private toggleMouseOverStyle(el: HTMLElement) {
this.alt ? setKeepStyle(el) : setRemoveStyle(el)
}

private registerEventsListeners() {
const onmouseover = trapMouseEvent(this.toggleMouseOverStyle.bind(this))
const onmouseout = trapMouseEvent(unsetMouseOverStyle)
const onclick = trapMouseEvent(this.clickElement.bind(this))
this.doc.body.querySelectorAll<HTMLElement>('*').forEach((node) => {
node.onmouseover = onmouseover
node.onmouseout = onmouseout
node.onclick = onclick
})
}

private registerKeyboardShortcuts() {
this.doc.body.addEventListener('keydown', (ev: KeyboardEvent) => {
if (ev.ctrlKey && ev.code === 'Backspace') {
this.undo()
}
if (ev.ctrlKey && ev.code === 'Backslash') {
this.alt = !this.alt
}
})
}

boot() {
console.log('running bookmarklet...')
this.registerEventsListeners()
this.registerKeyboardShortcuts()
}
}

window.readflowBookmarklet = new ReadflowBookmarklet()
24 changes: 24 additions & 0 deletions bookmarklet/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<body>
<h1>Readflow bookmarklet</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc.
</p>
<p>
Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
</p>
<p>
Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui.
</p>
<p>
Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi. Integer lacinia sollicitudin massa. Cras metus.
</p>
<p>
Sed aliquet risus a tortor. Integer id quam. Morbi mi. Quisque nisl felis, venenatis tristique, dignissim in, ultrices sit amet, augue. Proin sodales libero eget ante. Nulla quam. Aenean laoreet. Vestibulum nisi lectus, commodo ac, facilisis ac, ultricies eu, pede. Ut orci risus, accumsan porttitor, cursus quis, aliquet eget, justo. Sed pretium blandit orci. Ut eu diam at pede suscipit sodales. </p>
</p>
<script src="./bookmarklet.ts"></script>
<script>
window.readflowBookmarklet.boot()
</script>
</body>
</html>
Loading

0 comments on commit e93a894

Please sign in to comment.