From c95a3c27e5e1d126059b3c086a4946b6aed644c4 Mon Sep 17 00:00:00 2001 From: gamerdonkey Date: Mon, 9 Jan 2023 05:28:35 +0000 Subject: [PATCH] Adding ability to share a link to a filled out WAKI-LIBS form. --- batch/process_wiki_article.py | 5 +++- web/index.html | 29 ++++++++++++++++--- web/script.js | 52 ++++++++++++++++++++++++++++++++--- 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/batch/process_wiki_article.py b/batch/process_wiki_article.py index 9d93c94..dc6ed0f 100644 --- a/batch/process_wiki_article.py +++ b/batch/process_wiki_article.py @@ -1,3 +1,4 @@ +import hashlib import json import random import re @@ -96,7 +97,9 @@ def adlibify(wiki_page, min_adlib_rest): output_tokens.append(output_token) i += 1 - article = {'title': wiki_page.displaytitle, 'url': wiki_page.canonicalurl} + url = wiki_page.canonicalurl + url_hash = hashlib.sha1(url.encode('utf-8')).hexdigest()[:8] + article = {'title': wiki_page.displaytitle, 'url': url, 'hash': url_hash} article['tokens'] = output_tokens return article diff --git a/web/index.html b/web/index.html index 6a46792..d457806 100644 --- a/web/index.html +++ b/web/index.html @@ -5,6 +5,20 @@ WAKI-LIBS -

WAKI-LIBS: Wikipedia Ad-Libbed

-
+

WAKI-LIBS: Wikipedia Ad-Libbed

+
+


diff --git a/web/script.js b/web/script.js index 0bbabe2..d3cef4b 100644 --- a/web/script.js +++ b/web/script.js @@ -4,15 +4,57 @@ function createInputs(article) { if(token.adlib_tag) { inputs += `

- - + +

` } }) document.getElementById('inputs').innerHTML = inputs + + const urlParams = new URL(window.location.toLocaleString()).searchParams + if(urlParams.has('stored')) { + try { + let storedInputs = JSON.parse(atob(urlParams.get('stored'))) + + if(storedInputs.h !== article.hash) { + showError("Unable to use shared link because it's for an old article, and well, time makes fools of us all.") + } + else { + document.querySelectorAll('.token-input').forEach(function(tokenInput) { + let index = tokenInput.id.split('-')[1] + tokenInput.value = storedInputs.i[index] + }); + } + } + catch(err) { + showError("Unable to parse shared link.") + } + } } + +function showError(errorMessage) { + errorDiv = document.getElementById('error') + errorDiv.innerHTML = errorMessage; + errorDiv.style.display = 'block' +} + + +function makeSharableLink(article) { + let userInputs = {} + document.querySelectorAll('.token-input').forEach(function(tokenInput) { + let index = tokenInput.id.split('-')[1] + userInputs[index] = tokenInput.value + }); + + let storedInputs = {h: article.hash, i: userInputs} + let url = new URL(window.location.toLocaleString()) + url.searchParams.set('stored', btoa(JSON.stringify(storedInputs))) + return(url.toLocaleString()) +} + + function showArticle(article) { const noSpaceBeforeTags = new Set(['.', ',', ')', ';', ':', '!', '?', "''", 'POS']) const noSpaceBeforeTokens = new Set(["n't", ']']) @@ -20,7 +62,7 @@ function showArticle(article) { const noSpaceAfterTokens = new Set(['[']) document.getElementById('title').innerHTML = article.title - document.getElementById('link').innerHTML = `[ Original Article ]` + document.getElementById('original-article-link').href = article.url let output = '' let spaceBefore = false @@ -30,7 +72,7 @@ function showArticle(article) { } spaceBefore = true - let adlibInput = document.getElementById(`token_${token.id}`); + let adlibInput = document.getElementById(`token-${token.id}`); if(adlibInput && adlibInput.value) { output += `${adlibInput.value}` } @@ -44,10 +86,12 @@ function showArticle(article) { }) document.getElementById('summary').innerHTML = output + document.getElementById('sharable-link').href = makeSharableLink(article) document.getElementById('article').classList.add('visible') } + createInputs(article) document.addEventListener('click', function (event) {