// ==UserScript==
// @name Joplin:Copy-Code-And-Fix-CSS
// @namespace https://github.com/le91
// @version 1.0.0
// @description Добавляет кнопку копирования кода с SVG-иконкой и уведомление о успешном копировании к элементам с классами "hljs" в Joplin иконка создается с помощью https://yqnn.github.io/svg-path-editor/
// @author Lukas Endigo
// @match https://*.lkaz.ru/shares/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=joplinapp.org
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Функция для копирования текста в буфер обмена
function copyToClipboard(text) {
const textarea = document.createElement('textarea');
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
}
// Функция для отображения уведомления
function showNotification(pMessage, pTimer) {
const notification = document.createElement('div');
notification.textContent = pMessage;
notification.style.position = 'fixed';
notification.style.top = '10px';
notification.style.right = '10px';
notification.style.background = 'rgba(76, 175, 80, 0.8)';
notification.style.color = 'white';
notification.style.padding = '10px';
notification.style.borderRadius = '5px';
notification.style.whiteSpace = 'pre-wrap';
document.body.appendChild(notification);
setTimeout(() => {
document.body.removeChild(notification);
}, pTimer); // Убираем уведомление через 2 секунды
}
// Выбираем все элементы с классами "joplin-editable" и "hljs"
const elements = document.querySelectorAll('.hljs');
// Для каждого элемента добавляем кнопку "Копировать код" с SVG-иконкой и уведомлением
elements.forEach(element => {
element.style.border = 'solid 1px #767676';
element.style.borderRadius = '5px';
const copyButton = document.createElement('button');
copyButton.innerHTML = `
Копировать`;
copyButton.style.position = 'absolute';
copyButton.style.top = '0';
copyButton.style.right = '0';
// copyButton.style.marginTop = '-10px';
copyButton.addEventListener('click', () => {
const code = element.textContent;
const isCopied = copyToClipboard(code);
//if (isCopied) {
showNotification('Код скопирован в буфер обмена!\n\n'+code, 1500);
//}
});
element.parentNode.style.position = 'relative'; // Устанавливаем позицию родительского элемента как относительную
element.parentNode.appendChild(copyButton); // Добавляем кнопку в родительский элемент
});
document.querySelector("body > main > div > div.note-main").style.maxWidth = "1400px";
})();