Created
October 15, 2022 05:56
-
-
Save levantAJ/caca1d9edaea1a01e1b71d78213c4ffe to your computer and use it in GitHub Desktop.
Auto Fill - Getting Form Input Values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Method use for logging for debugging | |
function autoFillGettingLog(message) { | |
window.webkit.messageHandlers.autoFillGettingShippingInfoLog.postMessage(message); | |
} | |
autoFillGettingLog("Beginning...."); | |
/// The configuration xpath will be replace before injected to IAB | |
/// format: [{xPath, key, value}] | |
var autoFillGettingConfigurationXPathsString = "checkout-configuration-xpaths"; | |
/// Contains list of timers which used for waiting to be the node to be appeared | |
var autoFillGettingNodeIntervalIDs = {}; // {configuration.key: intervalID} | |
/// Contains all of the input data which will be notified back to IAB every time the event's of `autoFillGettingConfigurationXPathsString` is trigger | |
var autoFillGettingCheckoutInfo = {}; // Store all of the checkout info | |
/// Stores all appearing nodes | |
/// Use this to identify whether all node disappear or not | |
/// [key, configuration] | |
var autoFillGettingAppearingNodes = {}; | |
var autoFillGettingDisappearingNodesIntervalID; | |
function autoFillGettingGetNode(xPath) { | |
var formNode = document.evaluate(xPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; | |
return formNode | |
} | |
function autoFillGettingUpdateValueAndNotify(key, value) { | |
autoFillGettingCheckoutInfo[key] = value; | |
window.webkit.messageHandlers.autoFillGettingShippingInfo.postMessage(autoFillGettingCheckoutInfo); | |
} | |
function autoFillGettingObserveNodeEvents(node, configuration) { | |
var xPath = configuration.xPath; | |
var key = configuration.key; | |
var events = configuration.events; | |
autoFillGettingLog("Observing.... " + key); | |
// Get and notify the current value | |
// Case some merchant already filled the info for the input | |
if (node.value) { | |
autoFillGettingUpdateValueAndNotify(key, node.value); | |
} | |
for (let i = 0; i < events.length; i++) { | |
node.addEventListener(events[i], function(e) { | |
// Update checkout info | |
let newValue = autoFillGettingGetNode(xPath).value; | |
autoFillGettingLog("Value changed: " + newValue); | |
autoFillGettingUpdateValueAndNotify(key, newValue); | |
}); | |
} | |
} | |
/// Checking whether all nodes have found | |
function autoFillGettingIsFoundAllNodes() { | |
var configurationXPaths = JSON.parse(autoFillGettingConfigurationXPathsString); | |
for (let i = 0; i < configurationXPaths.length; i++) { | |
var configuration = configurationXPaths[i] | |
if (!autoFillGettingAppearingNodes[configuration.key]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/// Observe when all nodes get disappeared | |
function autoFillGettingObserveAllNodesDisappeared() { | |
autoFillGettingLog("Check all nodes disappearing..."); | |
var configurationXPaths = JSON.parse(autoFillGettingConfigurationXPathsString); | |
for (let i = 0; i < configurationXPaths.length; i++) { | |
var configuration = configurationXPaths[i] | |
var node = autoFillGettingGetNode(configuration.xPath); | |
if (!node) { | |
autoFillGettingLog("Node removed: " + configuration.key); | |
delete autoFillGettingAppearingNodes[configuration.key]; | |
} | |
} | |
if (Object.keys(autoFillGettingAppearingNodes).length == 0) { | |
clearInterval(autoFillGettingDisappearingNodesIntervalID); | |
window.webkit.messageHandlers.autoFillGettingShippingSubmit.postMessage(autoFillGettingCheckoutInfo); | |
} | |
} | |
/// Observe a node when it's disappeared | |
/// Once all observing nodes are disappeared we trigger to ask use to save shipping info | |
function autoFillGettingObserveNodeDisappeared(node, configuration) { | |
var key = configuration.key; | |
if (!autoFillGettingAppearingNodes[key]) { | |
autoFillGettingAppearingNodes[key] = configuration; | |
var hasFoundAllNodes = autoFillGettingIsFoundAllNodes(); | |
autoFillGettingLog("Found all nodes: " + hasFoundAllNodes.toString()); | |
if (hasFoundAllNodes && !autoFillGettingDisappearingNodesIntervalID) { | |
autoFillGettingDisappearingNodesIntervalID = setInterval(function(){ | |
autoFillGettingObserveAllNodesDisappeared(); | |
}, 3000); | |
} | |
} | |
} | |
/// configuration = {xPath, key, events} | |
function autoFillGettingObserveNodeAppeared(configuration) { | |
var xPath = configuration.xPath; | |
var key = configuration.key; | |
var intervalID = autoFillGettingNodeIntervalIDs[key]; | |
// Get node by xPath | |
var node = autoFillGettingGetNode(xPath); | |
if (node) { | |
// When all nodes are disappear | |
// Trigger to show pop-up to save shipping info | |
autoFillGettingObserveNodeDisappeared(node, configuration); | |
// Remove interval when node is appeared | |
if (intervalID) { | |
clearInterval(intervalID); | |
} | |
autoFillGettingLog("Found: " + key); | |
autoFillGettingObserveNodeEvents(node, configuration); | |
} else { | |
autoFillGettingLog("Looking for: " + key); | |
// Start a timer if needed | |
if (!intervalID) { | |
var newInterval = setInterval(() => autoFillGettingObserveNodeAppeared(configuration), 3000); | |
autoFillGettingNodeIntervalIDs[key] = newInterval; | |
autoFillGettingLog("Start timer for: " + key); | |
} | |
} | |
} | |
function autoFillGettingObserve() { | |
var configurationXPaths = JSON.parse(autoFillGettingConfigurationXPathsString); | |
window.webkit.messageHandlers.autoFillGettingShippingInfo.postMessage(configurationXPaths); | |
for (let i = 0; i < configurationXPaths.length; i++) { | |
autoFillGettingObserveNodeAppeared(configurationXPaths[i]) | |
} | |
} | |
autoFillGettingObserve(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment