Skip to content

Instantly share code, notes, and snippets.

@channprj
Created May 21, 2025 17:13
Show Gist options
  • Save channprj/5b0535bcedc80b65eb881cc6e8afcd0e to your computer and use it in GitHub Desktop.
Save channprj/5b0535bcedc80b65eb881cc6e8afcd0e to your computer and use it in GitHub Desktop.
Allow drag and copy and make web great again.
(function() {
'use strict';
// 우클릭 차단을 해제하는 함수
function enableRightClick(e) {
e.stopPropagation();
}
// 컨텍스트 메뉴 이벤트 재정의
document.addEventListener('contextmenu', enableRightClick, true);
// 우클릭 및 복사 차단 속성 제거 함수
function removeBlockingAttributes(node) {
node.removeAttribute('oncontextmenu');
node.removeAttribute('oncopy');
node.removeAttribute('onselectstart');
node.removeAttribute('unselectable');
node.style.userSelect = 'auto';
node.style.webkitUserSelect = 'auto';
node.style.MozUserSelect = 'auto';
node.style.msUserSelect = 'auto';
}
// 모든 요소에서 차단 속성 제거
document.querySelectorAll('*').forEach(function(node) {
removeBlockingAttributes(node);
});
// 동적으로 추가되는 요소를 감시하는 MutationObserver 설정
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === 1) { // 요소 노드인 경우
removeBlockingAttributes(node);
}
});
});
});
// 문서 전체를 감시 대상으로 설정
observer.observe(document, {
childList: true,
subtree: true
});
// 드래그 감지 및 텍스트 복사를 위한 변수
let startX, startY, dragging = false, targetElement = null;
// 마우스 다운 이벤트 처리
document.addEventListener('mousedown', function(event) {
if (event.target.tagName.toLowerCase() === 'a' || event.target.tagName.toLowerCase() === 'button') {
startX = event.clientX;
startY = event.clientY;
dragging = true;
targetElement = event.target;
}
});
// 마우스 이동 이벤트 처리
document.addEventListener('mousemove', function(event) {
if (dragging) {
const distance = Math.sqrt(Math.pow(event.clientX - startX, 2) + Math.pow(event.clientY - startY, 2));
if (distance > 5) { // 5픽셀 이상 이동했을 때 드래그로 간주
if (targetElement) {
const elementText = targetElement.textContent.trim();
navigator.clipboard.writeText(elementText)
.then(() => {
console.log('텍스트가 클립보드에 복사됨:', elementText);
})
.catch(err => {
console.error('텍스트 복사 실패: ', err);
});
dragging = false;
targetElement = null;
}
}
}
});
// 마우스 업 이벤트 처리
document.addEventListener('mouseup', function() {
dragging = false;
targetElement = null;
});
// 마우스가 문서 밖으로 나갈 때 처리
document.addEventListener('mouseleave', function() {
dragging = false;
targetElement = null;
});
// 클립보드에 추가되는 부가 텍스트 제거
document.addEventListener('copy', function(event) {
event.stopPropagation();
const selection = document.getSelection();
if (!selection.rangeCount) return;
const copiedText = selection.toString().trim();
event.clipboardData.setData('text/plain', copiedText);
event.preventDefault();
}, true);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment