This commit is contained in:
parent
96b13e4b7f
commit
846dc6f8a4
7 changed files with 175 additions and 10 deletions
37
data/options/index.html
Normal file
37
data/options/index.html
Normal file
|
@ -0,0 +1,37 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>My Test Extension Options</title>
|
||||
<style>
|
||||
body { padding: 10px; }
|
||||
textarea { width: 100%; }
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<table width=100%>
|
||||
<tr>
|
||||
<td>
|
||||
<label><input type="radio" name="mode" value="blacklist" id="mode-blacklist"> Black-list mode: Apply user-agent string to all tabs except the tabs with the following top-level hostnames (comma separated list of hostnames)</label>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><textarea id="blacklist" rows="3" placeholder="e.g.: www.google.com, www.bing.com"></textarea></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<label><input type="radio" name="mode" value="whitelist" id="mode-whitelist"> White-list mode: Only apply user-agent string to the tabs with following top-level hostnames</label>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><textarea id="whitelist" rows="3"></textarea></td>
|
||||
</tr>
|
||||
</table>
|
||||
<p>
|
||||
<button id="save">Save</button>
|
||||
<span id="status"></span>
|
||||
</p>
|
||||
|
||||
<script src="index.js"></script>
|
||||
</body>
|
||||
</html>
|
36
data/options/index.js
Normal file
36
data/options/index.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
'use strict';
|
||||
function prepare(str) {
|
||||
return str.split(/\s*,\s*/)
|
||||
.map(s => s.replace('http://', '')
|
||||
.replace('https://', '').split('/')[0].trim())
|
||||
.filter((h, i, l) => h && l.indexOf(h) === i);
|
||||
}
|
||||
|
||||
function save() {
|
||||
chrome.storage.local.set({
|
||||
blacklist: prepare(document.getElementById('blacklist').value),
|
||||
whitelist: prepare(document.getElementById('whitelist').value),
|
||||
mode: document.getElementById('mode-blacklist').checked ? 'blacklist' : 'whitelist'
|
||||
}, () => {
|
||||
restore();
|
||||
// Update status to let user know options were saved.
|
||||
const status = document.getElementById('status');
|
||||
status.textContent = 'Options saved.';
|
||||
setTimeout(() => status.textContent = '', 750);
|
||||
});
|
||||
}
|
||||
|
||||
function restore() {
|
||||
chrome.storage.local.get({
|
||||
mode: 'blacklist',
|
||||
whitelist: [],
|
||||
blacklist: []
|
||||
}, prefs => {
|
||||
document.getElementById('mode-blacklist').checked = prefs.mode === 'blacklist';
|
||||
document.getElementById('mode-whitelist').checked = prefs.mode === 'whitelist';
|
||||
document.getElementById('blacklist').value = prefs.blacklist.join(', ');
|
||||
document.getElementById('whitelist').value = prefs.whitelist.join(', ');
|
||||
});
|
||||
}
|
||||
document.addEventListener('DOMContentLoaded', restore);
|
||||
document.getElementById('save').addEventListener('click', save);
|
|
@ -110,3 +110,10 @@ input[type=button]:disabled {
|
|||
padding: 10px 0;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
#info {
|
||||
padding: 0 5px;
|
||||
}
|
||||
#info:empty {
|
||||
display: none;
|
||||
}
|
||||
|
|
|
@ -152,6 +152,7 @@
|
|||
<div hbox id="agent" pack="center" align="center">
|
||||
User-Agent String:
|
||||
<input id="ua" type="text" name="">
|
||||
<span id="info"></span>
|
||||
<input type="button" value="Apply" title="Set this string as the browser's User-Agent string" data-cmd="apply">
|
||||
<input type="button" value="Reset" title="Reset User-Agent string to the default one" style="margin-left: 2px;" data-cmd="reset">
|
||||
</div>
|
||||
|
|
|
@ -142,13 +142,26 @@ window.addEventListener('load', () => {
|
|||
}, 100);
|
||||
});
|
||||
|
||||
function msg(msg) {
|
||||
const info = document.getElementById('info');
|
||||
info.textContent = msg;
|
||||
window.setTimeout(() => info.textContent = '', 750);
|
||||
}
|
||||
|
||||
// commands
|
||||
document.addEventListener('click', ({target}) => {
|
||||
const cmd = target.dataset.cmd;
|
||||
if (cmd) {
|
||||
if (cmd === 'apply') {
|
||||
const value = document.getElementById('ua').value;
|
||||
if (value === navigator.userAgent) {
|
||||
msg('Default user-agent');
|
||||
}
|
||||
else {
|
||||
msg('user-agent is set');
|
||||
}
|
||||
chrome.storage.local.set({
|
||||
ua: document.getElementById('ua').value
|
||||
ua: value === navigator.userAgent ? '' : value
|
||||
});
|
||||
}
|
||||
else if (cmd === 'reset') {
|
||||
|
@ -159,6 +172,7 @@ document.addEventListener('click', ({target}) => {
|
|||
chrome.storage.local.set({
|
||||
ua: ''
|
||||
});
|
||||
msg('reset to default');
|
||||
}
|
||||
else if (cmd === 'refresh') {
|
||||
chrome.tabs.query({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue