building the UA objects from multiple JSON lists to improve popup's startup performance

This commit is contained in:
Ray Lothian 2019-03-12 10:50:52 +01:00
parent 1a3038ed05
commit 7cfa8fb4db
300 changed files with 454 additions and 252 deletions

View file

@ -0,0 +1 @@
../../../node/browsers/

View file

@ -0,0 +1,194 @@
[hbox] {
display: flex;
flex-direction: row;
}
[vbox] {
display: flex;
flex-direction: column;
}
[flex="1"] {
flex: 1;
}
[pack=center] {
justify-content: center;
}
[align=center] {
align-items: center;
}
[pack=end] {
justify-content: flex-end;
}
[align=end] {
align-items: flex-end;
}
body {
background-color: #fff;
font-family: "Helvetica Neue",Helvetica,sans-serif;
font-size: 13px;
width: 600px;
}
table {
width: 100%;
border-collapse: collapse;
}
fieldset {
border: solid 1px #ccc;
}
input[type=search],
input[type=text] {
width: 100%;
margin-right: 2px;
text-indent: 5px;
padding-right: 5px;
}
input {
outline: none;
background-color: #fff;
color: #000;
border: solid 1px #e7e7e7;
box-sizing: border-box;
height: 24px;
border-radius: 0;
font-size: 11px;
}
input[type=button] {
cursor: pointer;
min-width: 100px;
}
input[type=button]:active {
opacity: 0.5;
}
input[type=button]:disabled {
opacity: 0.2;
cursor: default;
}
select {
-webkit-appearance: none;
-moz-appearance: none;
border: none;
user-select: none;
outline: none;
color: #000;
background: rgba(255,255,255,.5) url(list.svg) no-repeat center right 4px;
background-size: 8px;
font-size: 13px;
border-radius: 0;
padding: 2px 16px 2px 4px;
}
#list {
overflow: auto;
height: 300px;
margin-bottom: 10px;
color: #000;
background-color: #fdfafa;
background-position: top 120px center;
background-repeat: no-repeat;
}
#list[data-loading=true] {
background-image: url(loading.gif);
background-size: 64px;
}
#list table {
table-layout: fixed;
}
#list th {
height: 30px;
color: #000;
background-color: #e7e7e7;
white-space: nowrap;
padding-left: 10px;
}
#list tr {
cursor: pointer;
}
#list tr[data-matched=false] {
opacity: 0.5;
}
#list tbody {
position: relative;
}
#list tbody tr:nth-child(odd) {
color: #000;
background-color: #fff;
}
#list tbody tr:nth-child(even) {
color: #000;
background-color: #f5f5f5;
}
#list[data-loading=false] tbody:empty::before {
content: 'no user-agent string for this query!';
display: block;
position: absolute;
top: 50px;
left: 10px;
}
#list td:nth-child(1) {
text-align: center;
}
#list td {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding: 3px 5px;
}
#tools input {
width: 100px;
margin: 3px 0 0 5px;
}
#filter td:first-child {
width: 100px;
}
#filter th:last-of-type {
text-align: right;
padding-right: 10px;
}
#agent {
padding: 10px 0;
white-space: nowrap;
}
#info {
padding: 0 5px;
}
#info:empty {
display: none;
}
[data-cmd="refresh"] {
margin-left: 2px;
margin-right: 2px;
}
[data-cmd="window"],
[data-cmd="apply"] {
color: #fff;
background-color: #3c923c;
border: solid 1px #327932;
margin-right: 2px;
margin-left: 2px;
}
[data-cmd="reset"] {
color: #fff;
background-color: #eea345;
border: solid 1px #ec9730;
}
[data-cmd="reload"],
[data-cmd="options"],
[data-cmd="refresh"] {
color: #000;
background-color: #f5f5f5;
}
#explore:not([data-loaded="true"]) {
height: 16px;
}
body[data-android="true"] [data-cmd="reload"],
body[data-android="true"] [data-cmd="window"] {
display: none;
}

View file

