bitwarden-estensione-browser/src/notification/bar.js

141 lines
5.3 KiB
JavaScript
Raw Normal View History

2018-04-11 04:05:23 +02:00
require('./bar.scss');
2018-04-14 04:54:28 +02:00
document.addEventListener('DOMContentLoaded', () => {
var i18n = {};
var lang = window.navigator.language;
if (typeof safari !== 'undefined') {
2018-01-13 21:09:05 +01:00
const responseCommand = 'notificationBarFrameDataResponse';
sendPlatformMessage({
command: 'bgGetDataForTab',
responseCommand: responseCommand
});
2018-04-14 04:54:28 +02:00
safari.self.addEventListener('message', (msgEvent) => {
2019-08-20 19:06:38 +02:00
const msg = JSON.parse(msgEvent.message.msg);
2018-01-13 21:09:05 +01:00
if (msg.command === responseCommand && msg.data) {
i18n = msg.data.i18n;
load();
}
}, false);
2018-04-14 04:54:28 +02:00
} else {
i18n.appName = chrome.i18n.getMessage('appName');
i18n.close = chrome.i18n.getMessage('close');
i18n.yes = chrome.i18n.getMessage('yes');
i18n.never = chrome.i18n.getMessage('never');
i18n.notificationAddSave = chrome.i18n.getMessage('notificationAddSave');
i18n.notificationNeverSave = chrome.i18n.getMessage('notificationNeverSave');
i18n.notificationAddDesc = chrome.i18n.getMessage('notificationAddDesc');
2018-08-01 05:24:11 +02:00
i18n.notificationChangeSave = chrome.i18n.getMessage('notificationChangeSave');
i18n.notificationChangeDesc = chrome.i18n.getMessage('notificationChangeDesc');
lang = chrome.i18n.getUILanguage();
// delay 50ms so that we get proper body dimensions
setTimeout(load, 50);
}
2017-11-10 04:38:09 +01:00
function load() {
var closeButton = document.getElementById('close-button'),
2017-11-10 04:38:09 +01:00
body = document.querySelector('body'),
bodyRect = body.getBoundingClientRect();
2017-09-19 18:30:19 +02:00
2017-11-10 04:38:09 +01:00
// i18n
body.classList.add('lang-' + lang.slice(0, 2));
document.getElementById('logo-link').title = i18n.appName;
closeButton.title = i18n.close;
2019-10-09 16:12:06 +02:00
closeButton.setAttribute('aria-label', i18n.close);
2017-11-10 04:38:09 +01:00
if (bodyRect.width < 768) {
document.querySelector('#template-add .add-save').textContent = i18n.yes;
document.querySelector('#template-add .never-save').textContent = i18n.never;
2018-08-01 05:24:11 +02:00
document.querySelector('#template-change .change-save').textContent = i18n.yes;
2018-04-14 04:54:28 +02:00
} else {
document.querySelector('#template-add .add-save').textContent = i18n.notificationAddSave;
document.querySelector('#template-add .never-save').textContent = i18n.notificationNeverSave;
2018-08-01 05:24:11 +02:00
document.querySelector('#template-change .change-save').textContent = i18n.notificationChangeSave;
2017-11-10 04:38:09 +01:00
}
document.querySelector('#template-add .add-text').textContent = i18n.notificationAddDesc;
2018-08-01 05:24:11 +02:00
document.querySelector('#template-change .change-text').textContent = i18n.notificationChangeDesc;
2017-11-10 04:38:09 +01:00
if (getQueryVariable('add')) {
setContent(document.getElementById('template-add'));
2017-11-10 04:38:09 +01:00
var addButton = document.querySelector('#template-add-clone .add-save'),
neverButton = document.querySelector('#template-add-clone .never-save');
2018-04-14 04:54:28 +02:00
addButton.addEventListener('click', (e) => {
2017-11-10 04:38:09 +01:00
e.preventDefault();
sendPlatformMessage({
2017-11-10 04:38:09 +01:00
command: 'bgAddSave'
});
});
2018-04-14 04:54:28 +02:00
neverButton.addEventListener('click', (e) => {
2017-11-10 04:38:09 +01:00
e.preventDefault();
sendPlatformMessage({
2017-11-10 04:38:09 +01:00
command: 'bgNeverSave'
});
});
2018-08-01 05:24:11 +02:00
} else if (getQueryVariable('change')) {
setContent(document.getElementById('template-change'));
var changeButton = document.querySelector('#template-change-clone .change-save');
changeButton.addEventListener('click', (e) => {
e.preventDefault();
sendPlatformMessage({
command: 'bgChangeSave'
});
});
2018-04-14 04:54:28 +02:00
} else if (getQueryVariable('info')) {
2017-11-10 04:38:09 +01:00
setContent(document.getElementById('template-alert'));
document.getElementById('template-alert-clone').textContent = getQueryVariable('info');
}
2018-04-14 04:54:28 +02:00
closeButton.addEventListener('click', (e) => {
e.preventDefault();
sendPlatformMessage({
2017-11-10 04:38:09 +01:00
command: 'bgCloseNotificationBar'
});
});
sendPlatformMessage({
2017-11-10 04:38:09 +01:00
command: 'bgAdjustNotificationBar',
data: {
height: body.scrollHeight
}
});
2017-11-10 04:38:09 +01:00
}
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (pair[0] === variable) {
return pair[1];
}
}
return null;
}
function setContent(element) {
const content = document.getElementById('content');
while (content.firstChild) {
content.removeChild(content.firstChild);
}
var newElement = element.cloneNode(true);
newElement.id = newElement.id + '-clone';
content.appendChild(newElement);
}
function sendPlatformMessage(msg) {
if (typeof safari !== 'undefined') {
2019-08-27 14:03:55 +02:00
safari.extension.dispatchMessage('bitwarden', msg);
2018-04-14 04:54:28 +02:00
} else {
chrome.runtime.sendMessage(msg);
}
}
});