2017-11-11 05:52:29 +01:00
|
|
|
document.addEventListener('DOMContentLoaded', (event) => {
|
2018-05-09 20:00:13 +02:00
|
|
|
let pageHref: string = null;
|
2018-05-18 23:05:46 +02:00
|
|
|
let filledThisHref = false;
|
|
|
|
let delayFillTimeout: number;
|
|
|
|
|
2018-05-09 14:39:12 +02:00
|
|
|
const isSafari = (typeof safari !== 'undefined') && navigator.userAgent.indexOf(' Safari/') !== -1 &&
|
|
|
|
navigator.userAgent.indexOf('Chrome') === -1;
|
2017-10-06 04:30:46 +02:00
|
|
|
|
2018-05-09 14:39:12 +02:00
|
|
|
if (isSafari) {
|
2018-09-27 17:07:17 +02:00
|
|
|
if ((window as any).__bitwardenFrameId == null) {
|
|
|
|
(window as any).__bitwardenFrameId = Math.floor(Math.random() * Math.floor(99999999));
|
|
|
|
}
|
2018-01-13 04:31:03 +01:00
|
|
|
const responseCommand = 'autofillerAutofillOnPageLoadEnabledResponse';
|
2019-08-16 02:31:23 +02:00
|
|
|
safari.extension.dispatchMessage('bitwarden', {
|
2018-01-13 16:13:31 +01:00
|
|
|
command: 'bgGetDataForTab',
|
2018-05-09 20:00:13 +02:00
|
|
|
responseCommand: responseCommand,
|
2018-09-27 17:07:17 +02:00
|
|
|
bitwardenFrameId: (window as any).__bitwardenFrameId,
|
2018-01-13 04:31:03 +01:00
|
|
|
});
|
2018-05-09 20:00:13 +02:00
|
|
|
safari.self.addEventListener('message', (msgEvent: any) => {
|
2019-08-20 19:06:38 +02:00
|
|
|
const msg = JSON.parse(msgEvent.message.msg);
|
2018-09-27 17:07:17 +02:00
|
|
|
if (msg.bitwardenFrameId != null && (window as any).__bitwardenFrameId !== msg.bitwardenFrameId) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-13 16:19:05 +01:00
|
|
|
if (msg.command === responseCommand && msg.data.autofillEnabled === true) {
|
2018-05-18 23:05:46 +02:00
|
|
|
setInterval(() => doFillIfNeeded(), 500);
|
|
|
|
} else if (msg.command === 'fillForm' && pageHref === msg.url) {
|
|
|
|
filledThisHref = true;
|
2018-01-13 04:11:18 +01:00
|
|
|
}
|
2018-01-13 04:31:03 +01:00
|
|
|
}, false);
|
|
|
|
return;
|
2018-04-14 04:54:28 +02:00
|
|
|
} else {
|
2018-01-13 16:19:05 +01:00
|
|
|
const enabledKey = 'enableAutoFillOnPageLoad';
|
2018-05-09 20:00:13 +02:00
|
|
|
chrome.storage.local.get(enabledKey, (obj: any) => {
|
|
|
|
if (obj != null && obj[enabledKey] === true) {
|
2018-05-18 23:05:46 +02:00
|
|
|
setInterval(() => doFillIfNeeded(), 500);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
chrome.runtime.onMessage.addListener((msg: any, sender: any, sendResponse: Function) => {
|
|
|
|
if (msg.command === 'fillForm' && pageHref === msg.url) {
|
|
|
|
filledThisHref = true;
|
2018-01-13 04:11:18 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-08-28 19:00:46 +02:00
|
|
|
|
2018-05-18 23:05:46 +02:00
|
|
|
function doFillIfNeeded(force: boolean = false) {
|
|
|
|
if (force || pageHref !== window.location.href) {
|
|
|
|
if (!force) {
|
|
|
|
// Some websites are slow and rendering all page content. Try to fill again later
|
|
|
|
// if we haven't already.
|
|
|
|
filledThisHref = false;
|
|
|
|
if (delayFillTimeout != null) {
|
|
|
|
window.clearTimeout(delayFillTimeout);
|
|
|
|
}
|
|
|
|
delayFillTimeout = window.setTimeout(() => {
|
|
|
|
if (!filledThisHref) {
|
|
|
|
doFillIfNeeded(true);
|
|
|
|
}
|
|
|
|
}, 1500);
|
|
|
|
}
|
|
|
|
|
2017-10-06 04:30:46 +02:00
|
|
|
pageHref = window.location.href;
|
2018-09-27 17:07:17 +02:00
|
|
|
const msg: any = {
|
2017-11-11 05:52:29 +01:00
|
|
|
command: 'bgCollectPageDetails',
|
2018-05-09 20:00:13 +02:00
|
|
|
sender: 'autofiller',
|
2018-01-13 04:11:18 +01:00
|
|
|
};
|
|
|
|
|
2018-05-09 14:39:12 +02:00
|
|
|
if (isSafari) {
|
2018-09-27 17:07:17 +02:00
|
|
|
msg.bitwardenFrameId = (window as any).__bitwardenFrameId;
|
2019-08-16 02:31:23 +02:00
|
|
|
safari.extension.dispatchMessage('bitwarden', msg);
|
2018-04-14 04:54:28 +02:00
|
|
|
} else {
|
2018-01-13 04:11:18 +01:00
|
|
|
chrome.runtime.sendMessage(msg);
|
|
|
|
}
|
2017-10-06 04:30:46 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-28 19:00:46 +02:00
|
|
|
});
|