This repository was archived by the owner on Feb 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunlockedState.js
170 lines (149 loc) · 4.3 KB
/
unlockedState.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"use strict";
import { ChromePromiseApi } from '$lib/chrome-api-promise.js'
import { parseUrl } from '$lib/utils.js'
const chromePromise = ChromePromiseApi()
/**
* Shared state and methods for an unlocked password file.
*/
function UnlockedState($router, keepassReference, protectedMemory, settings, notifications) {
var my = {
tabId: "", //tab id of current tab
url: "", //url of current tab
title: "", //window title of current tab
origin: "", //url of current tab without path or querystring
sitePermission: false, //true if the extension already has rights to autofill the password
cache: {}, // a secure cache that refreshes when values are set or fetched
clipboardStatus: "" //status message about clipboard, used when copying password to the clipboard
};
var copyEntry;
var copyPart;
var cacheTimeoutId;
//determine current url:
my.getTabDetails = function () {
return new Promise(function (resolve, reject) {
chrome.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
if (tabs && tabs.length) {
my.tabId = tabs[0].id;
var url = tabs[0].url.split('?');
my.url = url[0];
my.title = tabs[0].title;
var parsedUrl = parseUrl(tabs[0].url);
my.origin = parsedUrl.protocol + '//' + parsedUrl.hostname + '/';
chromePromise.permissions.contains({
origins: [my.origin]
})
.then(function () {
my.sitePermission = true;
})
.catch(function (err) {
my.sitePermission = false;
})
.then(function () {
resolve();
})
} else {
reject(new Error("Unable to determine tab details"));
}
});
});
};
my.clearCache = function () {
// Destroys an object in memory.
function destroy(obj) {
for (var prop in obj) {
var property = obj[prop];
if (property != null && typeof (property) == 'object') {
destroy(property);
} else {
obj[prop] = null;
}
}
}
destroy(my.cache)
my.cache = {}
}
my.cacheSet = function (key, val) {
// Refresh cache
clearTimeout(cacheTimeoutId)
cacheTimeoutId = setTimeout(function () {
my.clearCache()
window.close()
}, 120000);
my.cache[key] = val;
}
my.cacheGet = function (key) {
// Refresh cache
clearTimeout(cacheTimeoutId)
cacheTimeoutId = setTimeout(function () {
my.clearCache()
window.close()
}, 120000);
return my.cache[key];
}
my.clearClipboardState = function () {
my.clipboardStatus = "";
}
setTimeout(my.clearClipboardState, 60000); //clear backgroundstate after 1 minutes live - we should never be alive that long
my.autofill = function (entry) {
chrome.runtime.sendMessage({
m: "requestPermission",
perms: {
origins: [my.origin]
},
then: {
m: "autofill",
tabId: my.tabId,
u: entry.userName,
p: getAttribute(entry, 'password'),
o: my.origin
}
});
window.close(); //close the popup
}
//get clear-text password from entry
function getAttribute(entry, attr = 'password') {
return my.getDecryptedAttribute(entry, attr);
}
my.copyPassword = function (entry) {
copyPart = 'password';
copyEntry = entry;
document.execCommand('copy');
}
my.copyUsername = function (entry) {
copyPart = 'userName';
copyEntry = entry;
document.execCommand('copy');
}
my.gotoDetails = function (entry) {
$router.route('/entry-details/' + entry.id);
}
my.getDecryptedAttribute = function (entry, attributeName) {
return keepassReference.getFieldValue(entry, attributeName, my.cache.allEntries);
}
//listens for the copy event and does the copy
document.addEventListener('copy', function (e) {
if (!copyEntry && !copyPart) {
return; //listener can get registered multiple times
}
var textToPutOnClipboard = getAttribute(copyEntry, copyPart);
var fieldName = copyPart.charAt(0).toUpperCase() + copyPart.slice(1); // https://stackoverflow.com/a/1026087
copyEntry = null;
copyPart = null;
e.clipboardData.setData('text/plain', textToPutOnClipboard);
e.preventDefault();
settings.getSetClipboardExpireInterval().then(interval => {
settings.setForgetTime('clearClipboard', Date.now() + interval * 60000);
notifications.push({
text: fieldName + ' copied to clipboard. Clipboard will clear in ' + interval + ' minute(s).',
type: 'clipboard',
}).then(() => window.close())
})
});
return my;
}
export {
UnlockedState
}