Creating a full Tampermonkey user script that superimposes an interface onto a webpage and then allows a user to copy/paste text into it to export to CSV format requires a fair bit of JavaScript. Please note that installing and using custom scripts can pose a security risk if the scripts contain malicious code, so always ensure you trust the source and understand what the script is doing before using it.
Below is a simplified example of a Tampermonkey user script that adds a basic interface for collecting text snippets and exporting them to CSV. You'll need to have Tampermonkey installed in your browser and then create a new script with this code:
// ==UserScript==
// @name Superimposed CSV Exporter Interface
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Allows you to copy/paste text and data to export as CSV
// @author YourName
// @match *://*/*
// @grant GM_addStyle
// @grant GM_download
// ==/UserScript==
(function() {
'use strict';
// Function to encode CSV
const toCSV = (data) => {
return data
.map(row => row.map(String)
.map(v => v.replaceAll('"', '""'))
.map(v => `"${v}"`)
.join(','))
.join('\n');
};
// Initialize data storage
let dataStorage = [];
// Creating and styling the container div for the interface
GM_addStyle(`
#tmCSVInterface {
position: fixed;
top: 10px;
right: 10px;
z-index: 9999999;
background-color: white;
border: 1px solid black;
padding: 10px;
width: 300px;
}
#tmCSVInterface textarea {
width: 100%;
height: 100px;
}
`);
// Adding the HTML for the interface
let container = document.createElement('div');
container.id = "tmCSVInterface";
container.innerHTML = `
<textarea id="tmCSVTextArea" placeholder="Paste text here"></textarea>
<button id="tmCSVAddBtn">Add to CSV</button>
<button id="tmCSVExportBtn">Export to CSV</button>
`;
document.body.appendChild(container);
// Add entry to CSV array
document.querySelector('#tmCSVAddBtn').addEventListener('click', () => {
let textValue = document.querySelector('#tmCSVTextArea').value.split('\n').map(line => line.split(','));
dataStorage.push(...textValue);
document.querySelector('#tmCSVTextArea').value = ''; // clear text area after adding
});
// Export to CSV file
document.querySelector('#tmCSVExportBtn').addEventListener('click', () => {
let csvData = toCSV(dataStorage);
GM_download("data:text/csv;charset=utf-8," + encodeURIComponent(csvData), 'export.csv');
});
})();
This script will add a floating interface to the top right of every webpage where you can paste text. Each line of text represents a row, and you can use commas to separate values into different CSV columns. Click "Add to CSV" to store the values, and then "Export to CSV" to download the data as a export.csv file.
Remember, this script is quite basic and for demonstration purposes only. It doesn't include error checking, data validation, or escaping other than simple double-quote handling (necessary for CSV formatting). Depending on your specific use case, you might want to enhance and extend this basic functionality or add a more sophisticated UI/UX element. Always thoroughly test and consider security implications when creating and using scripts like this.