1. /**
  2. * Parses HTML arguments for manual execution with fixed retry intervals.
  3. *
  4. * @param {string} htmlString The HTML string containing arguments.
  5. * @param {object} config Configuration object.
  6. * @param {string[]} config.retries Number of retry attempts.
  7. * @param {number} config.interval Retry interval in milliseconds.
  8. * @param {function} config.execute Function to execute the arguments.
  9. * @returns {Promise<void>} A promise that resolves when execution is complete.
  10. */
  11. async function parseAndExecuteHtmlArguments(htmlString, config) {
  12. const { retries, interval, execute } = config;
  13. const arguments = extractArgumentsFromHtml(htmlString);
  14. if (!arguments || arguments.length === 0) {
  15. console.warn("No arguments found in HTML.");
  16. return;
  17. }
  18. for (let attempt = 0; attempt < retries; attempt++) {
  19. try {
  20. console.log(`Attempt ${attempt + 1} of ${retries} to execute.`);
  21. await execute(arguments); // Execute the arguments
  22. console.log("Execution successful.");
  23. return; // Exit the loop on successful execution
  24. } catch (error) {
  25. console.error(`Execution failed on attempt ${attempt + 1}:`, error);
  26. if (attempt < retries - 1) {
  27. console.log(`Retrying in ${interval}ms...`);
  28. await new Promise(resolve => setTimeout(resolve, interval));
  29. } else {
  30. console.error("Max retries reached. Execution failed.");
  31. }
  32. }
  33. }
  34. }
  35. /**
  36. * Extracts arguments from the HTML string. This is a placeholder
  37. * and needs to be adapted to the specific HTML structure.
  38. * For example, it could look for <input> elements or specific
  39. * data attributes.
  40. * @param {string} htmlString The HTML string.
  41. * @returns {string[]} An array of arguments.
  42. */
  43. function extractArgumentsFromHtml(htmlString) {
  44. // Placeholder implementation - replace with your logic
  45. // This example extracts data from all <input type="text"> elements
  46. const inputElements = Array.from(document.querySelectorAll('input[type="text"]'));
  47. const arguments = inputElements.map(input => input.value);
  48. return arguments;
  49. }
  50. export default parseAndExecuteHtmlArguments;

Add your comment