Add video preview, video import, and more.

- Add support for importing direct videos urls
- Add video preview support for some sites
- [pixiv] Fix image preview not working
pull/25/head v0.6.0
neobooru 2021-02-01 00:44:41 +01:00
parent de406b1b0b
commit 225981d26d
9 changed files with 4277 additions and 6117 deletions

View File

@ -39,7 +39,7 @@ The third part is the background script which handles the upload functionality.
### The popup
When opened (when you click on the add-on logo in your browser) it sends a message to the content script injected on the current page (active tab). This message asks the content script to scrape the DOM and return a SzuruPost element (with a source, imageUrl, safety, and a list of tags). Once it receives this element it renders it in the popup where you can make changes before importing it. When you click on the "Upload" button the SzuruPost will be sent to the background script which will handle the upload functionality. This is done so that the upload continues even after you close the popup. This means that you can close the popup after clicking the "Upload" button and it'll continue with the upload.
When opened (when you click on the add-on logo in your browser) it sends a message to the content script injected on the current page (active tab). This message asks the content script to scrape the DOM and return a SzuruPost element (with a source, contentUrl, safety, and a list of tags). Once it receives this element it renders it in the popup where you can make changes before importing it. When you click on the "Upload" button the SzuruPost will be sent to the background script which will handle the upload functionality. This is done so that the upload continues even after you close the popup. This means that you can close the popup after clicking the "Upload" button and it'll continue with the upload.
### The content script
@ -53,15 +53,15 @@ This script handles the uploading of a post. It will also update a tag's categor
```sh
# Install node modules
npm install
yarn install
# Use any of the following commands to build once
npm run build
npm run build:dev
yarn build
yarn build:dev
# Or use one of these to build + rebuild on changes
npm run watch
npm run watch:dev
yarn watch
yarn watch:dev
```
After this you can load them in your browser.

6088
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "szuru-chrome",
"version": "0.5.1",
"version": "0.6.0",
"description": "Import media from various sites into szurubooru.",
"author": "neobooru",
"license": "MIT",
@ -16,7 +16,7 @@
},
"dependencies": {
"axios": "^0.21.1",
"neo-scraper": "^0.3.1",
"neo-scraper": "^0.4.0",
"vue": "^2.6.11",
"webextension-polyfill": "^0.3.1"
},

View File