@ -0,0 +1,83 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="index.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div id="list" data-loading=true>
<table>
<colgroup>
<col width="40">
<col width="150">
<col width="100">
<col>
</colgroup>
<thead id="filter">
<tr>
<th colspan="3">
<select id="browser">
<optgroup label="Populars">
<option value="Internet Explorer">Internet Explorer</option>
<option value="Safari">Safari</option>
<option value="Chrome">Chrome</option>
<option value="Firefox">Firefox</option>
<option value="Opera">Opera</option>
<option value="Edge">Edge</option>
</optgroup>
<optgroup label="Others"></optgroup>
</select>&nbsp;
<select id="os">
<optgroup label="Populars">
<option value="Windows">Windows</option>
<option value="Mac OS">Mac OS</option>
<option value="Linux">Linux</option>
<option value="Chromium OS">Chromium OS</option>
<option value="Ubuntu">Ubuntu</option>
<option value="Debian">Debian</option>
<option value="Android">Android</option>
<option value="iOS">iOS</option>
</optgroup>
<optgroup label="Others"></optgroup>
</select>
</th>
<th>
User-Agent&nbsp;
<select id="sort">
<option value="true">descending</option>
<option value="false">ascending</option>
</select>
</th>
</tr>
</thead>
<template>
<tr>
<td><input type="radio" name="select"></td>
<td></td>
<td></td>
<td></td>
</tr>
</template>
<tbody></tbody>
</table>
</div>
<div hbox>
<input type="search" id="custom" placeholder="Filter items">
<input type="button" value="Options" title="Open options page" style="margin-left: 2px;" data-cmd="options">
<input type="button" value="Refresh Tab" title="Refresh the current page" data-cmd="refresh">
<input type="button" value="Restart" title="Click to reload the extension. This will cause all the window-based user-agent strings to be cleared" data-cmd="reload">
</div>
<div hbox id="agent" pack="center" align="center">
<span id="info">User-Agent String:</span>&nbsp;
<span flex="1"></span>
<input type="button" value="Apply" title="Set this string as the browser's User-Agent string" data-cmd="apply">
<input type="button" value="Window" title="Set this string as this window's User-Agent string" data-cmd="window">
<input type="button" value="Reset" title="Reset User-Agent string to the default one. This will not reset window-based UA strings. To reset them, use the 'Restart' button" style="margin-left: 2px;" data-cmd="reset">
</div>
<input id="ua" type="text" placeholder="Your preferred user-agent string" title="To set blank user-agent string, use 'empty' keyword">
<div id="explore"></div>
<script src="index.js"></script>
<script async src="matched.js"></script>
</body>
</html>

View file

