-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
76 lines (69 loc) · 1.83 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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
"use strict";
let body = document.body;
const defaultConfig = {
selector: 'table',
label: '说明',
prop: '参数名'
}
let config = {};
let host = ''
window.onload = function () {
const form = document.getElementById("form");
chrome.tabs.getSelected(null, function (tab) {
host = new URL(tab.url).host
document.getElementById('footer').innerText = host
chrome.storage.sync.get([host], function(result) {
config = Object.assign({}, defaultConfig, result[host])
console.log(config)
form.innerHTML = renderTemplate(config);
});
});
document.getElementById("submit").addEventListener("click", saveChange);
};
function renderTemplate(config) {
return Object.keys(config).reduce((pre, key) => {
return (
pre +
`
<div class="form-item">
<label class="form-item__label">${key}</label>
<input class="form-item__input" id="${key}" value="${config[key]}">
</div>
`
);
}, "");
}
function saveChange(e) {
e.preventDefault();
let prop = ''
Array.from(document.getElementsByClassName("form-item__input")).forEach(
(el) => {
config[el.id] = el.value
if (el.id === 'prop') {
prop = el.value
}
}
);
if (!prop) {
document.getElementById('error-tip').innerHTML = 'prop不能为空'
return
} else {
document.getElementById('error-tip').innerHTML = ''
chrome.tabs.query(
{
active: true,
currentWindow: true,
},
(tabs) => {
let data = {}
data[host] = config
chrome.storage.sync.set(data)
chrome.tabs.sendMessage(tabs[0].id, { ...config });
setTimeout(window.close, 10)
}
);
}
}