-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
59 lines (53 loc) · 1.65 KB
/
Copy pathbackground.js
File metadata and controls
59 lines (53 loc) · 1.65 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
// 3D Viewer by SceneView — Background Service Worker
// Handles context menu creation and viewer tab opening
const MODEL_EXTENSIONS = ['.glb', '.gltf', '.usdz'];
const CONTEXT_MENU_ID = 'sceneview-preview-3d';
// Create context menu on install
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: CONTEXT_MENU_ID,
title: 'Preview in 3D',
contexts: ['link'],
targetUrlPatterns: [
'*://*/*.glb*',
'*://*/*.gltf*',
'*://*/*.usdz*',
'*://*/*.GLB*',
'*://*/*.GLTF*',
'*://*/*.USDZ*',
],
});
// Set default settings
chrome.storage.local.get(['theme'], (result) => {
if (!result.theme) {
chrome.storage.local.set({ theme: 'dark' });
}
});
});
// Handle context menu click
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === CONTEXT_MENU_ID && info.linkUrl) {
openViewer(info.linkUrl);
}
});
// Handle messages from content script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'openViewer' && message.url) {
openViewer(message.url);
sendResponse({ success: true });
}
if (message.action === 'getTheme') {
chrome.storage.local.get(['theme'], (result) => {
sendResponse({ theme: result.theme || 'dark' });
});
return true; // async response
}
if (message.action === 'setTheme') {
chrome.storage.local.set({ theme: message.theme });
sendResponse({ success: true });
}
});
function openViewer(modelUrl) {
const viewerUrl = chrome.runtime.getURL('viewer.html') + '?model=' + encodeURIComponent(modelUrl);
chrome.tabs.create({ url: viewerUrl });
}