2020-03-16 12:21:34 +01:00
|
|
|
import UserSession from './shared/models/UserSession.js';
|
2020-03-16 08:24:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a uCaptcha box
|
|
|
|
* @param {string} key Website key
|
|
|
|
* @return {HTMLElement} uCaptcha box
|
|
|
|
*/
|
|
|
|
function uCaptchaBox(key) {
|
2020-03-16 12:21:34 +01:00
|
|
|
const checkbox = createElement('div', {
|
|
|
|
style: 'cursor:pointer;border-radius:3px;border:2px solid #888;width:25px;height:25px;display:inline-block',
|
2020-03-16 08:24:10 +01:00
|
|
|
});
|
|
|
|
checkbox.onclick = function() {
|
|
|
|
fetch(`https://localhost:444/api/init?k=${key}`)
|
|
|
|
.then((r)=>r.text())
|
|
|
|
.then((r)=>r.substr(2))
|
|
|
|
.then((r)=>JSON.parse(r))
|
|
|
|
.then((resp)=>{
|
|
|
|
const session = new UserSession();
|
|
|
|
session.deserialize(resp);
|
|
|
|
});
|
|
|
|
|
2020-03-16 12:21:34 +01:00
|
|
|
checkbox.setAttribute('style',
|
|
|
|
checkbox.getAttribute('style') + 'background-color:royalblue;');
|
2020-03-16 08:24:10 +01:00
|
|
|
};
|
2020-03-16 12:21:34 +01:00
|
|
|
const captchaBox = createElement('div');
|
2020-03-16 08:24:10 +01:00
|
|
|
captchaBox.appendChild(checkbox);
|
|
|
|
return captchaBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate a uCaptcha box
|
|
|
|
* @param {string} websiteKey
|
|
|
|
* @param {string} selector
|
|
|
|
*/
|
|
|
|
export function create(websiteKey, selector) {
|
|
|
|
// const iframe = createElement("iframe");
|
|
|
|
// iframe.setAttribute("src", "https://localhost:444/?k="+websiteKey)
|
|
|
|
|
|
|
|
document.querySelector(selector).appendChild(uCaptchaBox(websiteKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Short hand for document.createElement
|
|
|
|
* @param {string} tagName Tag name
|
|
|
|
* @param {object} attributes A key-value pair of DOM attributes
|
|
|
|
* @return {HTMLElement}
|
|
|
|
*/
|
|
|
|
function createElement(tagName, attributes={}) {
|
|
|
|
const elem = document.createElement(tagName);
|
2020-03-16 12:21:34 +01:00
|
|
|
for (const [name, value] of Object.entries(attributes)) {
|
2020-03-16 08:24:10 +01:00
|
|
|
elem.setAttribute(name, value);
|
2020-03-16 12:21:34 +01:00
|
|
|
}
|
|
|
|
return elem;
|
2020-03-16 08:24:10 +01:00
|
|
|
}
|