function makeElementsSelectableAndCopyable() {
// Store the currently selected element
let selectedElement = null;
let originalStyles = new WeakMap();
const eventHandlers = new WeakMap();
let active = true;
// Create the instruction div
const instructionDiv = document.createElement('div');
instructionDiv.setAttribute('id', 'element-selector-instructions');
instructionDiv.style.position = 'fixed';
instructionDiv.style.bottom = '10px';
instructionDiv.style.right = '10px';
instructionDiv.style.padding = '10px';
instructionDiv.style.background = 'rgba(0, 0, 0, 0.7)';
instructionDiv.style.color = 'white';
instructionDiv.style.borderRadius = '5px';
instructionDiv.style.zIndex = '9999';
instructionDiv.style.fontFamily = 'Arial, sans-serif';
instructionDiv.innerHTML = `
<div style="margin-bottom: 5px; font-weight: bold;">Element Selector Tool</div>
<div>• Hover over elements to highlight them</div>
<div>• Click to select an element</div>
<div>• Press C or click Copy to copy the selected element</div>
<div>• Press ESC to exit selection mode</div>
<button id="copy-element-btn" style="margin-top: 8px; padding: 4px 8px; background: #3498db; border: none; border-radius: 3px; color: white; cursor: pointer;">Copy</button>
<div id="copy-feedback" style="display: none; margin-top: 5px; color: #2ecc71;">Copied to clipboard!</div>
`;
// Function to save original styles
function saveOriginalStyles(element) {
if (!originalStyles.has(element)) {
originalStyles.set(element, {
outline: element.style.outline,
cursor: element.style.cursor
});
}
}
// Function to restore original styles
function restoreOriginalStyles(element) {
const styles = originalStyles.get(element);
if (styles) {
element.style.outline = styles.outline;
element.style.cursor = styles.cursor;
}
}
// Function to highlight an element on hover
function highlightElement(event) {
if (!active || this === instructionDiv) return;
saveOriginalStyles(this);
this.style.outline = '2px dashed #3498db';
this.style.cursor = 'pointer';
event.stopPropagation();
}
// Function to remove highlight
function removeHighlight(event) {
if (!active || this === instructionDiv || this === selectedElement) return;
restoreOriginalStyles(this);
event.stopPropagation();
}
// Function to handle element selection
function handleClick(event) {
if (!active) return;
event.preventDefault();
event.stopPropagation();
if (selectedElement) {
restoreOriginalStyles(selectedElement);
}
selectedElement = this;
saveOriginalStyles(selectedElement);
selectedElement.style.outline = '2px solid #e74c3c';
console.log('Selected element:', selectedElement);
return false;
}
// Function to copy the selected element to clipboard
function copySelectedElement() {
if (!selectedElement) {
console.log('No element selected');
return;
}
// Get the HTML of the selected element
const elementHTML = selectedElement.outerHTML;
// Copy to clipboard
navigator.clipboard.writeText(elementHTML).then(() => {
// Show feedback
const feedback = document.getElementById('copy-feedback');
feedback.style.display = 'block';
// Hide feedback after 2 seconds
setTimeout(() => {
feedback.style.display = 'none';
}, 2000);
console.log('Element HTML copied to clipboard');
}).catch(err => {
console.error('Failed to copy:', err);
});
}
// Function to handle key presses
function handleKeyDown(event) {
if (event.key === 'Escape') {
stopSelectMode();
} else if (event.key === 'c' || event.key === 'C') {
copySelectedElement();
}
}
// Add handlers to an element
function addEventHandlers(element) {
const handlers = {
mouseover: highlightElement.bind(element),
mouseout: removeHighlight.bind(element),
click: handleClick.bind(element)
};
eventHandlers.set(element, handlers);
element.addEventListener('mouseover', handlers.mouseover, true);
element.addEventListener('mouseout', handlers.mouseout, true);
element.addEventListener('click', handlers.click, true);
}
// Add the instruction div to the document
document.body.appendChild(instructionDiv);
// Set up the copy button
const copyButton = document.getElementById('copy-element-btn');
copyButton.addEventListener('click', function(event) {
event.stopPropagation();
copySelectedElement();
});
// Add event handlers to all existing elements
const allElements = document.querySelectorAll('*');
allElements.forEach(element => {
if (element === document.body ||
element === document.documentElement ||
element === instructionDiv ||
element.closest('#element-selector-instructions')) {
return;
}
addEventHandlers(element);
});
// Handle dynamic elements
document.addEventListener('mouseover', function(event) {
if (!active) return;
const element = event.target;
if (!eventHandlers.has(element) &&
element !== document.body &&
element !== document.documentElement &&
!element.closest('#element-selector-instructions')) {
addEventHandlers(element);
}
}, true);
document.addEventListener('keydown', handleKeyDown);
// Function to stop the selection mode
function stopSelectMode() {
if (!active) return selectedElement;
active = false;
eventHandlers.forEach((handlers, element) => {
element.removeEventListener('mouseover', handlers.mouseover, true);
element.removeEventListener('mouseout', handlers.mouseout, true);
element.removeEventListener('click', handlers.click, true);
restoreOriginalStyles(element);
});
document.removeEventListener('keydown', handleKeyDown);
if (instructionDiv.parentNode) {
instructionDiv.parentNode.removeChild(instructionDiv);
}
console.log('Element selection mode stopped.');
return selectedElement;
}
return stopSelectMode;
}
// Start the selection mode
const stopSelectMode = makeElementsSelectableAndCopyable();
// To stop the selection mode later: const selectedElement = stopSelectMode();
javascript:(function(){function makeElementsSelectableAndCopyable(){let selectedElement=null;let originalStyles=new WeakMap();const eventHandlers=new WeakMap();let active=true;const instructionDiv=document.createElement('div');instructionDiv.setAttribute('id','element-selector-instructions');instructionDiv.style.position='fixed';instructionDiv.style.bottom='10px';instructionDiv.style.right='10px';instructionDiv.style.padding='10px';instructionDiv.style.background='rgba(0,0,0,0.7)';instructionDiv.style.color='white';instructionDiv.style.borderRadius='5px';instructionDiv.style.zIndex='9999';instructionDiv.style.fontFamily='Arial,sans-serif';instructionDiv.innerHTML=`<div style="margin-bottom:5px;font-weight:bold;">Element Selector Tool</div><div>• Hover over elements to highlight them</div><div>• Click to select an element</div><div>• Press C or click Copy to copy the selected element</div><div>• Press ESC to exit selection mode</div><button id="copy-element-btn" style="margin-top:8px;padding:4px 8px;background:#3498db;border:none;border-radius:3px;color:white;cursor:pointer;">Copy</button><div id="copy-feedback" style="display:none;margin-top:5px;color:#2ecc71;">Copied to clipboard!</div>`;function saveOriginalStyles(element){if(!originalStyles.has(element)){originalStyles.set(element,{outline:element.style.outline,cursor:element.style.cursor});}}function restoreOriginalStyles(element){const styles=originalStyles.get(element);if(styles){element.style.outline=styles.outline;element.style.cursor=styles.cursor;}}function highlightElement(event){if(!active||this===instructionDiv)return;saveOriginalStyles(this);this.style.outline='2px dashed #3498db';this.style.cursor='pointer';event.stopPropagation();}function removeHighlight(event){if(!active||this===instructionDiv||this===selectedElement)return;restoreOriginalStyles(this);event.stopPropagation();}function handleClick(event){if(!active)return;event.preventDefault();event.stopPropagation();if(selectedElement){restoreOriginalStyles(selectedElement);}selectedElement=this;saveOriginalStyles(selectedElement);selectedElement.style.outline='2px solid #e74c3c';console.log('Selected element:',selectedElement);return false;}function copySelectedElement(){if(!selectedElement){console.log('No element selected');return;}const elementHTML=selectedElement.outerHTML;navigator.clipboard.writeText(elementHTML).then(()=>{const feedback=document.getElementById('copy-feedback');feedback.style.display='block';setTimeout(()=>{feedback.style.display='none';},2000);console.log('Element HTML copied to clipboard');}).catch(err=>{console.error('Failed to copy:',err);});}function handleKeyDown(event){if(event.key==='Escape'){stopSelectMode();}else if(event.key==='c'||event.key==='C'){copySelectedElement();}}function addEventHandlers(element){const handlers={mouseover:highlightElement.bind(element),mouseout:removeHighlight.bind(element),click:handleClick.bind(element)};eventHandlers.set(element,handlers);element.addEventListener('mouseover',handlers.mouseover,true);element.addEventListener('mouseout',handlers.mouseout,true);element.addEventListener('click',handlers.click,true);}document.body.appendChild(instructionDiv);const copyButton=document.getElementById('copy-element-btn');copyButton.addEventListener('click',function(event){event.stopPropagation();copySelectedElement();});const allElements=document.querySelectorAll('*');allElements.forEach(element=>{if(element===document.body||element===document.documentElement||element===instructionDiv||element.closest('#element-selector-instructions')){return;}addEventHandlers(element);});document.addEventListener('mouseover',function(event){if(!active)return;const element=event.target;if(!eventHandlers.has(element)&&element!==document.body&&element!==document.documentElement&&!element.closest('#element-selector-instructions')){addEventHandlers(element);}},true);document.addEventListener('keydown',handleKeyDown);function stopSelectMode(){if(!active)return selectedElement;active=false;eventHandlers.forEach((handlers,element)=>{element.removeEventListener('mouseover',handlers.mouseover,true);element.removeEventListener('mouseout',handlers.mouseout,true);element.removeEventListener('click',handlers.click,true);restoreOriginalStyles(element);});document.removeEventListener('keydown',handleKeyDown);if(instructionDiv.parentNode){instructionDiv.parentNode.removeChild(instructionDiv);}console.log('Element selection mode stopped.');return selectedElement;}return stopSelectMode;}makeElementsSelectableAndCopyable();})();