Skip to content

Commit fe8f576

Browse files
committed
tsify background
1 parent b5aacfd commit fe8f576

File tree

3 files changed

+108
-99
lines changed

3 files changed

+108
-99
lines changed

src/pages/Background/index.js

Lines changed: 0 additions & 98 deletions
This file was deleted.

src/pages/Background/index.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import '../../assets/img/icon-34.png';
2+
import '../../assets/img/icon-128.png';
3+
import { defaults } from '../../shared/defaults';
4+
// @ts-ignore
5+
import imageClipper from './image-clipper';
6+
7+
interface ImageDimension {
8+
w: number;
9+
h: number;
10+
}
11+
12+
chrome.storage.sync.set({ openInTab: defaults.openInTab });
13+
chrome.storage.sync.set({ download: defaults.download });
14+
15+
const getImageDimensions = (file: string): Promise<ImageDimension> => {
16+
return new Promise(function (resolved, rejected) {
17+
var img = new Image();
18+
img.onload = function () {
19+
resolved({ w: img.width, h: img.height });
20+
};
21+
img.src = file;
22+
});
23+
};
24+
25+
chrome.browserAction.setTitle({
26+
title:
27+
'Hold the Option/Alt key and drag the mouse to create partial screenshots.\nClick the icon to create full-page screenshots.',
28+
});
29+
30+
chrome.browserAction.onClicked.addListener(function () {
31+
chrome.tabs.captureVisibleTab(function (screenshotUrl) {
32+
if (!screenshotUrl) {
33+
return;
34+
}
35+
chrome.storage.sync.get(['download', 'openInTab'], (result) => {
36+
// download image
37+
if (result.download) {
38+
chrome.downloads.download({
39+
url: screenshotUrl,
40+
filename: `${new Date().getTime().toString()}.jpg`,
41+
});
42+
}
43+
44+
// see for yourself the screenshot during testing
45+
if (result.openInTab) {
46+
chrome.tabs.create({
47+
url: screenshotUrl,
48+
});
49+
}
50+
});
51+
});
52+
});
53+
54+
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
55+
if (request.msg === 'SCREENSHOT_WITH_COORDINATES') {
56+
let rect = request.rect;
57+
let windowSize = request.windowSize;
58+
chrome.tabs.captureVisibleTab(function (screenshotUrl) {
59+
if (!screenshotUrl) {
60+
return;
61+
}
62+
getImageDimensions(screenshotUrl).then(
63+
(imageDimensions: ImageDimension) => {
64+
let scale = imageDimensions.w / windowSize.width;
65+
let x = Math.floor(rect.x * scale);
66+
let y = Math.floor(rect.y * scale);
67+
let width = Math.floor(rect.width * scale);
68+
let height = Math.floor(rect.height * scale);
69+
imageClipper(screenshotUrl, function () {
70+
// @ts-ignore
71+
this.crop(x, y, width, height).toDataURL((dataUrl: string) => {
72+
chrome.storage.sync.get(['download', 'openInTab'], (result) => {
73+
// download image
74+
if (result.download) {
75+
chrome.downloads.download({
76+
url: dataUrl,
77+
filename: `${new Date().getTime().toString()}.jpg`,
78+
});
79+
}
80+
81+
// see for yourself the screenshot during testing
82+
if (result.openInTab) {
83+
chrome.tabs.create({
84+
url: dataUrl,
85+
});
86+
}
87+
});
88+
89+
// get dimensions
90+
// getImageDimensions(dataUrl).then((croppedImageDimensions) => {
91+
// let dimensions = {
92+
// trueWidth: croppedImageDimensions.w,
93+
// trueHeight: croppedImageDimensions.h,
94+
// rectWidth: rect.width,
95+
// rectHeight: rect.height,
96+
// rectX: rect.x,
97+
// rectY: rect.y,
98+
// };
99+
// console.log(dimensions);
100+
// });
101+
});
102+
});
103+
}
104+
);
105+
});
106+
}
107+
});

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var options = {
3737
mode: process.env.NODE_ENV || 'development',
3838
entry: {
3939
options: path.join(__dirname, 'src', 'pages', 'Options', 'index.tsx'),
40-
background: path.join(__dirname, 'src', 'pages', 'Background', 'index.js'),
40+
background: path.join(__dirname, 'src', 'pages', 'Background', 'index.ts'),
4141
contentScript: path.join(__dirname, 'src', 'pages', 'Content', 'index.ts'),
4242
},
4343
chromeExtensionBoilerplate: {

0 commit comments

Comments
 (0)