@ -57,7 +57,7 @@ export default class SzuruWrapper {
tags: post.tags.map(x => x.name),
safety: post.rating,
source: post.source,
contentUrl: post.imageUrl
contentUrl: post.contentUrl
};
console.log("Create new post object");
@ -66,9 +66,9 @@ export default class SzuruWrapper {
return (await this.apiPost("posts", obj)).data;
}
async reverseSearch(imageUrl: string): Promise<ImageSearchResult> {
async reverseSearch(contentUrl: string): Promise<ImageSearchResult> {
var obj = {
contentUrl: imageUrl
contentUrl: contentUrl
};
return (await this.apiPost("posts/reverse-search", obj)).data;

View File

@ -14,7 +14,10 @@
"options_page": "options/options.html",
"permissions": [
"storage",
"activeTab"
"activeTab",
"webRequest",
"webRequestBlocking",
"<all_urls>"
],
"content_scripts": [
{
@ -29,8 +32,7 @@
"background": {
"scripts": [
"scripts/background.js"
],
"persistent": false
]
},
"browser_specific_settings": {
"gecko": {

View File

@ -79,7 +79,10 @@
</div>
<div class="popup-section">
<img :src="selectedPost.imageUrl" />
<img :src="selectedPost.contentUrl" v-if="selectedPost.contentType == 'image'" />
<video v-if="selectedPost.contentType == 'video'" controls>
<source :src="selectedPost.contentUrl" />
</video>
<div style="display: flex">
<button class="primary" @click="findSimilar">Find similar</button>
@ -92,7 +95,7 @@
<script lang="ts">
import Vue from "vue";
import { browser, Runtime } from "webextension-polyfill-ts";
import { browser, Runtime, WebRequest } from "webextension-polyfill-ts";
import { ScrapedPost, ScrapedTag, ScrapeResults } from "neo-scraper";
import SzuruWrapper from "../SzuruWrapper";
import { Post, SzuruError } from "../SzuruTypes";
@ -124,18 +127,22 @@ export default Vue.extend({
},
},
methods: {
async getActiveTabId() {
const activeTabs = await browser.tabs.query({
active: true,
currentWindow: true,
});
if (activeTabs.length > 0) return activeTabs[0].id!;
throw "No active tab.";
},
// Try to scrape the post from the page
async grabPost() {
// Get active tab
const activeTab = (
await browser.tabs.query({
active: true,
currentWindow: true,
})
)[0];
const activeTabId = await this.getActiveTabId();
// Send 'grab_post' to the content script on the active tab
const x = await browser.tabs.sendMessage(activeTab.id!, new BrowserCommand("grab_post"));
const x = await browser.tabs.sendMessage(activeTabId, new BrowserCommand("grab_post"));
// We need to create a new ScrapeResults object and fill it with our data, because the get_posts()
// method gets 'lost' when sent from the contentscript to the popup (it gets JSON.stringified and any prototype defines are lost there)
const res = Object.assign(new ScrapeResults(), x);
@ -260,7 +267,7 @@ export default Vue.extend({
this.clearMessages("FIND_SIMILAR");
const msg = this.pushInfo("Searching for similar posts...", "FIND_SIMILAR");
const res = await this.szuru.reverseSearch(this.selectedPost.imageUrl);
const res = await this.szuru.reverseSearch(this.selectedPost.contentUrl);
if (!res.exactPost && res.similarPosts.length == 0) {
msg.content = "No similar posts found";
@ -285,6 +292,13 @@ export default Vue.extend({
}
}
},
// Returns ScrapedPost for a contentUrl, or undefined if the given contentUrl does not belong to a ScrapedPost.
getPostForUrl(contentUrl: string): ScrapedPostViewModel | undefined {
for (const post of this.posts) {
if (post.contentUrl == contentUrl) return post;
}
return undefined;
},
},
async mounted() {
this.config = await Config.load();
@ -310,6 +324,27 @@ export default Vue.extend({
}
});
browser.webRequest.onBeforeSendHeaders.addListener(
(details: WebRequest.OnBeforeSendHeadersDetailsType) => {
let requestHeaders = details.requestHeaders ?? [];
// Find which ScrapedPost this belongs to, if any.
const post = this.getPostForUrl(details.url);
if (post != undefined) {
console.log(`Setting referrer to '${post.referrer}' for request to '${post.contentUrl}'.`);
// TODO: If the headers already have a 'Referer' header this will _not_ override that.
// Though as far as I am aware the 'img' tag doesn't set this header.
requestHeaders.push({ name: "Referer", value: post.referrer });
}
return <WebRequest.BlockingResponse>{
requestHeaders,
};
},
{ urls: ["<all_urls>"] },
["requestHeaders", "blocking", "extraHeaders" as any]
);
// Always call grabPost, even when there is no activeSite
this.grabPost();
},
@ -317,6 +352,10 @@ export default Vue.extend({
</script>
<style scoped>
video {
width: 100%;
}
.popup-container {
width: 700px;
display: grid;

View File

@ -1,12 +1,11 @@
import { ScrapedPost } from "neo-scraper";
import { browser, Runtime } from "webextension-polyfill-ts";
import { BrowserCommand, Message } from "../Common";
import { Config } from "../Config.ts";
import SzuruWrapper from "../SzuruWrapper.ts";
import { Config } from "../Config";
import SzuruWrapper from "../SzuruWrapper";
import { SzuruError } from "../SzuruTypes";
async function uploadPost(post: ScrapedPost) {
// TODO: Upload post and send status updates to popup
console.dir(post);
const cfg = await Config.load();

View File

@ -5,12 +5,12 @@
"strict": true,
"module": "es2020",
"moduleResolution": "node",
"target": "es6",
"target": "es2019",
"skipLibCheck": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"lib": [
"es2017",
"es2019",
"dom"
]
},

4208
yarn.lock Normal file

File diff suppressed because it is too large Load Diff