@ -0,0 +1,195 @@
'use strict';
document.body.dataset.android = navigator.userAgent.indexOf('Android') !== -1;
var map = {};
function sort(arr) {
function sort(a = '', b = '') {
const pa = a.split('.');
const pb = b.split('.');
for (let i = 0; i < 3; i++) {
const na = Number(pa[i]);
const nb = Number(pb[i]);
if (na > nb) {
return 1;
}
if (nb > na) {
return -1;
}
if (!isNaN(na) && isNaN(nb)) {
return 1;
}
if (isNaN(na) && !isNaN(nb)) {
return -1;
}
}
return 0;
}
const list = arr.sort((a, b) => sort(a.browser.version, b.browser.version));
if (document.getElementById('sort').value === 'true') {
return list.reverse();
}
return list;
}
function update() {
const browser = document.getElementById('browser').value;
const os = document.getElementById('os').value;
const t = document.querySelector('template');
const parent = document.getElementById('list');
const tbody = parent.querySelector('tbody');
tbody.textContent = '';
parent.dataset.loading = true;
fetch('browsers/' + browser + '-' + os.replace(/\//g, '-') + '.json').then(r => r.json()).catch(() => []).then(list => {
if (list) {
const fragment = document.createDocumentFragment();
for (const o of sort(list)) {
const clone = document.importNode(t.content, true);
const second = clone.querySelector('td:nth-child(2)');
second.title = second.textContent = o.browser.name + ' ' + (o.browser.version || ' ');
const third = clone.querySelector('td:nth-child(3)');
third.title = third.textContent = o.os.name + ' ' + (o.os.version || ' ');
const forth = clone.querySelector('td:nth-child(4)');
forth.title = forth.textContent = o.ua;
fragment.appendChild(clone);
}
tbody.appendChild(fragment);
document.getElementById('custom').placeholder = `Filter among ${list.length} "User-Agent" strings`;
[...document.getElementById('os').querySelectorAll('option')].forEach(option => {
option.disabled = map[browser][option.value] !== true;
});
}
else {
throw Error('OS is not found');
}
}).finally(() => {
parent.dataset.loading = false;
});
}
document.addEventListener('change', ({target}) => {
if (target.closest('#filter')) {
localStorage.setItem(target.id, target.value);
update();
}
if (target.type === 'radio') {
document.getElementById('ua').value = target.closest('tr').querySelector('td:nth-child(4)').textContent;
document.getElementById('ua').dispatchEvent(new Event('input'));
}
});
document.addEventListener('DOMContentLoaded', () => fetch('./map.json').then(r => r.json())
.then(o => {
Object.assign(map, o);
const OSs = new Set();
const f1 = document.createDocumentFragment();
const f2 = document.createDocumentFragment();
Object.keys(map).sort().forEach(s => {
Object.keys(map[s]).forEach(s => OSs.add(s));
const option = document.createElement('option');
option.value = option.textContent = s;
f1.appendChild(option);
});
document.querySelector('#browser optgroup:last-of-type').appendChild(f1);
document.getElementById('browser').value = localStorage.getItem('browser') || 'Chrome';
for (const os of Array.from(OSs).sort()) {
const option = document.createElement('option');
option.value = option.textContent = os;
f2.appendChild(option);
}
document.querySelector('#os optgroup:last-of-type').appendChild(f2);
document.getElementById('os').value = localStorage.getItem('os') || 'Windows';
update();
}));
document.getElementById('list').addEventListener('click', ({target}) => {
const tr = target.closest('tr');
if (tr) {
const input = tr.querySelector('input');
if (input && input !== target) {
input.checked = !input.checked;
input.dispatchEvent(new Event('change', {
bubbles: true
}));
}
}
});
document.getElementById('custom').addEventListener('keyup', ({target}) => {
const value = target.value;
[...document.querySelectorAll('#list tr')]
.forEach(tr => tr.dataset.matched = tr.textContent.toLowerCase().indexOf(value.toLowerCase()) !== -1);
});
chrome.storage.local.get({
ua: ''
}, prefs => document.getElementById('ua').value = prefs.ua || navigator.userAgent);
chrome.storage.onChanged.addListener(prefs => {
if (prefs.ua) {
document.getElementById('ua').value = prefs.ua.newValue || navigator.userAgent;
document.getElementById('ua').dispatchEvent(new Event('input'));
}
});
function msg(msg) {
const info = document.getElementById('info');
info.textContent = msg;
window.setTimeout(() => info.textContent = 'User-Agent String:', 2000);
}
// 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 UA, press the reset button instead');
}
else {
msg('user-agent is set');
}
chrome.storage.local.set({
ua: value === navigator.userAgent ? '' : value
});
}
else if (cmd === 'window') {
const value = document.getElementById('ua').value;
chrome.tabs.query({
active: true,
currentWindow: true
}, ([tab]) => chrome.runtime.getBackgroundPage(bg => bg.ua.update(value, tab.windowId)));
}
else if (cmd === 'reset') {
const input = document.querySelector('#list :checked');
if (input) {
input.checked = false;
}
chrome.storage.local.set({
ua: ''
});
msg('reset to default');
}
else if (cmd === 'refresh') {
chrome.tabs.query({
active: true,
currentWindow: true
}, ([tab]) => chrome.tabs.reload(tab.id, {
bypassCache: true
}));
}
else if (cmd === 'options') {
chrome.runtime.openOptionsPage();
}
else if (cmd === 'reload') {
chrome.runtime.reload();
}
}
});
document.getElementById('ua').addEventListener('input', e => {
document.querySelector('[data-cmd=apply]').disabled = e.target.value === '';
document.querySelector('[data-cmd=window]').disabled = e.target.value === '';
});

View file

@ -0,0 +1 @@
<?xml version="1.0" ?><!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'><svg enable-background="new 0 0 48 48" height="48px" id="Layer_3" version="1.1" viewBox="0 0 48 48" width="48px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><polygon fill="#241F20" points="0,12.438 48,12.438 24,35.562 "/></svg>

After

Width:  |  Height:  |  Size: 404 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

View file

@ -0,0 +1 @@
../../../node/map.json

View file

