From 4848377d9dcdc8a209c1ea63a58c4c20dad58460 Mon Sep 17 00:00:00 2001 From: "Chris K.Y. FUNG" <8746768+chriskyfung@users.noreply.github.com> Date: Wed, 13 Oct 2021 19:06:21 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20Bugs=20in=20Update=20Datab?= =?UTF-8?q?ase=20Records?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qwiklabs-explorer.user.js | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/qwiklabs-explorer.user.js b/qwiklabs-explorer.user.js index 3715fc6..2dba0cb 100644 --- a/qwiklabs-explorer.user.js +++ b/qwiklabs-explorer.user.js @@ -2,7 +2,7 @@ // @name Qwiklabs Completed Labs Tracker // @name:ja Qwiklabsラボ完成トラッカー // @namespace https://chriskyfung.github.io/ -// @version 2.0.6 +// @version 2.0.7 // @author chriskyfung // @description Label completed quests and labs on the Catalog page(s) and Lab pages on Qwiklabs (https://www.qwiklabs.com/catalog) // @homepage https://chriskyfung.github.io/blog/qwiklabs/Userscript-for-Labelling-Completed-Qwiklabs @@ -706,8 +706,8 @@ * @return {Object} */ async function updateRecordById(table, id, obj) { - obj.id = id; - const updated = await qdb.table(table).update(id, obj); + obj.id = parseInt(id); + const updated = await qdb.table(table).update(obj.id, obj); if (updated) { console.log(`Updated ${JSON.stringify(obj)} to ${table}`); } @@ -758,6 +758,15 @@ console.log('Batch updated - finished\nPress F5 to reload the page or wait 10 seconds for automatically refresh!'); } + /** + * Standardize the string to be stored in the database + * @param {string} title - A lab/quest title + * @return {string} + */ + function formatTitle(title) { + return title.replace(/:\S/g, ': ').replace(/\s\s+/g, ' ').trim(); + } + /** * Status Query Methods. * @param {number} id @@ -782,14 +791,10 @@ * @return {Object|null} A lab record or null if not found. */ async function getLabByTitle(title) { - let formattedTitle; - // Format the text if the lab title contains a colon - if (title.includes(': ')) { - formattedTitle = title.replace(': ', ':'); - } + const formattedTitle = formatTitle(title); // const record = await tmpdb.labs.filter((record) => { - return record.name == title || record.name == formattedTitle; + return record.name == formattedTitle; })[0]; try { return record || {status: null}; @@ -822,8 +827,9 @@ * @return {Object|null} A quest record or null if not found. */ async function getQuestByTitle(title) { + const formattedTitle = formatTitle(title); const record = await tmpdb.quests.filter((record) => { - return record.name == title; + return record.name == formattedTitle; })[0]; try { return record || {status: null}; @@ -953,14 +959,14 @@ // Annotate as Completed setBackgroundColor(el, 'green'); appendIcon(el, 'check', {elementType: 'span'}); - updateRecordById('labs', id, {'name': title}); + updateRecordById('labs', id, {'name': formatTitle(title)}); break; case null: // Annotate as Unregistered; console.log(`[ status = null ] for lab ${id}: ${el.innerText}`); setBackgroundColor(el, 'yellow'); appendIcon(el, 'new', {elementType: 'span'}); - createRecord('labs', id, {'name': title, 'status': ''}); + createRecord('labs', id, {'name': formatTitle(title), 'status': ''}); break; }; } @@ -977,14 +983,14 @@ // Annotate as Completed setBackgroundColor(el, 'green'); appendIcon(el, 'check', {elementType: 'span'}); - updateRecordById('quests', id, {'name': title}); + updateRecordById('quests', id, {'name': formatTitle(title)}); break; case null: // Annotate as Unregistered; console.log(`[ status = null ] for lab ${id}: ${el.innerText}`); setBackgroundColor(el, 'yellow'); appendIcon(el, 'new', {elementType: 'span'}); - createRecord('quests', id, {'name': title, 'status': ''}); + createRecord('quests', id, {'name': formatTitle(title), 'status': ''}); break; }; }