Skip to content

Commit 9d8acfc

Browse files
committed
阻止页面中的链接、图片等元素被拖到桌面生成复制项
1 parent e325821 commit 9d8acfc

3 files changed

Lines changed: 37 additions & 13 deletions

File tree

frontend/plugin-manager/src/main.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,47 @@ import { initPluginDashboardYuiGuideRuntime } from './yui-guide-runtime'
2727
initDarkMode()
2828
initPluginDashboardYuiGuideRuntime()
2929

30-
function initDecorativeImageDragGuard() {
31-
const markImage = (img: HTMLImageElement) => {
32-
img.draggable = false
33-
img.setAttribute('draggable', 'false')
30+
function initNativeDragGuard() {
31+
const markNativeDragSource = (element: HTMLAnchorElement | HTMLImageElement) => {
32+
element.draggable = false
33+
element.setAttribute('draggable', 'false')
3434
}
3535

36-
const markImages = (root: ParentNode | HTMLImageElement = document) => {
37-
if (root instanceof HTMLImageElement) {
38-
markImage(root)
36+
const markNativeDragSources = (root: ParentNode | HTMLAnchorElement | HTMLImageElement = document) => {
37+
if (root instanceof HTMLAnchorElement || root instanceof HTMLImageElement) {
38+
markNativeDragSource(root)
3939
return
4040
}
41-
root.querySelectorAll<HTMLImageElement>('img').forEach(markImage)
41+
root.querySelectorAll<HTMLAnchorElement | HTMLImageElement>('a[href], img').forEach(markNativeDragSource)
4242
}
4343

4444
const handleDragStart = (event: DragEvent) => {
45-
if (event.target instanceof HTMLImageElement) {
45+
const target = event.target
46+
if (
47+
target instanceof HTMLAnchorElement
48+
|| target instanceof HTMLImageElement
49+
|| (target instanceof Element && target.closest('a[href], img'))
50+
) {
4651
event.preventDefault()
4752
}
4853
}
4954

50-
markImages(document)
55+
markNativeDragSources(document)
5156
document.addEventListener('dragstart', handleDragStart, true)
5257

5358
const observer = new MutationObserver((mutations) => {
5459
mutations.forEach((mutation) => {
5560
mutation.addedNodes.forEach((node) => {
5661
if (node instanceof Element) {
57-
markImages(node)
62+
markNativeDragSources(node)
5863
}
5964
})
6065
})
6166
})
6267
observer.observe(document.documentElement, { childList: true, subtree: true })
6368
}
6469

65-
initDecorativeImageDragGuard()
70+
initNativeDragGuard()
6671

6772
console.log('🚀 Starting N.E.K.O Plugin Management System...')
6873

static/css/base.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ button img,
3939

4040
img,
4141
svg,
42+
a[href],
4243
[aria-hidden="true"] img {
4344
-webkit-user-drag: none;
4445
-khtml-user-drag: none;

static/js/window_controls.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const CONTROL_SELECTOR = '[data-neko-window-control]';
55
const MAXIMIZE_ICON_SELECTOR = '.neko-window-maximize-icon';
6+
const NATIVE_DRAG_SOURCE_SELECTOR = 'a[href], img, svg, video, audio';
67

78
function translate(key, fallback) {
89
try {
@@ -121,6 +122,19 @@
121122
}
122123
}
123124

125+
function initNativeDragGuard() {
126+
if (window.__nekoNativeDragGuardBound) return;
127+
window.__nekoNativeDragGuardBound = true;
128+
129+
document.addEventListener('dragstart', (event) => {
130+
const target = event.target;
131+
if (!target || typeof target.closest !== 'function') return;
132+
const source = target.closest(NATIVE_DRAG_SOURCE_SELECTOR);
133+
if (!source) return;
134+
event.preventDefault();
135+
}, true);
136+
}
137+
124138
async function restoreCurrentWindowFromOpener() {
125139
const api = window.nekoWindowControl;
126140
if (!api || typeof api.restore !== 'function') return;
@@ -139,8 +153,12 @@
139153
});
140154

141155
if (document.readyState === 'loading') {
142-
document.addEventListener('DOMContentLoaded', initWindowControls);
156+
document.addEventListener('DOMContentLoaded', () => {
157+
initWindowControls();
158+
initNativeDragGuard();
159+
});
143160
} else {
144161
initWindowControls();
162+
initNativeDragGuard();
145163
}
146164
})();

0 commit comments

Comments
 (0)