1
1
<script setup lang="ts">
2
2
import { useDark } from " @vueuse/core" ;
3
3
import { cloneDeep } from " lodash" ;
4
- import { NeoScraper , ScrapeResults } from " neo-scraper" ;
4
+ import { ScrapeResults } from " neo-scraper" ;
5
5
import { getUrl , encodeTagName , getErrorMessage , getPostInfoSummary } from " ~/utils" ;
6
6
import {
7
7
BrowserCommand ,
@@ -14,7 +14,6 @@ import {
14
14
SzuruSiteConfig ,
15
15
PoolDetails ,
16
16
} from " ~/models" ;
17
- import { ImageSearchResult } from " ~/api/models" ;
18
17
import { isMobile } from " ~/env" ;
19
18
import { DeepReadonly } from " vue" ;
20
19
import { cfg , usePopupStore } from " ~/stores" ;
@@ -99,14 +98,11 @@ async function grabPost() {
99
98
// Clear current posts array
100
99
pop .posts .splice (0 );
101
100
102
- const scraper = new NeoScraper ();
103
-
104
101
for (const result of res .results ) {
105
- const engine = scraper .engines .find ((x ) => x .name == result .engine );
106
-
107
102
for (const i in result .posts ) {
108
103
const vm = new ScrapedPostDetails (result .posts [i ]);
109
- vm .name = ` [${result .engine }] Post ${parseInt (i ) + 1 } ` ; // parseInt() is required!
104
+ const name = result .posts [i ].name ?? ` Post ${parseInt (i ) + 1 } ` ; // parseInt() is required!
105
+ vm .name = ` [${result .engine }] ${name } ` ;
110
106
111
107
if (! cfg .value .addAllParsedTags ) {
112
108
vm .tags .splice (0 );
@@ -125,10 +121,6 @@ async function grabPost() {
125
121
vm .instanceSpecificData [site .id ] = {};
126
122
}
127
123
128
- if (engine ) {
129
- vm .uploadMode = engine .uploadMode ;
130
- }
131
-
132
124
pop .posts .push (vm );
133
125
}
134
126
}
@@ -145,7 +137,7 @@ async function grabPost() {
145
137
}
146
138
147
139
if (cfg .value .fetchPostInfo ) {
148
- await fetchPostInfo ();
140
+ await fetchPostsInfo ();
149
141
}
150
142
151
143
// Delayed call to findSimilar, as fetchPostInfo might change the post's contentUrl.
@@ -168,7 +160,7 @@ async function upload() {
168
160
}
169
161
170
162
try {
171
- const post: ScrapedPostDetails = cloneDeep (pop .selectedPost );
163
+ const post: ScrapedPostDetails = cloneDeep (pop .selectedPost )! ;
172
164
173
165
// uploadMode "content" requires a content token to work. So ensure it is set.
174
166
if (post .uploadMode == " content" ) {
@@ -291,10 +283,10 @@ async function findSimilar(post: ScrapedPostDetails | undefined) {
291
283
292
284
isSearchingForSimilarPosts .value ++ ;
293
285
294
- await ensurePostHasContentToken (post );
295
-
296
286
try {
297
- const res = await selectedInstance .reverseSearchToken (instanceSpecificData .contentToken );
287
+ await ensurePostHasContentToken (post );
288
+
289
+ const res = await selectedInstance .reverseSearchToken (instanceSpecificData .contentToken ! );
298
290
299
291
instanceSpecificData .reverseSearchResult = {
300
292
exactPostId: res .exactPost ?.id ,
@@ -331,33 +323,58 @@ async function loadTagCounts() {
331
323
}
332
324
}
333
325
334
- async function fetchPostInfo() {
326
+ async function updatePostWithRemoteInfo(post : ScrapedPostDetails , contentUrl : string ) {
327
+ // We are missing cookies/etc which means that the request might fail.
328
+ // If you want access to the cookies/session/etc then you need to execute this code inside the content script.
329
+ try {
330
+ // This request follows redirects.
331
+ const res = await fetch (contentUrl , { method: " HEAD" });
332
+ const size = res .headers .get (" Content-Length" );
333
+ const type = res .headers .get (" Content-Type" );
334
+
335
+ if (type ) {
336
+ if (type .indexOf (" text/html" ) != - 1 ) {
337
+ // If we get a HTML page back it usually means that the request failed.
338
+ // Not all sites (e.g. e-hentai) reply with a 403.
339
+ throw new Error (
340
+ " Received a text/html content type. This probably means that we don't have permission to access the resource." ,
341
+ );
342
+ }
343
+
344
+ const [_main, sub] = type .split (" /" );
345
+ if (sub ) post .contentSubType = sub .toUpperCase ();
346
+ }
347
+
348
+ // This should be after the type check, because the type check also checks whether the response we get is valid/unsable.
349
+ if (size ) post .contentSize = parseInt (size );
350
+
351
+ // Update url if it changes. The url can change when:
352
+ // a. the extraContentUrl was successfully loaded
353
+ // b. because of redirects
354
+ if (res .url != post .contentUrl ) {
355
+ // The developer needs to make sure that the remote server can load this URL if `post.uploadMode == 'url'`!
356
+ console .log (` Updating post.contentUrl to '${res .url }' ` );
357
+ post .contentUrl = res .url ;
358
+ }
359
+
360
+ return true ;
361
+ } catch (ex ) {
362
+ console .error (ex );
363
+ return false ;
364
+ }
365
+ }
366
+
367
+ async function fetchPostsInfo() {
335
368
for (const post of pop .posts ) {
336
369
if (! post .contentSize || post .extraContentUrl ) {
337
- // We are missing cookies/etc which means that the request might fail.
338
- // If you want access to the cookies/session/etc then you need to execute this code inside the content script.
339
- try {
340
- const contentUrl = post .extraContentUrl ?? post .contentUrl ;
341
-
342
- // TODO: Check whether we need to call this in the background page, or if it is fine to call it from the popup.
343
- const res = await fetch (contentUrl , { method: " HEAD" });
344
- const size = res .headers .get (" Content-Length" );
345
- const type = res .headers .get (" Content-Type" );
346
-
347
- if (size ) post .contentSize = parseInt (size );
348
-
349
- if (type ) {
350
- const [_main, sub] = type .split (" /" );
351
- if (sub ) post .contentSubType = sub .toUpperCase ();
352
- }
353
-
354
- // Resolve extraContentUrl redirects.
355
- if (post .extraContentUrl && res .url != post .contentUrl ) {
356
- post .contentUrl = res .url ;
357
- }
358
- } catch (ex ) {
359
- // TODO: Maybe display an error in the UI.
360
- console .error (ex );
370
+ let ok = false ;
371
+ if (post .extraContentUrl ) {
372
+ // Prioritize info from extraContentUrl.
373
+ ok = await updatePostWithRemoteInfo (post , post .extraContentUrl );
374
+ }
375
+ if (! ok ) {
376
+ // Fall back to normal contentUrl if we can't get the data from the extraContentUrl.
377
+ await updatePostWithRemoteInfo (post , post .contentUrl );
361
378
}
362
379
}
363
380
}
@@ -369,7 +386,8 @@ function getUpdatedTagsText(count: number) {
369
386
}
370
387
371
388
function onResolutionLoaded(res : any ) {
372
- if (pop .selectedPost && ! pop .selectedPost .resolution ) {
389
+ // This no longer checks for `!pop.selectedPost.resolution` because the resolution can change if the contentUrl is updated.
390
+ if (pop .selectedPost ) {
373
391
pop .selectedPost .resolution = res ;
374
392
}
375
393
}
0 commit comments