-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
82 lines (69 loc) · 2 KB
/
Copy pathcontent.js
File metadata and controls
82 lines (69 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// 3D Viewer by SceneView — Content Script
// Detects 3D model links on web pages and adds preview icons
const MODEL_PATTERN = /\.(glb|gltf|usdz)(\?.*)?$/i;
function isModelLink(href) {
if (!href) return false;
try {
const url = new URL(href, window.location.origin);
return MODEL_PATTERN.test(url.pathname);
} catch {
return false;
}
}
function getModelFormat(href) {
const match = href.match(/\.(glb|gltf|usdz)/i);
return match ? match[1].toUpperCase() : '3D';
}
function createPreviewButton(link) {
const btn = document.createElement('span');
btn.className = 'sceneview-preview-btn';
btn.title = `Preview ${getModelFormat(link.href)} in 3D`;
btn.textContent = '\uD83D\uDC41\uFE0F';
btn.setAttribute('role', 'button');
btn.setAttribute('aria-label', `Preview 3D model: ${link.href.split('/').pop()}`);
btn.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
chrome.runtime.sendMessage({
action: 'openViewer',
url: link.href,
});
});
return btn;
}
function scanAndAnnotateLinks() {
const links = document.querySelectorAll('a[href]');
let count = 0;
links.forEach((link) => {
// Skip already processed links
if (link.dataset.sceneviewProcessed) return;
link.dataset.sceneviewProcessed = 'true';
if (isModelLink(link.href)) {
const btn = createPreviewButton(link);
link.parentNode.insertBefore(btn, link.nextSibling);
count++;
}
});
if (count > 0) {
console.log(`[3D Viewer by SceneView] Found ${count} 3D model link(s) on this page`);
}
}
// Initial scan
scanAndAnnotateLinks();
// Watch for dynamically added links (SPAs, infinite scroll)
const observer = new MutationObserver((mutations) => {
let hasNewNodes = false;
for (const mutation of mutations) {
if (mutation.addedNodes.length > 0) {
hasNewNodes = true;
break;
}
}
if (hasNewNodes) {
scanAndAnnotateLinks();
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});