-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgeneric.js
154 lines (133 loc) · 3.92 KB
/
generic.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
'use strict';
const DEFAULT_SETTINGS = {
markfields: true,
sendnotifications: true,
submitafterfill: true,
handleauthrequests: true,
defaultfolder: 'Account',
omitkeys: 'otpauth,totp',
defaultpasswordlength: 24,
appendlogintoname: false,
};
const LAST_DOMAIN_SEARCH_PREFIX = 'LAST_DOMAIN_SEARCH_';
const LAST_DETAIL_VIEW_PREFIX = 'LAST_DETAIL_VIEW_';
const REQUIRED_GOPASS_VERSION = [1, 8, 5];
let versionOK = undefined;
function checkVersion() {
if (versionOK) {
return Promise.resolve();
}
return sendNativeAppMessage({ type: 'getVersion' }).then((response) => {
let major = REQUIRED_GOPASS_VERSION[0];
let minor = REQUIRED_GOPASS_VERSION[1];
let patch = REQUIRED_GOPASS_VERSION[2];
if (
response.major > major ||
(response.major === major &&
(response.minor > minor || (response.minor === minor && response.patch >= patch)))
) {
versionOK = true;
console.log('Version is OK');
return Promise.resolve();
}
console.log('Version is not OK');
return Promise.reject(
new Error(`Please update gopass to version ${REQUIRED_GOPASS_VERSION.join('.')} or newer.`)
);
});
}
const URL_PATTERN = /^https?:\/\//i;
function getSettings() {
return browser.storage.sync.get(DEFAULT_SETTINGS);
}
function sendNativeAppMessage(message) {
const app = 'com.justwatch.gopass';
console.log(JSON.stringify(message));
return browser.runtime.sendNativeMessage(app, message);
}
function logError(error) {
console.log(error);
}
function makeAbsolute(string) {
if (!URL_PATTERN.test(string)) {
return 'https://' + string;
}
return string;
}
function openURL(url) {
return browser.tabs.create({ url: makeAbsolute(url) });
}
function openURLOnEvent(event) {
event.preventDefault();
openURL(event.target.href);
}
function executeOnSetting(setting, trueCallback, falseCallback) {
return getSettings().then((result) => {
if (result[setting]) {
if (trueCallback) trueCallback();
} else {
if (falseCallback) falseCallback();
}
}, logError);
}
function createButtonWithCallback(attributes, callback) {
const element = document.createElement('button');
Object.keys(attributes).forEach((attribute) => {
element[attribute] = attributes[attribute];
});
element.addEventListener('click', callback);
return element;
}
function urlDomain(urlString) {
try {
return new URL(urlString).hostname;
} catch (e) {
return 'localhost';
}
}
function setLocalStorageKey(key, value) {
const update = {};
update[key] = value;
console.log('setting local storage', update);
return browser.storage.local.set(update);
}
function getLocalStorageKey(key) {
console.log('getting local storage', key);
return browser.storage.local.get(key).then((values) => {
return values[key];
}, logError);
}
function showNotificationOnSetting(message) {
return executeOnSetting('sendnotifications', () => {
browser.notifications.create({
type: 'basic',
iconUrl: browser.runtime.getURL('icons/gopassbridge-96.png'),
title: 'gopassbridge',
message: message,
});
});
}
function getPopupUrl() {
return browser.runtime.getURL('gopassbridge.html');
}
function isChrome() {
return browser.runtime.getURL('/').startsWith('chrome');
}
if ('window' in globalThis) {
window.tests = {
generic: {
sendNativeAppMessage,
executeOnSetting,
urlDomain,
setLocalStorageKey,
getLocalStorageKey,
createButtonWithCallback,
showNotificationOnSetting,
getPopupUrl,
isChrome,
openURLOnEvent,
makeAbsolute,
checkVersion,
},
};
}