Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xlplugins/5bea31edaafc67032c4ee3079764ce90 to your computer and use it in GitHub Desktop.
Save xlplugins/5bea31edaafc67032c4ee3079764ce90 to your computer and use it in GitHub Desktop.
re include google pay js if google.payment is deleted by some js
add_action( 'wfacp_meta_added', function () {
?>
<script>
(function watchAllScriptLoads() {
function loadScript(src, callback) {
var script = document.createElement('script');
script.src = src;
script.onload = callback;
script.onerror = function () {
console.error('Failed to load script:', src);
};
document.head.appendChild(script);
}
// Usage
function attachLoadEvents(script) {
if (!script.src || script._loadHandled) return;
script._loadHandled = true;
script.addEventListener('load', () => {
if (typeof google == "undefined" || !google.hasOwnProperty('payments')) {
loadScript('https://pay.google.com/gp/p/js/pay.js', function () {
console.log('[Script Loaded]:', 'https://pay.google.com/gp/p/js/pay.js');
});
}
});
script.addEventListener('error', () => {
console.warn('[Script Failed]:', script.src);
});
}
// Attach to existing script tags
document.querySelectorAll('script[src]').forEach(attachLoadEvents);
// Watch for new script tags added dynamically
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
mutation.addedNodes.forEach((node) => {
if (node.tagName === 'SCRIPT') {
attachLoadEvents(node);
}
});
}
});
observer.observe(document.documentElement, {
childList: true,
subtree: true,
});
})();
</script>
<?php
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment