-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
140 lines (107 loc) · 3.19 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2012,2013 Peter Coles - http://mrcoles.com/ - All rights reserved.
// Use of this source code is governed by the MIT License found in LICENSE
//
// State fields
//
var currentTab, // result of chrome.tabs.query of current active tab
resultWindowId; // window id for putting resulting images
//
// Utility methods
//
function $(id) { return document.getElementById(id); }
function show(id) { $(id).style.display = 'block'; }
function hide(id) { $(id).style.display = 'none'; }
function getFilename(contentURL) {
var name = contentURL.split('?')[0].split('#')[0];
if (name) {
name = name
.replace(/^https?:\/\//, '')
.replace(/[^A-z0-9]+/g, '-')
.replace(/-+/g, '-')
.replace(/^[_\-]+/, '')
.replace(/[_\-]+$/, '');
name = '__' + name;
} else {
name = '';
}
// Adjust the date format
var today = new Date();
var yyyy = today.getFullYear();
var mm = today.getMonth()+1; //January is 0!
var dd = today.getDate();
if(dd < 10) {
dd='0'+dd
}
if(mm < 10) {
mm='0'+mm
}
today = yyyy+'-'+mm+'-'+dd;
// The full file name
return today + '-screenshot' + name + '.png';
}
//
// Capture Handlers
//
function displayCaptures(filenames) {
if (!filenames || !filenames.length) {
show('uh-oh');
return;
}
_displayCapture(filenames);
}
function _displayCapture(filenames, index) {
index = index || 0;
var filename = filenames[index];
var last = index === filenames.length - 1;
if (currentTab.incognito && index === 0) {
// cannot access file system in incognito, so open in non-incognito
// window and add any additional tabs to that window.
//
// we have to be careful with focused too, because that will close
// the popup.
chrome.windows.create({
url: filename,
incognito: false,
focused: last
}, function(win) {
resultWindowId = win.id;
});
} else {
chrome.tabs.create({
url: filename,
active: last,
windowId: resultWindowId,
openerTabId: currentTab.id,
index: (currentTab.incognito ? 0 : currentTab.index) + 1 + index
});
}
if (!last) {
_displayCapture(filenames, index + 1);
}
}
function errorHandler(reason) {
show('uh-oh'); // TODO - extra uh-oh info?
}
function progress(complete) {
if (complete === 0) {
// Page capture has just been initiated.
show('loading');
}
else {
// $('bar').style.width = ;
document.getElementById('progressCounter').innerHTML = parseInt(complete * 100, 10) + '%';
}
}
function splitnotifier() {
show('split-image');
}
//
// start doing stuff immediately! - including error cases
//
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var tab = tabs[0];
currentTab = tab; // used in later calls to get tab info
var filename = getFilename(tab.url);
CaptureAPI.captureToFiles(tab, filename, displayCaptures,
errorHandler, progress, splitnotifier);
});