Skip to content

Commit 2124e8d

Browse files
committed
v0.0.1
1 parent 2457f67 commit 2124e8d

29 files changed

+293
-265
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
image-clipper.js

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "screenshot-chrome-extension",
2+
"name": "screenshot-extension",
33
"version": "0.0.1",
4-
"description": "A chrome extension that takes screenshots",
4+
"description": "A browser extension that takes screenshots",
55
"license": "MIT",
66
"repository": {
77
"type": "git",

src/assets/img/icon-128.png

100755100644
-9.08 KB
Loading

src/assets/img/icon-34.png

100755100644
-197 Bytes
Loading

src/assets/img/logo.svg

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

src/manifest.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
{
2-
"name": "Chrome Extension with React & Webpack",
3-
"options_page": "options.html",
2+
"name": "Screenshot Extension",
43
"background": {
54
"page": "background.html"
65
},
76
"browser_action": {
8-
"default_popup": "popup.html",
97
"default_icon": "icon-34.png"
108
},
11-
"chrome_url_overrides": {
12-
"newtab": "newtab.html"
9+
"options_ui": {
10+
"page": "options.html",
11+
"open_in_tab": false
1312
},
1413
"icons": {
1514
"128": "icon-128.png"
1615
},
17-
"content_scripts": [{
18-
"matches": ["http://*/*", "https://*/*", "<all_urls>"],
19-
"js": ["contentScript.bundle.js"],
20-
"css": ["content.styles.css"]
21-
}],
16+
"permissions": ["tabs", "downloads", "<all_urls>", "storage"],
17+
"content_scripts": [
18+
{
19+
"matches": ["http://*/*", "https://*/*", "<all_urls>"],
20+
"js": ["contentScript.bundle.js"],
21+
"css": ["content.styles.css"]
22+
}
23+
],
2224
"web_accessible_resources": [
2325
"content.styles.css",
2426
"icon-128.png",
2527
"icon-34.png"
2628
],
2729
"manifest_version": 2,
2830
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
29-
}
31+
}

src/pages/Background/image-clipper.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pages/Background/index.js

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,88 @@
11
import '../../assets/img/icon-34.png';
22
import '../../assets/img/icon-128.png';
3+
import imageClipper from './image-clipper.js';
34

4-
console.log('This is the background page.');
5-
console.log('Put the background scripts here.');
5+
const getImageDimensions = (file) => {
6+
return new Promise(function (resolved, rejected) {
7+
var img = new Image();
8+
img.onload = function () {
9+
resolved({ w: img.width, h: img.height });
10+
};
11+
img.src = file;
12+
});
13+
};
14+
15+
chrome.browserAction.setTitle({
16+
title:
17+
'Hold the Option/Alt key and drag the mouse to create partial screenshots.\nClick the icon to create full-page screenshots.',
18+
});
19+
20+
chrome.browserAction.onClicked.addListener(function () {
21+
chrome.tabs.captureVisibleTab(function (screenshotUrl) {
22+
chrome.storage.sync.get(['download', 'openInTab'], (result) => {
23+
// download image
24+
if (result.download) {
25+
chrome.downloads.download({
26+
url: screenshotUrl,
27+
filename: `${new Date().getTime().toString()}.jpg`,
28+
});
29+
}
30+
31+
// see for yourself the screenshot during testing
32+
if (result.openInTab) {
33+
chrome.tabs.create({
34+
url: screenshotUrl,
35+
});
36+
}
37+
});
38+
});
39+
});
40+
41+
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
42+
if (request.msg === 'SCREENSHOT_WITH_COORDINATES') {
43+
let rect = request.rect;
44+
let windowSize = request.windowSize;
45+
chrome.tabs.captureVisibleTab(function (screenshotUrl) {
46+
getImageDimensions(screenshotUrl).then((imageDimensions) => {
47+
let scale = imageDimensions.w / windowSize.width;
48+
let x = Math.floor(rect.x * scale);
49+
let y = Math.floor(rect.y * scale);
50+
let width = Math.floor(rect.width * scale);
51+
let height = Math.floor(rect.height * scale);
52+
imageClipper(screenshotUrl, function () {
53+
this.crop(x, y, width, height).toDataURL((dataUrl) => {
54+
chrome.storage.sync.get(['download', 'openInTab'], (result) => {
55+
// download image
56+
if (result.download) {
57+
chrome.downloads.download({
58+
url: dataUrl,
59+
filename: `${new Date().getTime().toString()}.jpg`,
60+
});
61+
}
62+
63+
// see for yourself the screenshot during testing
64+
if (result.openInTab) {
65+
chrome.tabs.create({
66+
url: dataUrl,
67+
});
68+
}
69+
});
70+
71+
// get dimensions
72+
// getImageDimensions(dataUrl).then((croppedImageDimensions) => {
73+
// let dimensions = {
74+
// trueWidth: croppedImageDimensions.w,
75+
// trueHeight: croppedImageDimensions.h,
76+
// rectWidth: rect.width,
77+
// rectHeight: rect.height,
78+
// rectX: rect.x,
79+
// rectY: rect.y,
80+
// };
81+
// console.log(dimensions);
82+
// });
83+
});
84+
});
85+
});
86+
});
87+
}
88+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#screenshot-bbox {
2+
border: 1px dashed red;
3+
}
4+
5+
.no-select {
6+
-webkit-touch-callout: none;
7+
-webkit-user-select: none;
8+
-khtml-user-select: none;
9+
-moz-user-select: none;
10+
-ms-user-select: none;
11+
user-select: none;
12+
}

0 commit comments

Comments
 (0)