Skip to content

Instantly share code, notes, and snippets.

@thedward
Last active July 11, 2024 19:39
Show Gist options
  • Save thedward/f22e0f6294c93162e2f3d6db41da65e5 to your computer and use it in GitHub Desktop.
Save thedward/f22e0f6294c93162e2f3d6db41da65e5 to your computer and use it in GitHub Desktop.
Serialize Airtable Base Schema to JSON and Copy it to the Clipboard
(function () {
//-------------------------------------------------------------------------------------------------------------------------
// To use this script, go the the API page for your Airtable Base (something like https://airtable.com/<base_id>/api/docs).
// Then, open the javascript console (F12) on Chrome. Then copy this script and paste it into the console (and hit Enter).
// The schema serialized as JSON should now be in your clipboard.
//
// Alternatively, you can use the bookmarklet below.
//-------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// copyToClipboard implementation via:
// <https://gist.github.com/Chalarangelo/4ff1e8c0ec03d9294628efbae49216db/#file-copytoclipboard-js>
// ------------------------------------------------------------------------------------------------
const copyToClipboard = str => {
const el = document.createElement('textarea'); // Create a <textarea> element
el.value = str; // Set its value to the string that you want copied
el.setAttribute('readonly', ''); // Make it readonly to be tamper-proof
el.style.position = 'absolute';
el.style.left = '-9999px'; // Move outside the screen to make it invisible
document.body.appendChild(el); // Append the <textarea> element to the HTML document
const selected =
document.getSelection().rangeCount > 0 // Check if there is any content selected previously
? document.getSelection().getRangeAt(0) // Store selection if found
: false; // Mark as false to know no selection existed before
el.select(); // Select the <textarea> content
document.execCommand('copy'); // Copy - only works as a result of a user action (e.g. click events)
document.body.removeChild(el); // Remove the <textarea> element
if (selected) { // If a selection existed before copying
document.getSelection().removeAllRanges(); // Unselect everything on the HTML document
document.getSelection().addRange(selected); // Restore the original selection
}
};
// ------------------------------------------------------------------------------------------------------------------------
// Advanced documentation for JSON.stringify, and the 'replacer' function argument:
// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter>
// ------------------------------------------------------------------------------------------------------------------------
const getSchemaReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if ( key === 'sampleRows' ) {
return undefined;
} else if (key === 'foreignTable') {
let ftobj = { "id" : value.id , "name" : value.name };
return ftobj;
} else if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
let serializedSchema = JSON.stringify(window.application,getSchemaReplacer(),2);
let schema = JSON.parse(serializedSchema);
let info = `${schema.name} (${schema.id})`;
copyToClipboard(serializedSchema);
alert(`Copied to Clipboard:\nJSON for ${info}`);
console.log(serializedSchema);
}
)();

Bookmarklet

Here is the same script in bookmarklet form, ready to be added to your bookmarks:

javascript:(function()%7B(function%20()%20%7B%2F%2F%20copyToClipboard%20via%20%3Chttps%3A%2F%2Fgist.github.com%2FChalarangelo%2F4ff1e8c0ec03d9294628efbae49216db%2F%23file-copytoclipboard-js%3Econst%20copyToClipboard%20%3D%20str%20%3D%3E%20%7Bconst%20el%20%3D%20document.createElement('textarea')%3B%20%20%2F%2F%20Create%20a%20%3Ctextarea%3E%20elementel.value%20%3D%20str%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Set%20its%20value%20to%20the%20string%20that%20you%20want%20copiedel.setAttribute('readonly'%2C%20'')%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Make%20it%20readonly%20to%20be%20tamper-proofel.style.position%20%3D%20'absolute'%3Bel.style.left%20%3D%20'-9999px'%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Move%20outside%20the%20screen%20to%20make%20it%20invisibledocument.body.appendChild(el)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Append%20the%20%3Ctextarea%3E%20element%20to%20the%20HTML%20documentconst%20selected%20%3Ddocument.getSelection().rangeCount%20%3E%200%20%20%20%20%20%20%20%20%2F%2F%20Check%20if%20there%20is%20any%20content%20selected%20previously%3F%20document.getSelection().getRangeAt(0)%20%20%20%20%20%2F%2F%20Store%20selection%20if%20found%3A%20false%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Mark%20as%20false%20to%20know%20no%20selection%20existed%20beforeel.select()%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Select%20the%20%3Ctextarea%3E%20contentdocument.execCommand('copy')%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Copy%20-%20only%20works%20as%20a%20result%20of%20a%20user%20action%20(e.g.%20click%20events)document.body.removeChild(el)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Remove%20the%20%3Ctextarea%3E%20elementif%20(selected)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20a%20selection%20existed%20before%20copyingdocument.getSelection().removeAllRanges()%3B%20%20%20%20%2F%2F%20Unselect%20everything%20on%20the%20HTML%20documentdocument.getSelection().addRange(selected)%3B%20%20%20%2F%2F%20Restore%20the%20original%20selection%7D%7D%3B%2F%2F%20https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FGlobal_Objects%2FJSON%2Fstringify%23The_replacer_parameterconst%20getSchemaReplacer%20%3D%20()%20%3D%3E%20%7Bconst%20seen%20%3D%20new%20WeakSet()%3Breturn%20(key%2C%20value)%20%3D%3E%20%7Bif%20(%20key%20%3D%3D%3D%20'sampleRows'%20)%20%7Breturn%20undefined%3B%7D%20else%20if%20(key%20%3D%3D%3D%20'foreignTable')%20%7Blet%20ftobj%20%3D%20%7B%20%22id%22%20%3A%20value.id%20%2C%20%22name%22%20%3A%20value.name%20%7D%3Breturn%20ftobj%3B%7D%20else%20if%20(typeof%20value%20%3D%3D%3D%20%22object%22%20%26%26%20value%20!%3D%3D%20null)%20%7Bif%20(seen.has(value))%20%7Breturn%3B%7Dseen.add(value)%3B%7Dreturn%20value%3B%7D%3B%7D%3Blet%20serializedSchema%20%3D%20JSON.stringify(window.application%2CgetSchemaReplacer()%2C2)%3Blet%20schema%20%3D%20JSON.parse(serializedSchema)%3Blet%20info%20%3D%20%60%24%7Bschema.name%7D%20(%24%7Bschema.id%7D)%60%3BcopyToClipboard(serializedSchema)%3Balert(%60Copied%20to%20Clipboard%3A%5CnJSON%20for%20%24%7Binfo%7D%60)%3Bconsole.log(serializedSchema)%3B%7D)()%7D)()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment