Skip to content

Commit

Permalink
ダッシュボードでスクショボタンを設置
Browse files Browse the repository at this point in the history
Fix #1743
  • Loading branch information
otiai10 committed Nov 24, 2024
1 parent 963d15b commit 3d125f5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/page/components/dashboard/ActionsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useMemo } from "react";
import { Launcher } from "../../../services/Launcher";
import { useRevalidator } from "react-router-dom";
import { DownloadService } from "../../../services/DownloadService";

function MuteControlButton({ tab, launcher, refresh }: { tab?: chrome.tabs.Tab, launcher: Launcher, refresh: () => void }) {
if (!tab) return null;
Expand All @@ -14,11 +15,16 @@ function MuteControlButton({ tab, launcher, refresh }: { tab?: chrome.tabs.Tab,
)
}

function CaptureControlButton({tab}: { tab?: chrome.tabs.Tab }) {
function CaptureControlButton({ tab, launcher }: { tab?: chrome.tabs.Tab, launcher: Launcher }) {
if (!tab) return null;
return (
<div onClick={() => {
window.alert("capture!");
<div onClick={async () => {
const s = new DownloadService();
const format = "jpeg";
const uri = await launcher.capture(tab.windowId, { format });
const dir = "艦これ";
/* const downloadId = */ await s.download(uri, DownloadService.filename.screenshot({ dir, format }));
// await s.show(downloadId);
}} className="cursor-pointer text-slate-400 hover:text-slate-600">
<FontAwesomeIcon icon="camera" className="w-8 h-8"/>
</div>
Expand All @@ -40,7 +46,7 @@ export function ActionsView({tab}: { tab?: chrome.tabs.Tab }) {
return (
<div className="flex-1 flex items-center justify-end space-x-4">
<LaunchControlButton />
<CaptureControlButton tab={tab} />
<CaptureControlButton tab={tab} launcher={launcher} />
<MuteControlButton tab={tab} launcher={launcher} refresh={refresh} />
</div>
)
Expand Down
1 change: 1 addition & 0 deletions src/public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"permissions": [
"activeTab",
"alarms",
"downloads",
"notifications",
"scripting",
"storage",
Expand Down
24 changes: 24 additions & 0 deletions src/services/DownloadService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

export class DownloadService {
constructor(
private readonly mod = chrome.downloads,
) { }

public static filename = {
screenshot: (opt: { dir?: string, format: "jpeg" | "png" }) => {
const datetime = new Date().toISOString().replace(/[-:]/g, "").replace("T", "_").replace("Z", "").replace(/\..+/, "");
return opt.dir ? `${opt.dir}/${datetime}.${opt.format}` : `艦これ_${datetime}.${opt.format}`;
},
}
public async download(url: string, filename: string): Promise<number> {
return new Promise((resolve, reject) => {
this.mod.download({ url, filename }, (id) => {
if (chrome.runtime.lastError) return reject(chrome.runtime.lastError);
resolve(id);
});
});
}
public async show(downloadId: number) {
return this.mod.show(downloadId);
}
}
7 changes: 7 additions & 0 deletions src/services/Launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ export class Launcher {
public async mute(tabId: number, muted: boolean = true) {
return this.tabs.update(tabId, { muted });
}

public async capture(tabId: number, options: chrome.tabs.CaptureVisibleTabOptions = {
format: "jpeg",
quality: 100,
}): Promise<string> {
return this.tabs.capture(tabId, options);
}
}
9 changes: 9 additions & 0 deletions src/services/TabService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@ export class TabService {
public async update(tabId: number, props: chrome.tabs.UpdateProperties) {
return await this.mod.update(tabId, props);
}

// TODO: 各所で chrome.tabs.captureVisibleTab を使っているので、ここに集約させたい
public async capture(windowId: number, options: chrome.tabs.CaptureVisibleTabOptions) {
return await new Promise<string>((resolve) => {
this.mod.captureVisibleTab(windowId, options, (dataUrl) => {
resolve(dataUrl);
});
});
}
}

0 comments on commit 3d125f5

Please sign in to comment.