-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
92 lines (82 loc) · 2.45 KB
/
script.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
chrome.browserAction.onClicked.addListener(emailTabs);
// Display options when extension updates.
chrome.runtime.onInstalled.addListener(function(details){
if (details.reason == chrome.runtime.OnInstalledReason.UPDATE){
chrome.tabs.create({url: "options.html"});
}
});
/*
* Create new mailto tab.
*/
function emailTabs() {
if(localStorage["legacy"]=="true"){
chrome.tabs.getAllInWindow(null, function(tabs){
tabsToEmailLink(tabs);
});
}
else {
chrome.tabs.getAllInWindow(null, function(tabs){
tabsToClipboard(tabs);
});
}
}
/*
* Convert tabs to HTML links.
*/
function tabsToClipboard(tabs) {
var text = '';
for(i=0; i<tabs.length; i++){
text += "<a href='" + tabs[i].url + "'>" + tabs[i].title + "</a><br>";
}
var defaultSignature = "--Sent with <a href='https://chrome.google.com/webstore/detail/email-all-tabs/"
+ "hgebccnmgpigdgkbenjkamcnioejlghh'>Email all Tabs</a>";
text += localStorage["signature"] || defaultSignature;
copyToClipboard(text);
chrome.tabs.create({ url: "mailto:?body=Paste%20copied%20links%20here" });
}
// Copy text to clipboard, from the internet.
function copyToClipboard(text){
var copyDiv = document.createElement('div');
copyDiv.contentEditable = true;
document.body.appendChild(copyDiv);
copyDiv.innerHTML = text;
copyDiv.unselectable = "off";
copyDiv.focus();
document.execCommand('SelectAll');
document.execCommand("Copy", false, null);
document.body.removeChild(copyDiv);
}
/*
* Goes through all tabs and adds URL to body of email.
*/
function tabsToEmailLink(tabs) {
var text = '';
var maxTextUrl = 1500;
var skippedLinks = false;
for(i=0; i<tabs.length; i++){
if(text.length += tabs[i].url.length < maxTextUrl){
text += tabs[i].url + "\n";
}
else {
skippedLinks = true;
}
}
var signature = localStorage["signature"] || "--Sent with Email all Tabs";
if(text.length + signature.length < maxTextUrl) {
text += signature;
}
var encodedString = encodeURIComponent(text);
var maxEncodedUrl = 1900;
if(encodedString.length > maxEncodedUrl){
skippedLinks = true;
encodedString = shortenText(encodedString, "%0A", maxEncodedUrl);
}
if(skippedLinks) {
encodedString += "%0A ..shortened";
}
chrome.tabs.create({ url: "mailto:?body=" + encodedString});
}
function shortenText(text, cutoff, max) {
var shortened = text.substring(0, max);
return shortened.substring(0, shortened.lastIndexOf(cutoff));
}