@ -0,0 +1,169 @@
'use strict';
{
const shuffle = array => {
for (let i = array.length - 1; i > 0; i -= 1) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
};
const root = document.getElementById('explore');
const INC = Number(root.dataset.inc || 100);
const count = Number(localStorage.getItem('explore-count') || INC - 5);
const style = document.createElement('style');
style.textContent = `
#explore {
background-color: #fff;
position: relative;
color: #969696;
user-select: none;
}
#explore[data-loaded=true] {
margin: 4px;
padding: 5px;
box-shadow: 0 0 1px #ccc;
border: solid 1px #ccc;
}
#explore .close {
position: absolute;
right: 6px;
top: 4px;
cursor: pointer;
}
#explore>table {
margin-top: 10px;
table-layout: fixed;
width: 100%;
border-collapse: collapse;
}
#explore a {
text-decoration: none;
color: #000;
display: flex;
align-items: center;
justify-content: center;
}
#explore td:first-child a {
justify-content: flex-start;
}
#explore td:last-child a {
justify-content: flex-end;
}
#explore .title {
border-left: solid 1px #ccc;
display: inline-block;
align-items: center;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
padding-left: 5px;
}
#explore .icon {
min-width: 28px;
height: 28px;
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 50%;
color: #fff;
margin-right: 5px;
font-size: 10px;
font-weight: 100;
}
#explore .explore {
position: absolute;
right: 10px;
z-index: 1000000;
cursor: pointer;
font-size: 15px;
}`;
document.documentElement.appendChild(style);
const cload = () => fetch('matched.json').then(r => r.json()).then(build);
const explore = () => {
const span = document.createElement('span');
span.textContent = '↯';
span.title = 'Explore more';
span.classList.add('explore');
root.appendChild(span);
span.onclick = () => {
root.textContent = '';
localStorage.setItem('explore-count', INC);
cload();
};
};
const build = json => {
if (json.length === 0) {
return;
}
root.dataset.loaded = true;
root.textContent = 'Explore more';
const table = document.createElement('table');
const tr = document.createElement('tr');
const span = document.createElement('span');
span.classList.add('close');
span.textContent = '✕';
span.onclick = () => {
root.textContent = '';
root.dataset.loaded = false;
localStorage.setItem('explore-count', 0);
explore();
};
root.appendChild(span);
const {homepage_url} = chrome.runtime.getManifest();
const origin = homepage_url.split('/').slice(0, -1).join('/');
const colors = shuffle(
['524c84', '606470', '755da3', 'c06c84', '393e46', '446e5c', '693e52', '1d566e', '693e52', 'd95858', 'f27370']
);
shuffle(Object.entries(json)).slice(0, 3).forEach(([id, {name}], i) => {
const td = document.createElement('td');
const a = Object.assign(document.createElement('a'), {
target: '_blank',
title: 'Click to browse',
href: origin + '/' + id + '.html?context=explore'
});
const icon = document.createElement('span');
icon.textContent = name.split(' ').slice(0, 2).map(s => s[0]).join('').toUpperCase();
icon.classList.add('icon');
icon.style['background-color'] = '#' + colors[i];
a.appendChild(icon);
const span = document.createElement('span');
span.classList.add('title');
span.textContent = name;
a.appendChild(span);
td.appendChild(a);
tr.appendChild(td);
});
table.appendChild(tr);
root.appendChild(table);
};
const init = () => {
if (count >= INC) {
if (count < INC + 3) {
cload();
}
else {
explore();
}
if (count > INC + 5) {
localStorage.setItem('explore-count', INC - 6);
}
else {
localStorage.setItem('explore-count', count + 1);
}
}
else {
explore();
localStorage.setItem('explore-count', count + 1);
}
};
init();
}

View file

@ -0,0 +1,32 @@
{
"country-flags": {
"name": "Country Flags & IP WHOIS"
},
"useragent-switcher": {
"name": "User Agent Switcher"
},
"work-offline": {
"name": "Work Offline"
},
"local-cdn": {
"name": "Local CDN"
},
"media-converter": {
"name": "Tor Control (Anonymity Layer)"
},
"save-images": {
"name": "Download All Images"
},
"privacy-settings": {
"name": "Privacy Settings"
},
"media-player": {
"name": "YouTube Media Player"
},
"tab-discard": {
"name": "Auto Tab Discard"
},
"send-to": {
"name": "Send to VLC"
}
}