--- /dev/null
+const { app } = require('electron').remote;
+var messagePort;
+var appId;
+var pins;
+var verified = false;
+
+chrome.storage.local.get(null, function(items){
+ pins = items;
+});
+
+chrome.runtime.onConnect.addListener(function(port) {
+ messagePort = port;
+ messagePort.onMessage.addListener(function(message) {
+ console.log('background: ' + message);
+
+ var messages = message.split(':');
+ switch (messages[0]) {
+ case 'id':
+ if (!verified) {
+ appId = messages[1];
+ for (var id in pins) {
+ if (id === appId) {
+ if (pins[appId] === 'skip') {
+ messagePort.postMessage('verified');
+ verified = true;
+ } else {
+ messagePort.postMessage('show:verify');
+ }
+ return;
+ }
+ }
+ messagePort.postMessage('show:register');
+ }
+ break;
+ case 'register':
+ var store = {};
+ store[appId] = messages[1];
+ chrome.storage.local.set(store, function() {
+ messagePort.postMessage('verified');
+ verified = true;
+ });
+ break;
+ case 'verify':
+ if (pins[appId] === messages[1]) {
+ messagePort.postMessage('verified');
+ verified = true;
+ } else {
+ messagePort.postMessage('wrongpin');
+ }
+ break;
+ case 'skip':
+ var store = {};
+ store[appId] = 'skip';
+ chrome.storage.local.set(store, function() {
+ messagePort.postMessage('verified');
+ verified = true;
+ });
+ break;
+ case 'exit':
+ app.quit();
+ break;
+ default:
+ break;
+ }
+ });
+});
--- /dev/null
+const MODAL_HTML = '<div id="asModal" class="modal"> \
+ <div class="modal-content"> \
+ <span class="close">×</span> \
+ <div class="modal-centered"> \
+ <p id="asTitle"></p> \
+ <input id="asPin" type="password" name="pin" pattern="[0-9]{4}"" maxlength="4"> \
+ <input id="asSubmit" type="submit" value="Submit"> \
+ <input id="asSkip" type="checkbox" value="Skip">Skip this app \
+ </div> \
+ </div> \
+ </div>';
+
+document.body.insertAdjacentHTML('beforeend', MODAL_HTML);
+
+var modalType = 'register';
+var messagePort = chrome.runtime.connect();
+var modal = document.getElementById('asModal');
+var title = document.getElementById('asTitle');
+var pin = document.getElementById('asPin');
+var submit = document.getElementById('asSubmit');
+var skip = document.getElementById('asSkip');
+var close = document.getElementsByClassName('close')[0];
+
+messagePort.onMessage.addListener(function(message) {
+ console.log('content: ' + message);
+
+ var messages = message.split(':');
+ switch (messages[0]) {
+ case 'show':
+ modalType = messages[1];
+ showModal();
+ break;
+ case 'verified':
+ hideModal();
+ break;
+ case 'wrongpin':
+ alert('Please check the pin.');
+ pin.value = '';
+ pin.focus();
+ default:
+ break;
+ }
+});
+
+submit.onclick = function() {
+ if (asSkip.checked)
+ messagePort.postMessage('skip');
+ else if (pin.value.length === 4)
+ messagePort.postMessage(modalType + ':' + pin.value);
+}
+
+close.onclick = function() {
+ if (asSkip.checked)
+ messagePort.postMessage('skip');
+ else
+ messagePort.postMessage('exit');
+ hideModal();
+};
+
+function showModal() {
+ if (modalType === 'register') {
+ title.textContent = 'New PIN';
+
+ } else if (modalType === 'verify') {
+ title.textContent = 'Enter PIN';
+ skip.disabled = true;
+ }
+
+ modal.style.display = 'block';
+}
+
+function hideModal() {
+ modal.style.display = 'none';
+}
+
+var urls = location.href.split('/');
+console.log('id:' + urls[urls.length - 2]);
+messagePort.postMessage('id:' + urls[urls.length - 2]);
--- /dev/null
+/* The Modal (background) */
+.modal {
+ display: none; /* Hidden by default */
+ position: fixed; /* Stay in place */
+ z-index: 1; /* Sit on top */
+ padding-top: 100px; /* Location of the box */
+ left: 0;
+ top: 0;
+ width: 100%; /* Full width */
+ height: 100%; /* Full height */
+ overflow: auto; /* Enable scroll if needed */
+ background-color: rgb(0,0,0); /* Fallback color */
+ background-color: rgba(0,0,0,0.8); /* Black w/ opacity */
+}
+
+/* Modal Content */
+.modal-content {
+ background-color: #fefefe;
+ margin: auto;
+ padding: 20px;
+ border: 1px solid #888;
+ width: 50%;
+}
+
+.modal-centered {
+ vertical-align: middle;
+ text-align: center;
+ clear: right;
+}
+
+/* The Close Button */
+.close {
+ color: #aaaaaa;
+ float: right;
+ font-size: 28px;
+ font-weight: bold;
+}
+
+.close:hover,
+.close:focus {
+ color: #000;
+ text-decoration: none;
+ cursor: pointer;
+}