Skip to content

Instantly share code, notes, and snippets.

@goeko
Last active November 3, 2024 20:49
Show Gist options
  • Save goeko/e28263f8a6c1577fa9b4dc5235858748 to your computer and use it in GitHub Desktop.
Save goeko/e28263f8a6c1577fa9b4dc5235858748 to your computer and use it in GitHub Desktop.
Bookmarklet: MultiCopy Text On Page
javascript:(function() {
var storageKey = 'text_storage'; /* Schlüssel für Textspeicher */
var currentIndex = -1; /* Index initial auf -1 setzen, um den gesamten Inhalt anzuzeigen */
function loadStorage() {
var storedData = localStorage.getItem(storageKey);
return storedData ? storedData.split('||') : []; /* Lädt gespeicherte Daten beim Start */
}
var storage = loadStorage(); /* Speicher initialisieren */
var modal; /* Modales Fenster für die Textanzeige */
var showButton; /* Button zum Einblenden des Modals */
function addStyles() {
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = `
#bookmarkletModal, #showButton {
position: fixed;
bottom: 10px;
right: 10px;
width: 300px;
max-height: 80vh;
background: white;
border: 1px solid black;
padding: 5px;
display: none;
flex-direction: column;
box-sizing: border-box;
overflow: hidden;
z-index: 10000;
}
#textareaCopy {
width: 100%;
min-height: 50px;
max-height: calc(80vh - 100px);
box-sizing: border-box;
overflow-y: auto;
margin-bottom: 10px;
}
#bookmarkletModal div {
display: flex;
justify-content: space-between;
width: 100%;
}
#bookmarkletModal button, #showButton {
flex-grow: 1;
margin: 2px;
}
#showButton {
height: 40px;
display: block;
}
`;
document.head.appendChild(style);
}
function createModal() {
modal = document.createElement('div');
modal.id = 'bookmarkletModal';
var textArea = document.createElement('textarea');
textArea.id = 'textareaCopy';
modal.appendChild(textArea);
var navigationContainer = document.createElement('div');
modal.appendChild(navigationContainer);
var prevButton = document.createElement('button');
prevButton.innerText = '<< prev';
prevButton.onclick = function() {
currentIndex = (currentIndex - 1 + storage.length) % storage.length;
textArea.value = storage[currentIndex];
};
navigationContainer.appendChild(prevButton);
var nextButton = document.createElement('button');
nextButton.innerText = 'next >>';
nextButton.onclick = function() {
currentIndex = (currentIndex + 1) % storage.length;
textArea.value = storage[currentIndex];
};
navigationContainer.appendChild(nextButton);
var buttonContainer = document.createElement('div');
modal.appendChild(buttonContainer);
var hideButton = document.createElement('button');
hideButton.innerText = 'Hide';
hideButton.onclick = function() {
modal.style.display = 'none';
showButton.style.display = 'block';
};
buttonContainer.appendChild(hideButton);
var saveAndCopyButton = document.createElement('button');
saveAndCopyButton.innerText = 'Save and Copy';
saveAndCopyButton.onclick = function() {
localStorage.setItem(storageKey, storage.join('||'));
navigator.clipboard.writeText(storage.join('\n\n')).then(function() {
alert('Alle Daten gespeichert und kopiert!');
}, function() {
alert('Fehler beim Kopieren!');
});
currentIndex = -1; /* Index zurücksetzen, um den gesamten Inhalt anzuzeigen */
textArea.value = storage.join('\n\n'); /* Gesamten Inhalt anzeigen */
};
buttonContainer.appendChild(saveAndCopyButton);
var clearButton = document.createElement('button');
clearButton.innerText = 'Leeren';
clearButton.onclick = function() {
storage = [];
localStorage.removeItem(storageKey);
textArea.value = '';
currentIndex = -1; /* Index zurücksetzen */
alert('Zwischenspeicher geleert!');
};
buttonContainer.appendChild(clearButton);
document.body.appendChild(modal);
showButton = document.createElement('button');
showButton.id = 'showButton';
showButton.innerText = 'Show';
showButton.onclick = function() {
modal.style.display = 'block';
showButton.style.display = 'none';
textArea.value = storage.join('\n\n'); /* Gesamten Inhalt initial anzeigen */
};
document.body.appendChild(showButton);
showButton.style.display = 'none';
}
function updateStorage(selectedText) {
if (!storage.includes(selectedText)) {
storage.push(selectedText);
localStorage.setItem(storageKey, storage.join('||'));
}
}
function init() {
console.log('Bookmarklet initialisiert.');
addStyles();
createModal();
document.addEventListener('mouseup', function(e) {
if (!e.target.matches('#textareaCopy')) { /* Events in der Textarea ignorieren */
var selectedText = window.getSelection().toString().trim();
if (selectedText.length > 0) {
updateStorage(selectedText);
}
}
var textarea = document.querySelector('#textareaCopy');
textarea.value = storage.join('\n\n'); /* Gesamten Inhalt nach jeder Auswahl anzeigen */
modal.style.display = 'block';
showButton.style.display = 'none';
});
}
init(); /* Bookmarklet bei Start initialisieren */
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment