Created
March 31, 2025 05:56
-
-
Save raksheetbhat/335d819297f5e6ece9c411338488f3e6 to your computer and use it in GitHub Desktop.
Chrome extension to autofill form
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
function setFormValues(inputTxt) { | |
// var inputTxt = "CREATE CATALOG mysql_catalog WITH (connector = 'mysql',connection-url = 'jdbc:mysql://your-mysql-host:3306/database_name',connection-user = 'your_username',connection-password = 'your_password');"; | |
var regex = /([a-zA-Z-]+)\s*=\s*'([^']*)'/g; | |
for (const match of inputTxt.matchAll(regex)) { | |
console.log(`${match[1]} ${match[2]}`); | |
var key = match[1]; | |
var val = match[2]; | |
if (document.getElementById(key)) { | |
document.getElementById(key).value = val; | |
} | |
} | |
} | |
async function getCurrentTab() { | |
let queryOptions = { active: true, lastFocusedWindow: true }; | |
// `tab` will either be a `tabs.Tab` instance or `undefined`. | |
let [tab] = await chrome.tabs.query(queryOptions); | |
return tab; | |
} | |
function ex(tabId, data) { | |
chrome.scripting.executeScript({ | |
target: { tabId: tabId }, | |
func: setFormValues, | |
args: [data] | |
}) | |
.then(() => console.log("injected a function")); | |
} | |
document.addEventListener('DOMContentLoaded', function () { | |
document.getElementById('submit').addEventListener('click', function () { | |
var val = document.getElementById('trino-command').value; | |
getCurrentTab() | |
.then(data => ex(data.id, val)); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment