-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
96 lines (75 loc) · 2.63 KB
/
popup.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
/* eslint-disable prefer-destructuring */
/* global chrome window document */
const console = chrome.extension.getBackgroundPage().console;
const config = chrome.extension.getBackgroundPage().config;
const app = {
entities: {},
init() {
this.entities = config.entities;
// console.log("entities", config.entities);
this.entityComponent = function entityComponent(name) {
return `<div id="${name}-section" >`
+ `<label for="${name}-input " >${name}:</label>`
+ `<input id="${name}-input" name="${name}"`
+ 'class="border border-radius width-small height-small" type="text">'
+ `<button id="${name}-button" value="${name}" class="float-right">Go</button>`
+ '</div>';
};
this.mainpage = document.querySelector('#main'); // innerHTML(this.entityComponent());
Object.keys(this.entities).forEach((key) => {
// const value = this.entities[key];
this.mainpage.innerHTML += this.entityComponent(key);
});
},
openLink(element) {
console.log('OpenLink Function : ', this.entities);
const inputValue = element.value.trim();
// check for blank input
if (inputValue === '') {
element.classList.add('border-red');
element.classList.remove('border');
return;
}
// build url
const urlKey = element.name;
const pageUrl = this.entities[urlKey] + inputValue;
console.log('resourceType (Input Element name):', urlKey);
console.log('inputValue (Input Element value):', inputValue);
console.log('pageURL:', pageUrl);
chrome.storage.sync.get({
tabOption: 'new-tab', // this is a default in case there is no value in storage.
}, (items) => {
if (items.tabOption === 'current-tab') {
// use existing tab
chrome.tabs.update({
url: pageUrl,
});
} else {
// open new tab
chrome.tabs.create({
url: pageUrl,
});
}
window.close();
});
},
};
// app start
document.addEventListener('DOMContentLoaded', () => {
app.init();
document.addEventListener('keyup', (event) => {
// ignore enter key on non-input elements
if (!event.target.matches('input')) return;
if (event.key === 'Enter') {
app.openLink(event.target);
}
}, false);
document.addEventListener('click', (event) => {
// ignore clicks on non-buttons
if (!event.target.matches('button')) return;
event.preventDefault();
// get input element based on button value attribute
const inputElement = document.getElementById(`${event.target.value}-input`);
app.openLink(inputElement);
}, false);
});