2017-09-13 03:18:12 -07:00
|
|
|
'use strict';
|
|
|
|
|
2018-08-16 23:16:10 -07:00
|
|
|
document.body.dataset.android = navigator.userAgent.indexOf('Android') !== -1;
|
|
|
|
|
2019-07-31 22:41:28 -07:00
|
|
|
const map = {};
|
2017-09-13 03:18:12 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-31 22:41:28 -07:00
|
|
|
function update(ua) {
|
2019-03-12 02:50:52 -07:00
|
|
|
const browser = document.getElementById('browser').value;
|
|
|
|
const os = document.getElementById('os').value;
|
|
|
|
|
2017-09-13 03:18:12 -07:00
|
|
|
const t = document.querySelector('template');
|
|
|
|
const parent = document.getElementById('list');
|
|
|
|
const tbody = parent.querySelector('tbody');
|
|
|
|
tbody.textContent = '';
|
2019-03-12 02:50:52 -07:00
|
|
|
|
2017-09-13 03:18:12 -07:00
|
|
|
parent.dataset.loading = true;
|
2019-06-25 03:22:49 -07:00
|
|
|
fetch('browsers/' + browser.toLowerCase() + '-' + os.toLowerCase().replace(/\//g, '-') + '.json').then(r => r.json()).catch(e => {
|
2019-03-14 07:20:52 -07:00
|
|
|
console.error(e);
|
|
|
|
return [];
|
|
|
|
}).then(list => {
|
2019-03-12 02:50:52 -07:00
|
|
|
if (list) {
|
|
|
|
const fragment = document.createDocumentFragment();
|
2019-07-31 22:41:28 -07:00
|
|
|
let radio;
|
2019-03-12 02:50:52 -07:00
|
|
|
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;
|
2019-07-31 22:41:28 -07:00
|
|
|
if (o.ua === ua) {
|
|
|
|
radio = clone.querySelector('input[type=radio]');
|
|
|
|
}
|
2019-03-12 02:50:52 -07:00
|
|
|
fragment.appendChild(clone);
|
|
|
|
}
|
|
|
|
tbody.appendChild(fragment);
|
2019-07-31 22:41:28 -07:00
|
|
|
if (radio) {
|
|
|
|
radio.checked = true;
|
|
|
|
radio.scrollIntoView({
|
|
|
|
block: 'center',
|
|
|
|
inline: 'nearest'
|
|
|
|
});
|
|
|
|
}
|
2019-03-12 02:50:52 -07:00
|
|
|
document.getElementById('custom').placeholder = `Filter among ${list.length} "User-Agent" strings`;
|
|
|
|
[...document.getElementById('os').querySelectorAll('option')].forEach(option => {
|
2019-06-25 03:22:49 -07:00
|
|
|
option.disabled = map.matching[browser.toLowerCase()].indexOf(option.value.toLowerCase()) === -1;
|
2019-03-12 02:50:52 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw Error('OS is not found');
|
|
|
|
}
|
|
|
|
}).finally(() => {
|
2017-09-14 05:50:53 -07:00
|
|
|
parent.dataset.loading = false;
|
2019-03-12 02:50:52 -07:00
|
|
|
});
|
2017-09-13 03:18:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('change', ({target}) => {
|
|
|
|
if (target.closest('#filter')) {
|
|
|
|
localStorage.setItem(target.id, target.value);
|
2019-07-31 23:53:48 -07:00
|
|
|
chrome.storage.local.get({
|
|
|
|
ua: ''
|
|
|
|
}, prefs => update(prefs.ua || navigator.userAgent));
|
2017-09-13 03:18:12 -07:00
|
|
|
}
|
2018-04-10 00:50:58 -07:00
|
|
|
if (target.type === 'radio') {
|
2017-09-13 03:18:12 -07:00
|
|
|
document.getElementById('ua').value = target.closest('tr').querySelector('td:nth-child(4)').textContent;
|
2018-08-16 03:14:11 -07:00
|
|
|
document.getElementById('ua').dispatchEvent(new Event('input'));
|
2017-09-13 03:18:12 -07:00
|
|
|
}
|
|
|
|
});
|
2019-03-12 02:50:52 -07:00
|
|
|
document.addEventListener('DOMContentLoaded', () => fetch('./map.json').then(r => r.json())
|
|
|
|
.then(o => {
|
|
|
|
Object.assign(map, o);
|
2019-06-25 03:22:49 -07:00
|
|
|
|
2019-03-12 02:50:52 -07:00
|
|
|
const f1 = document.createDocumentFragment();
|
2019-06-25 03:22:49 -07:00
|
|
|
for (const browser of map.browser) {
|
2019-03-12 02:50:52 -07:00
|
|
|
const option = document.createElement('option');
|
2019-06-25 03:22:49 -07:00
|
|
|
option.value = option.textContent = browser;
|
2019-03-12 02:50:52 -07:00
|
|
|
f1.appendChild(option);
|
2019-06-25 03:22:49 -07:00
|
|
|
}
|
|
|
|
const f2 = document.createDocumentFragment();
|
|
|
|
for (const os of map.os) {
|
2019-03-12 02:50:52 -07:00
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = option.textContent = os;
|
|
|
|
f2.appendChild(option);
|
|
|
|
}
|
2019-06-25 03:22:49 -07:00
|
|
|
|
|
|
|
document.querySelector('#browser optgroup:last-of-type').appendChild(f1);
|
|
|
|
document.getElementById('browser').value = localStorage.getItem('browser') || 'Chrome';
|
|
|
|
|
2019-03-12 02:50:52 -07:00
|
|
|
document.querySelector('#os optgroup:last-of-type').appendChild(f2);
|
|
|
|
document.getElementById('os').value = localStorage.getItem('os') || 'Windows';
|
2019-06-25 03:22:49 -07:00
|
|
|
|
2019-07-31 22:41:28 -07:00
|
|
|
chrome.storage.local.get({
|
|
|
|
ua: ''
|
|
|
|
}, prefs => {
|
|
|
|
const ua = prefs.ua || navigator.userAgent;
|
|
|
|
update(ua);
|
|
|
|
document.getElementById('ua').value = ua;
|
|
|
|
});
|
2019-03-12 02:50:52 -07:00
|
|
|
}));
|
2017-09-13 03:18:12 -07:00
|
|
|
|
|
|
|
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.onChanged.addListener(prefs => {
|
|
|
|
if (prefs.ua) {
|
|
|
|
document.getElementById('ua').value = prefs.ua.newValue || navigator.userAgent;
|
2018-08-16 03:14:11 -07:00
|
|
|
document.getElementById('ua').dispatchEvent(new Event('input'));
|
2017-09-13 03:18:12 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-11-22 04:35:22 -08:00
|
|
|
function msg(msg) {
|
|
|
|
const info = document.getElementById('info');
|
|
|
|
info.textContent = msg;
|
2018-08-16 03:14:11 -07:00
|
|
|
window.setTimeout(() => info.textContent = 'User-Agent String:', 2000);
|
2017-11-22 04:35:22 -08:00
|
|
|
}
|
|
|
|
|
2017-09-13 03:18:12 -07:00
|
|
|
// commands
|
|
|
|
document.addEventListener('click', ({target}) => {
|
|
|
|
const cmd = target.dataset.cmd;
|
|
|
|
if (cmd) {
|
|
|
|
if (cmd === 'apply') {
|
2017-11-22 04:35:22 -08:00
|
|
|
const value = document.getElementById('ua').value;
|
|
|
|
if (value === navigator.userAgent) {
|
2018-08-16 03:14:11 -07:00
|
|
|
msg('Default UA, press the reset button instead');
|
2017-11-22 04:35:22 -08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg('user-agent is set');
|
|
|
|
}
|
2017-09-13 03:18:12 -07:00
|
|
|
chrome.storage.local.set({
|
2017-11-22 04:35:22 -08:00
|
|
|
ua: value === navigator.userAgent ? '' : value
|
2017-09-13 03:18:12 -07:00
|
|
|
});
|
|
|
|
}
|
2018-08-16 03:14:11 -07:00
|
|
|
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)));
|
|
|
|
}
|
2017-09-13 03:18:12 -07:00
|
|
|
else if (cmd === 'reset') {
|
|
|
|
const input = document.querySelector('#list :checked');
|
|
|
|
if (input) {
|
|
|
|
input.checked = false;
|
|
|
|
}
|
|
|
|
chrome.storage.local.set({
|
|
|
|
ua: ''
|
|
|
|
});
|
2017-11-22 04:35:22 -08:00
|
|
|
msg('reset to default');
|
2017-09-13 03:18:12 -07:00
|
|
|
}
|
2017-11-19 01:32:13 -08:00
|
|
|
else if (cmd === 'refresh') {
|
|
|
|
chrome.tabs.query({
|
|
|
|
active: true,
|
|
|
|
currentWindow: true
|
|
|
|
}, ([tab]) => chrome.tabs.reload(tab.id, {
|
|
|
|
bypassCache: true
|
|
|
|
}));
|
|
|
|
}
|
2018-05-15 00:54:51 -07:00
|
|
|
else if (cmd === 'options') {
|
|
|
|
chrome.runtime.openOptionsPage();
|
|
|
|
}
|
2018-08-16 03:14:11 -07:00
|
|
|
else if (cmd === 'reload') {
|
|
|
|
chrome.runtime.reload();
|
|
|
|
}
|
2019-05-15 22:18:58 -07:00
|
|
|
else if (cmd === 'test') {
|
|
|
|
chrome.storage.local.get({
|
2019-08-01 00:24:39 -07:00
|
|
|
'test': 'https://webbrowsertools.com/useragent/?method=normal&verbose=false&r=' + Math.random()
|
2019-05-15 22:18:58 -07:00
|
|
|
}, prefs => chrome.tabs.create({
|
|
|
|
url: prefs.test
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd) {
|
|
|
|
target.classList.add('active');
|
|
|
|
window.setTimeout(() => target.classList.remove('active'), 500);
|
|
|
|
}
|
2017-09-13 03:18:12 -07:00
|
|
|
}
|
|
|
|
});
|
2018-08-16 03:14:11 -07:00
|
|
|
|
|
|
|
document.getElementById('ua').addEventListener('input', e => {
|
|
|
|
document.querySelector('[data-cmd=apply]').disabled = e.target.value === '';
|
|
|
|
document.querySelector('[data-cmd=window]').disabled = e.target.value === '';
|
|
|
|
});
|