From 8f1ff6db2e456e7291441b4f1c3477f42e62d614 Mon Sep 17 00:00:00 2001 From: Phillweston <2436559745@qq.com> Date: Thu, 16 May 2024 18:41:11 +0800 Subject: [PATCH 1/6] Optimize the handleAction code --- js/twitter-action.js | 116 +++++++++++++------------------------------ 1 file changed, 34 insertions(+), 82 deletions(-) diff --git a/js/twitter-action.js b/js/twitter-action.js index a41f843..35c7a78 100644 --- a/js/twitter-action.js +++ b/js/twitter-action.js @@ -174,39 +174,28 @@ function displayInfo(action, message, type) { async function handleAction(action) { try { + const actionConfig = { + 'follow-us': { configKey: 'userName', actionType: 'follow-us', info: 'follow-info', progress: 'follow-progress', section: 'follow-section' }, + 'retweet-2': { configKey: 'tweetId2', actionType: 'retweet', info: 'retweet-2-info', progress: 'retweet-2-progress', section: 'retweet-section-2' }, + 'retweet': { configKey: 'tweetId', actionType: 'retweet', info: 'retweet-info', progress: 'retweet-progress', section: 'retweet-section' }, + 'like': { configKey: 'tweetId', actionType: 'like', info: 'like-info', progress: 'like-progress', section: 'like-section' }, + }; + // Fetch the tweetId from the configuration file const configResponse = await fetch('../contract-config.json'); if (!configResponse.ok) { throw new Error(`Failed to load configuration file. Status: ${configResponse.status}`); } const jsonConfig = await configResponse.json(); - let queryParams = ''; - let actionType; - // Determine which identifier to use based on the action - if (action === 'follow-us') { - actionType = 'follow-us'; - const userName = jsonConfig.userName; // Assuming userName is stored in the config - if (!userName) { - throw new Error("Required configuration value 'userName' is missing."); - } - queryParams += `userName=${encodeURIComponent(userName)}`; - } else if (action === 'retweet-2') { - actionType = 'retweet'; - const tweetId2 = jsonConfig.tweetId2; - if (!tweetId2) { - throw new Error("Required configuration value 'tweetId2' is missing."); - } - queryParams += `tweetId=${encodeURIComponent(tweetId2)}`; - } else { - actionType = action; - const tweetId = jsonConfig.tweetId; - if (!tweetId) { - throw new Error("Required configuration value 'tweetId' is missing."); - } - queryParams += `tweetId=${encodeURIComponent(tweetId)}`; + + const configKey = actionConfig[action].configKey; + const configValue = jsonConfig[configKey]; + if (!configValue) { + throw new Error(`Required configuration value '${configKey}' is missing.`); } + const queryParams = `${configKey}=${encodeURIComponent(configValue)}`; + const actionType = actionConfig[action].actionType; - // If tweetId is fetched successfully, perform the action const actionResponse = await fetch(`${authWebAddress}/${actionType}?${queryParams}`, { method: 'GET', headers: { @@ -225,64 +214,27 @@ async function handleAction(action) { const response = await actionResponse.json(); console.log(`${action} action response:`, response); - displayInfo(action, `${action} action performed successfully`, 'info'); - - // Set the corresponding class to be disabled after success - switch (action) { - case 'retweet': - if (response.error && response.code == 10017) { - console.log('You have already retweeted this tweet.'); - displayInfo(action, 'You have already retweeted this tweet.', 'error'); - } else { - throw new Error('Error retweeting the tweet:', response.error); - } - hideElement('retweet-info'); - animateProgress('retweet-progress'); - setTimeout(() => { - document.getElementById('retweet-section').classList.add('disabled'); - }, 2000); - break; - case 'like': - if (response.error && response.code == 10018) { - console.log('You have already liked this tweet.'); - displayInfo(action, 'You have already liked this tweet.', 'error'); - } else { - throw new Error('Error liking the tweet:', response.error); - } - hideElement('like-info'); - animateProgress('like-progress'); - setTimeout(() => { - document.getElementById('like-section').classList.add('disabled'); - }, 2000); - break; - case 'retweet-2': - if (response.error && response.code == 10017) { - console.log('You have already retweeted this tweet.'); - displayInfo(action, 'You have already retweeted this tweet.', 'error'); - } else { - throw new Error('Error retweeting the tweet:', response.error); - } - hideElement('retweet-2-info'); - animateProgress('retweet-2-progress'); - setTimeout(() => { - document.getElementById('retweet-section-2').classList.add('disabled'); - }, 2000); - break; - case 'follow-us': - // Note: Since Twitter disallows checking if a user is already followed, the error is thrown if found in the response - if (response.error) { - throw new Error('Error following the user:', response.error); - } - hideElement('follow-info'); - animateProgress('follow-progress'); - setTimeout(() => { - document.getElementById('follow-section').classList.add('disabled'); - }, 2000); - break; - default: - break; + + const infoElementId = actionConfig[action].info; + const progressElementId = actionConfig[action].progress; + const sectionElementId = actionConfig[action].section; + + if (response.error && (response.code == 10017 || response.code == 10018)) { + console.log(`You have already ${actionType} this tweet.`); + displayInfo(action, `You have already ${actionType} this tweet.`, 'error'); + } else if (!response.error && response.code == 0) { + console.log(`${actionType.charAt(0).toUpperCase() + actionType.slice(1)} successful.`); + displayInfo(action, `${actionType.charAt(0).toUpperCase() + actionType.slice(1)} successful.`, 'info'); + } else { + throw new Error(`Error ${actionType} the tweet:`, response.error); } - // Wait for 3 seconds + + hideElement(infoElementId); + animateProgress(progressElementId); + setTimeout(() => { + document.getElementById(sectionElementId).classList.add('disabled'); + }, 2000); + setTimeout(() => { checkAllActionsDisabled(); }, 3000); From 6685c7f1103102a717cdf5921842a0745ee2a424 Mon Sep 17 00:00:00 2001 From: Phillweston <2436559745@qq.com> Date: Thu, 16 May 2024 19:12:54 +0800 Subject: [PATCH 2/6] Add success message for promotion code input row --- index.html | 5 ++--- js/claim-airdrop.js | 4 ++-- js/contact-form.js | 17 ++++++++++++----- js/get-promotion-code.js | 14 ++++++++++---- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index 6dd4bc1..e5d3dcb 100644 --- a/index.html +++ b/index.html @@ -408,8 +408,7 @@