From 21065e09d7e67615a73f50df751adbe34a23c420 Mon Sep 17 00:00:00 2001 From: Patrick Tang <36460266+patricksptang@users.noreply.github.com> Date: Mon, 24 Jun 2024 15:49:59 -0700 Subject: [PATCH] remove tagcloud which is no longer neeed --- .../cloudflare_caching/createhomepagecache | 103 ++++++++++++++++++ .../cloudflare_caching/sendcachedhomepage | 33 ++++++ step-web/pom.xml | 2 +- .../js/backbone/views/view_wordle_stat.js | 17 +-- .../src/main/webapp/js/require_config_dev.js | 2 +- 5 files changed, 147 insertions(+), 10 deletions(-) create mode 100644 step-tools/src/main/resources/cloudflare_caching/createhomepagecache create mode 100644 step-tools/src/main/resources/cloudflare_caching/sendcachedhomepage diff --git a/step-tools/src/main/resources/cloudflare_caching/createhomepagecache b/step-tools/src/main/resources/cloudflare_caching/createhomepagecache new file mode 100644 index 0000000000..a44d2e6199 --- /dev/null +++ b/step-tools/src/main/resources/cloudflare_caching/createhomepagecache @@ -0,0 +1,103 @@ +addEventListener('fetch', event => { + event.respondWith(handleRequest(event.request)) +}) + +const url = "https://www.stepbible.org/?" +async function gatherResponse(response) { + const { headers } = response + const contentType = headers.get("content-type") || "" + if (contentType.includes("application/json")) { + console.log("json") + return JSON.stringify(await response.json()) + } + else if (contentType.includes("application/text")) { + console.log("application") + return response.text() + } + else if (contentType.includes("text/html")) { + return response.text() + } + else { + console.log("other") + return response.text() + } +} + +async function handleRequest(request) { + const userurl = request.url + + // Function to parse query strings + function getParameterByName(name) { + name = name.replace(/[\[\]]/g, '\\$&') + name = name.replace(/\//g, '') + var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), + results = regex.exec(userurl) + + if (!results) return null + else if (!results[2]) return '' + else if (results[2]) { + results[2] = results[2].replace(/\//g, '') + } + + return decodeURIComponent(results[2].replace(/\+/g, ' ')); + } + + const langs = ["en", "es", "zh", "zh-tw", "aa", "af", "ar", "az", "be", "bg", "bn", "cs", "cy", "de", "el", "et", "fa", "fi", "fr", "ga", "gl", "gu", "hi", "hr", "hu", "id", "in", "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "ml", "ms", "mt", "nl", "no", "pl", "pt", "ro", "ru", "ry", "sk", "sl", "sq", "sr", "sv", "sw", "ta", "te", "th", "tr", "uk", "ur", "uz", "vi"]; + const respInit = { + headers: { + "content-type": "text/html;charset=UTF-8", + "accept-language": "en" + }, + } + const languageSelected = getParameterByName('lang') + if ((typeof languageSelected === "string") && (langs.indexOf(languageSelected) > -1)) { + var init = {} + init = { + headers: { + "content-type": "text/html;charset=UTF-8", + "accept-language": languageSelected + }, + } + const response = await fetch(url, init) + var results = await gatherResponse(response) + var resultsLines = results.split("\n") + var cfScriptRemoved = "" + var newResults = "" + var linesAdded = 0 + for (var i = 0; i < resultsLines.length; i++ ) { + var curLine = resultsLines[i].trim() + if ((curLine.indexOf(""))) { + cfScriptRemoved = ", Cloudflare script was removed" + } + else if (curLine !== "") { + newResults += curLine + "\n" + linesAdded ++ + } + } + await wwwcache.put(languageSelected, newResults) + return new Response("added " + linesAdded + " out of " + resultsLines.length + " lines for language " + languageSelected + " to Cloudflare KV " + cfScriptRemoved, respInit) + } + else { + var deleteName = getParameterByName('deleteKV') + if (typeof deleteName === "string") { + var deleteNames = [] + if (deleteName === "all") { + deleteNames = langs + } + else + deleteNames = [ deleteName ] + for (var i = 0; i < deleteNames.length; i++) { + await wwwcache.delete(deleteNames[i]) + } + return new Response("deleted " + deleteName + " from Cloudflare KV", respInit) + } + } + return new Response("don't understand your request", respInit) + +} + +addEventListener("fetch", event => { + return event.respondWith(handleRequest()) +}) \ No newline at end of file diff --git a/step-tools/src/main/resources/cloudflare_caching/sendcachedhomepage b/step-tools/src/main/resources/cloudflare_caching/sendcachedhomepage new file mode 100644 index 0000000000..c5593e9c51 --- /dev/null +++ b/step-tools/src/main/resources/cloudflare_caching/sendcachedhomepage @@ -0,0 +1,33 @@ +export default { + async fetch(request, env) { + var ul = request.headers.get("Accept-Language"); + if (typeof ul !== "string") + ul = "en" + const ulToCheck = ul.toLowerCase() + const ul2 = ulToCheck.substring(0,2) + var foundLang = ""; + if ((ulToCheck.length >= 5) && (ul2 === "zh")) { + const ul5 = ulToCheck.substring(2,5) + if ((ul5 === "_tw") || (ul5 === "-tw") || (ul5 === "_hk") || (ul5 === "-hk")) + foundLang = 'zh-tw'; + } + const langs = ["en", "es", "zh", "zh-tw", "aa", "af", "ar", "az", "be", "bg", "bn", "cs", "cy", "de", "el", "et", "fa", "fi", "fr", "ga", "gl", "gu", "hi", "hr", "hu", "id", "in", "is", "it", "iw", "ja", "ka", "ko", "lt", "lv", "mk", "ml", "ms", "mt", "nl", "no", "pl", "pt", "ro", "ru", "ry", "sk", "sl", "sq", "sr", "sv", "sw", "ta", "te", "th", "tr", "uk", "ur", "uz", "vi"]; + var redirectCode = 301 + if ((foundLang === "") && (langs.indexOf(ul2) > -1)) + foundLang = ul2 + if (foundLang !== "") { + var pageForUser = await env.wwwcache.get(foundLang, { cacheTtl: 4800 }) + if ((typeof pageForUser === "string") && (pageForUser.length > 6000)) { + const respInit = { + headers: { + "content-type": "text/html;charset=UTF-8", + "cache-control": "no-cache", + }, + } + return new Response(pageForUser, respInit) + } + else redirectCode = 302 // If redirect is needed, it is a temporary redirect because it is a language supported by STEP but does not have a KV for it. + } + return Response.redirect("https://www.stepbible.org/?q=reference=Gen.1", redirectCode); + }, +}; \ No newline at end of file diff --git a/step-web/pom.xml b/step-web/pom.xml index 96c76932d6..058c6ddda3 100644 --- a/step-web/pom.xml +++ b/step-web/pom.xml @@ -220,7 +220,7 @@ . - libs/jquery.tagcloud.js + js/backbone/models/model_sidebar.js js/backbone/views/view_wordle_stat.js js/backbone/views/view_history.js diff --git a/step-web/src/main/webapp/js/backbone/views/view_wordle_stat.js b/step-web/src/main/webapp/js/backbone/views/view_wordle_stat.js index d945e99acb..60e9780e1c 100644 --- a/step-web/src/main/webapp/js/backbone/views/view_wordle_stat.js +++ b/step-web/src/main/webapp/js/backbone/views/view_wordle_stat.js @@ -345,14 +345,15 @@ var ViewLexiconWordle = Backbone.View.extend({ }); var links = $("a", container); - links.tagcloud({ - size: { - start: statType == "SUBJECT" ? self.minSubjectFont : self.minFont, - end: self.maxFont, - unit: "px" - }, - animate: isNextChapter - }); + // June 21, 2024 - PT, probably no longer use + // links.tagcloud({ + // size: { + // start: statType == "SUBJECT" ? self.minSubjectFont : self.minFont, + // end: self.maxFont, + // unit: "px" + // }, + // animate: isNextChapter + // }); if (statType === 'WORD') { links.hover( diff --git a/step-web/src/main/webapp/js/require_config_dev.js b/step-web/src/main/webapp/js/require_config_dev.js index 9b4019d0f0..d8bd010e77 100644 --- a/step-web/src/main/webapp/js/require_config_dev.js +++ b/step-web/src/main/webapp/js/require_config_dev.js @@ -17,7 +17,7 @@ requirejs.config({ shim : { "menu_extras" : ["js/backbone/views/view_advanced_search"], "sidebar" : ["js/backbone/models/model_sidebar", "js/backbone/views/view_wordle_stat", - "js/backbone/views/view_history", "libs/jquery.tagcloud"], + "js/backbone/views/view_history"], // "libs/jquery.tagcloud"], "search" : ["_search_display_view", "_other_search"], _other_search: ["_search_display_view", "js/backbone/views/view_display_text", "libs/dohighlight-min", "js/passage/step.alternatives"] }