-
Notifications
You must be signed in to change notification settings - Fork 0
/
model-detectors.js
259 lines (208 loc) · 6.1 KB
/
model-detectors.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* model detectors */
/*
Extract 3D Model From html string
3DModel Schema example:
<script type="application/ld+json">
{
"@context" : "http://schema.org/",
"@type" : "3DModel",
"image" : "https://images.samsung.com/us/smartphones/galaxy-note20/v1/images/galaxy-note20-thumbnail-image.jpg",
"name" : "Samsung Galaxy Note20 Ultra 5G",
"encoding" : [
{
"@type" : "MediaObject",
"contentUrl" : "https://images.samsung.com/common/smartphones/models/galaxy-note20-ultra/galaxy-note20-ultra-mystic-bronze.glb",
"encodingFormat" : "model/gltf-binary"
}
]
}
</script>
*/
function get3DModelFromHtml(html) {
const key = `3DModel`;
const start = `<script type="application/ld+json">`;
const end = `</script>`;
let modelIndex = html.indexOf(key);
if (modelIndex === -1) {
console.log('no 3DModel found');
return false;
}
let startIndex = html.lastIndexOf(start, modelIndex);
if (startIndex === -1) {
return false;
}
let endIndex = html.indexOf(end, startIndex);
if (endIndex === -1) {
return false;
}
let section = html.substring(startIndex+start.length, endIndex);
console.log(section);
let json = JSON.parse(section);
console.log(json);
let modelUrl = getModelUrlFromSchema(json);
return modelUrl;
}
/*
*/
function get3DModelFromPage() {
let scripts = document.querySelectorAll("script[type='application/ld+json']");
let schema;
let json;
const key = `3DModel`;
for (let i = 0; i < scripts.length; i++) {
let script = scripts[i];
let schemaText = script.innerHTML;
if (!schemaText.includes(key))
continue;
schema = JSON.parse(schemaText);
if (schema['@type'] && schema['@type'] === key) {
console.log('found 3D model');
json = schema;
console.log(json);
break;
}
}
if (!json) {
return;
}
let modelUrl = getModelUrlFromSchema(json);
return modelUrl;
}
function getModelUrlFromSchema(schema) {
let modelUrl;
const mimeTime = 'model/gltf';
let encodings = schema.encoding;
for (let i = 0; i < encodings.length; i++) {
let en = encodings[i];
if (en.encodingFormat.startsWith('model/gltf')) {
modelUrl = en.contentUrl;
break;
}
}
return modelUrl;
}
/*
Example:
<model-viewer src="model.glb" ar camera-controls alt="model"></model-viewer>
*/
function getModelFromModelViewer() {
let modelViewerTags = document.getElementsByTagName('model-viewer');
if (modelViewerTags.length === 0) {
return;
}
let mv = modelViewerTags[0];
let src = mv.getAttribute('src');
return src;
}
/*
Google Search page results may contain models:
<g-link>
<a data-url="${url}">
<span>View in 3D</span>
</a>
</g-link>
*/
function getModelFromGoogleSearch() {
let gLinks = document.querySelectorAll('g-link');
for (let i = 0; i < gLinks.length; i++) {
let gLink = gLinks[i];
console.log(gLink);
let link = gLink.querySelector('a');
if (link) {
let dataUrl = link.getAttribute('data-url');
if (dataUrl && dataUrl.startsWith('intent://arvr')) {
let intentInfo = extractIntentInfo(dataUrl);
return intentInfo;
}
}
/*let spans = gLink.querySelectorAll('span');
for (let k = 0; k < spans.length; k++) {
let span = spans[k];
const VIEW_3D_LABEL = 'View in 3D';
console.log(span.textContent);
if (span.textContent === VIEW_3D_LABEL) {
console.log(`'${VIEW_3D_LABEL}' model found`);
let link = gLink.querySelector('a');
let dataUrl = link.getAttribute('data-url');
console.log(dataUrl);
let intentInfo = extractIntentInfo(dataUrl);
return intentInfo;
}
}*/
}
}
/*
example:
intent://arvr.google.com/scene-viewer/1.2
?file=https://storage.googleapis.com/searchar/306423cf48b3c9e8/162f1f9bb74fbaca/gltf/galaxy-note20-ultra-mystic-bronze.glb
&title=Samsung+Galaxy+Note20+Ultra+5G
&referrer=google.com:result
&link=https://www.samsung.com/us/smartphones/galaxy-note20-5g/
#Intent;
package=com.google.android.googlequicksearchbox;
scheme=https;
S.browser_fallback_url=https://arvr.google.com/scene-viewer
?file=https://storage.googleapis.com/searchar/306423cf48b3c9e8/162f1f9bb74fbaca/gltf/galaxy-note20-ultra-mystic-bronze.glb
&title=Samsung+Galaxy+Note20+Ultra+5G
&referrer=google.com:result
&link=https://www.samsung.com/us/smartphones/galaxy-note20-5g/;end;
*/
function extractIntentInfo(url) {
let paramsString = url.substring(url.indexOf('?')+1);
const FB_URL_PARAM = 'S.browser_fallback_url';
const INTENT_END = ';end;';
const INTENT_START = '#Intent';
let info = {};
let fallbackUrl = paramsString.substring(
paramsString.indexOf(FB_URL_PARAM)+FB_URL_PARAM.length+1);
fallbackUrl = fallbackUrl.replace(INTENT_END, '');
info['viewerUrl'] = fallbackUrl;
console.log('fallbackUrl', fallbackUrl);
paramsString = paramsString.split(INTENT_START)[0];
let parts = paramsString.split('&');
for (let i = 0; i < parts.length; i++) {
let param = parts[i];
let key = param.substring(0, param.indexOf('='));
let value = param.substring(param.indexOf('=')+1);
//console.log(key, value);
info[key] = value;
}
return info;
}
/*
https://artsandculture.google.com/project/ar
<div data-model-url="model.glb">
*/
function getModelFromGoogleArtsAndCulture() {
const GOOGLE_AC_HOST = 'artsandculture.google.com';
let el = document.querySelector('div[data-model-url]');
if (el) {
let url = el.getAttribute('data-model-url');
return url;
}
}
/*
GLB supported
<a href="/path/model.glb" id="raw-url" role="button" class="btn">Download</a>
*/
function getModelFromGitHub() {
let rawEl = document.querySelector('#raw-url');
if (rawEl) {
let url = rawEl.href;
if (url.includes('.glb') || url.includes('.gltf')) {
return url;
}
}
}
/*
<a aria-label="Download" download="resource/model.glb" href="path/model.glb">
*/
function getModelFromGitLab() {
let rawEl = document.querySelector('a[aria-label="Download"]');
if (rawEl) {
let url = rawEl.href;
if (url.includes('.glb') || url.includes('.gltf')) {
return url;
}
}
}