-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clerk #50
Open
ehannes
wants to merge
6
commits into
master
Choose a base branch
from
clerk
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Clerk #50
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10b142a
Adds clerk component
ehannes e7634bd
Changes dev server and default port
ehannes dc50839
Fixes invalid success slot in clerk
ehannes 274091a
Cleans code in clerk
ehannes 4e33dc2
Prevent Clerk from posting when already posting
ehannes c985c28
Adds some notes to Clerk
ehannes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@oma-wc/clerk", | ||
"version": "0.11.0", | ||
"description": "", | ||
"main": "dist/index.cjs.js", | ||
"unpkg": "dist/index.cjs.js", | ||
"module": "dist/index.es.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/oma-wc/oma.git", | ||
"directory": "components/clerk" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "GPL3", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"haunted": "^4.7.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import configure from '../../rollup.config' | ||
import { dependencies } from './package.json' | ||
|
||
export default configure({ | ||
input: './src/index.js', | ||
dependencies, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
const version="1.1.0"; | ||
|
||
const obfuscate_string = (string) => { | ||
const length = string.length; | ||
let result = ''; | ||
let random_letter; | ||
|
||
for (let i = 0; i < length; i++) { | ||
random_letter = String.fromCharCode(97 + Math.floor(25 * Math.random())); | ||
result += string[i] + random_letter; | ||
} | ||
|
||
return result | ||
}; | ||
|
||
const create_class_list = (...mixed_array) => | ||
mixed_array.filter(Boolean).join(' '); | ||
|
||
const create_honeypot_label = ({ for_id, class_name, extra_classes }) => { | ||
const honeypot_label = document.createElement('label'); | ||
|
||
honeypot_label.for = for_id; | ||
honeypot_label.className = create_class_list(class_name, extra_classes); | ||
|
||
return honeypot_label | ||
}; | ||
|
||
const create_honeypot_field = ({ | ||
id, | ||
type, | ||
name, | ||
class_name, | ||
extra_classes, | ||
}) => { | ||
const honeypot_field = document.createElement('textarea'); | ||
|
||
honeypot_field.id = id; | ||
honeypot_field.name = name; | ||
honeypot_field.className = create_class_list(class_name, extra_classes); | ||
honeypot_field.autocomplete = 'false'; | ||
honeypot_field.tabindex = -1; | ||
|
||
return honeypot_field | ||
}; | ||
|
||
const create_timer_field = ({ class_name, name }) => { | ||
const timer_field = document.createElement('input'); | ||
timer_field.className = class_name; | ||
timer_field.name = obfuscate_string(name); | ||
timer_field.type = 'hidden'; | ||
timer_field.value = 0; | ||
|
||
return timer_field | ||
}; | ||
|
||
const create_version_field = ({ class_name, name }) => { | ||
const version_field = document.createElement('input'); | ||
version_field.className = class_name; | ||
version_field.name = obfuscate_string(name); | ||
version_field.type = 'hidden'; | ||
|
||
return version_field | ||
}; | ||
|
||
const create_config = (options = {}) => { | ||
const autobind = options.hasOwnProperty('autobind') ? options.autobind : true; | ||
|
||
const { | ||
id: sheet_id, | ||
field_prefix = 'clerk-field', | ||
label_prefix = 'clerk-label', | ||
} = options.stylesheet || {}; | ||
|
||
const { | ||
id: field_id = 'comment', | ||
class: field_class_name, | ||
extra_classes: field_extra_classes, | ||
} = options.field || {}; | ||
|
||
const { class: label_class_name, extra_classes: label_extra_classes } = | ||
options.label || {}; | ||
const { class: timer_class_name, name: timer_name = 'timer' } = | ||
options.timer || {}; | ||
const { class: version_class_name, name: version_name = 'version' } = | ||
options.version || {}; | ||
|
||
const honeypot_field_class_name = | ||
field_class_name || `${field_prefix}__${field_id}`; | ||
const honeypot_label_class_name = | ||
label_class_name || `${label_prefix}__${field_id}`; | ||
const honeypot_timer_class_name = | ||
timer_class_name || `${field_prefix}__${timer_name}`; | ||
const honeypot_version_class_name = | ||
version_class_name || `${field_prefix}__${version_name}`; | ||
|
||
return { | ||
autobind, | ||
style: { | ||
id: sheet_id, | ||
field_prefix: field_prefix, | ||
label_prefix: label_prefix, | ||
field_class_name: honeypot_field_class_name, | ||
label_class_name: honeypot_label_class_name, | ||
}, | ||
honeypot: { | ||
field: { | ||
id: field_id, | ||
name: 'comment', | ||
class_name: honeypot_field_class_name, | ||
extra_classes: field_extra_classes, | ||
}, | ||
label: { | ||
for_id: field_id, | ||
class_name: honeypot_label_class_name, | ||
extra_classes: label_extra_classes, | ||
}, | ||
timer: { | ||
class_name: honeypot_timer_class_name, | ||
name: timer_name, | ||
}, | ||
version: { | ||
version, | ||
class_name: honeypot_version_class_name, | ||
name: version_name, | ||
}, | ||
}, | ||
} | ||
}; | ||
|
||
class StyleSheet { | ||
constructor(id) { | ||
this.sheet = id ? document.getElementById(id) : this.create_sheet(); | ||
} | ||
|
||
create_sheet() { | ||
var style_element = document.createElement('style'); | ||
// WebKit hack :( | ||
style_element.appendChild(document.createTextNode('')); | ||
|
||
document.head.appendChild(style_element); | ||
|
||
return style_element.sheet | ||
} | ||
|
||
add_rule(selector, rules) { | ||
if ('insertRule' in this.sheet) { | ||
this.sheet.insertRule(selector + '{' + rules + '}'); | ||
} else if ('addRule' in this.sheet) { | ||
this.sheet.addRule(selector, rules); | ||
} | ||
} | ||
} | ||
|
||
class Clerk { | ||
constructor(form, options = {}) { | ||
this.version = version; | ||
this.loaded_at = new Date(); | ||
this.form = form; | ||
|
||
this.config = create_config(options); | ||
|
||
this.append_stylesheet(); | ||
this.obfuscate_field_names(); | ||
this.append_honeypot_fields(); | ||
this.append_timer_field(); | ||
this.append_version_field(); | ||
|
||
if (this.config.autobind) { | ||
this.bind(); | ||
} else { | ||
return this.on_submit.bind(this) | ||
} | ||
} | ||
|
||
bind() { | ||
this.form.addEventListener('submit', this.on_submit.bind(this)); | ||
} | ||
|
||
append_stylesheet() { | ||
const stylesheet = new StyleSheet(this.config.style.id); | ||
|
||
stylesheet.add_rule( | ||
`.${this.config.honeypot.field.class_name}`, | ||
` | ||
position: absolute; | ||
right: 200vw; | ||
` | ||
); | ||
stylesheet.add_rule( | ||
`.${this.config.honeypot.label.class_name}`, | ||
` | ||
position: absolute; | ||
right: 200vw; | ||
` | ||
); | ||
} | ||
|
||
obfuscate_field_names() { | ||
const form = this.form; | ||
const fields = Array.from( | ||
form.querySelectorAll('input:not([type=submit]),select,textarea') | ||
); | ||
fields.forEach((field) => { | ||
const id_or_name = field.id || field.name; | ||
if (!id_or_name) { | ||
console.warn('Found field without name:', field); | ||
return | ||
} | ||
|
||
const label = this.form.querySelector(`label[for=${id_or_name}]`); | ||
if (label) { | ||
label.classList.add(`${this.config.style.label_prefix}__${id_or_name}`); | ||
} | ||
field.classList.add(`${this.config.style.field_prefix}__${id_or_name}`); | ||
field.name = obfuscate_string(field.name); | ||
}); | ||
} | ||
|
||
append_honeypot_fields() { | ||
const label = create_honeypot_label(this.config.honeypot.label); | ||
const field = create_honeypot_field(this.config.honeypot.field); | ||
|
||
this.form.appendChild(label); | ||
this.form.appendChild(field); | ||
} | ||
|
||
append_timer_field() { | ||
this.timer_field = create_timer_field(this.config.honeypot.timer); | ||
this.form.appendChild(this.timer_field); | ||
} | ||
|
||
append_version_field() { | ||
this.version_field = create_version_field(this.config.honeypot.version); | ||
this.form.appendChild(this.version_field); | ||
} | ||
|
||
active_for() { | ||
return new Date() - this.loaded_at | ||
} | ||
|
||
on_submit(event) { | ||
this.timer_field.value = this.active_for(); | ||
this.version_field.value = this.version; | ||
} | ||
} | ||
|
||
export { Clerk as default }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider depending on Clerk here instead.