Skip to content

Instantly share code, notes, and snippets.

@gavinmh
Last active January 7, 2025 18:26
Show Gist options
  • Select an option

  • Save gavinmh/6f61a44a05f50b8b4d332443926b855c to your computer and use it in GitHub Desktop.

Select an option

Save gavinmh/6f61a44a05f50b8b4d332443926b855c to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Copy Salesforce OmniStudio Form Inputs
// @namespace http://tampermonkey.net/
// @version 1.2
// @description Copy values of all form inputs on the page, including Salesforce OmniStudio forms.
// @author YourName
// @match *://*/*
// @grant GM_setClipboard
// ==/UserScript==
(function () {
'use strict';
// Create a button to trigger the copy action
const button = document.createElement('button');
button.textContent = 'Sync to Link Dashboard';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.zIndex = '9999';
button.style.backgroundColor = '#007bff';
button.style.color = '#fff';
button.style.border = 'none';
button.style.padding = '10px 15px';
button.style.cursor = 'pointer';
button.style.borderRadius = '5px';
button.style.boxShadow = '0px 2px 5px rgba(0,0,0,0.3)';
button.style.fontSize = '14px';
// Add button to the document body
document.body.appendChild(button);
// Function to traverse Shadow DOMs and collect inputs
function traverseShadowDOM(root, formData = {}) {
const ignoredFieldNames = ['tl', 'sl', 'gtrans', 'query', 'vote', 'input0-217', 'input1-219', 'input2-221', 'input3-223'];
if (!root) return formData;
// Traverse the current root for inputs
root.querySelectorAll('input, textarea, select').forEach((field) => {
const name = field.name || field.id || `unnamed_${Math.random().toString(36).substr(2, 5)}`;
if (field.type !== 'password' && !ignoredFieldNames.includes(name)) {
formData[name] = field.value;
}
});
// Traverse shadow roots
root.querySelectorAll('*').forEach((node) => {
if (node.shadowRoot) {
traverseShadowDOM(node.shadowRoot, formData);
}
});
return formData;
}
// Function to collect and copy form inputs
function copyFormInputs() {
const formData = {};
// Collect inputs from the main DOM
traverseShadowDOM(document, formData);
// Convert formData to a pretty JSON string
const formDataString = JSON.stringify(formData, null, 2);
// Copy to clipboard using Tampermonkey's API
GM_setClipboard(formDataString);
// Notify the user
alert('Data (pretend) synced to Link Dashboard:\n' + formDataString);
}
// Monitor the DOM for dynamically added inputs (optional)
const observer = new MutationObserver(() => {
// Optionally log changes for debugging
console.log('DOM changed. Dynamic elements may have been added.');
});
// Start observing the entire document for changes
observer.observe(document.body, {
childList: true,
subtree: true,
});
// Add click event to the button
button.addEventListener('click', copyFormInputs);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment