Взглянув на SVG, вы можете увидеть, что текст карты наведения находится из элемента <title>
DOM каждой соответствующей фигуры группы. Вы можете сказать, отредактировав DOM и изменив одно из названий: вы увидите новый текст при наведении курсора на фигуру.
Итак, нам просто нужно взять текст оттуда и отправить его в буфер обмена .
РЕДАКТИРОВАТЬ: Теперь это должно быть намного проще для запуска. Просто нужно подождать, пока элемент g.graph
SVG будет загружен на страницу, а не каждый раз, когда он рендерится.
(function addListener() {
// This time, add the listener to the graph itself
document.querySelector('.graph').addEventListener('click', event => {
let str = ""
// Grab all the siblings of the element that was actually clicked on
for (const sibling of event.target.parentElement.children) {
// Check if they're the title
if (sibling.nodeName != 'title') continue;
str = sibling.innerHTML;
break;
}
const ta = document.createElement('textarea');
ta.value = str;
ta.setAttribute('readonly', '');
ta.style = { position: 'absolute', left: '-9999px' };
document.body.appendChild(ta);
ta.select();
document.execCommand('copy');
document.body.removeChild(ta);
if (str == "") console.log('Nothing found to copy!');
else console.log(`"${str}" copied to clipboard!`);
});
})();
Если вы хотите поместить это в исходный код страницы,вместо вставки в консоль Chrome избавьтесь от объявления функции и вытащите его из скобок. Он будет запускаться при загрузке файла.
Исходное решение:
// Function wrapped in brackets and called immediately after declaration
// (so that it can be run from the Chrome console):
(function addListeners() {
// Grab every SVG 'group' in the 'graph' group:
for (const el of document.querySelectorAll('.graph g')) {
// Tell each group to listen for a click on itself:
el.addEventListener('click', event => {
// Create an empty string variable to store the title in
let str = "";
// Loop through all the elements in the group for one called 'title'
for (const child of el.children) {
if (child.nodeName != 'title') continue;
// Store that title
str = child.innerHTML;
}
// Copy the string to the clipboard (see link above)
const ta = document.createElement('textarea');
ta.value = str;
ta.setAttribute('readonly', '');
ta.style = { position: 'absolute', left: '-9999px' };
document.body.appendChild(ta);
ta.select();
document.execCommand('copy');
document.body.removeChild(ta);
console.log(`"${str}" copied to clipboard!`);
});
}
})();
Я проверял это в консоли разработчика Chrome страницы. Вы связались, и он работает нормально.