2023-04-12 05:01:06 +00:00
|
|
|
// Set power setting
|
|
|
|
function setPower(setting) {
|
2023-04-28 05:17:29 +00:00
|
|
|
chrome.storage.local.set({ 'power': setting });
|
2023-04-12 05:01:06 +00:00
|
|
|
var powerImage = document.getElementById('powerImage');
|
|
|
|
powerImage.src = 'images/power-' + setting + '.png';
|
|
|
|
powerImage.alt = 'Indie Wiki Buddy is ' + setting;
|
2023-02-06 00:48:53 +00:00
|
|
|
|
2023-04-12 05:01:06 +00:00
|
|
|
chrome.runtime.sendMessage({
|
|
|
|
action: 'updateIcon',
|
|
|
|
value: setting
|
|
|
|
});
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 06:29:50 +00:00
|
|
|
// Populate BreezeWiki dropdown when enabled
|
2023-04-12 05:01:06 +00:00
|
|
|
async function loadBreezeWikiOptions() {
|
|
|
|
// Load BreezeWiki options:
|
|
|
|
chrome.storage.sync.get(['breezewikiHostOptions', 'breezewikiHostFetchTimestamp', 'breezewikiHost'], function (item) {
|
|
|
|
let hostOptions = item.breezewikiHostOptions;
|
|
|
|
let hostFetchTimestamp = item.breezewikiHostFetchTimestamp;
|
|
|
|
let host = item.breezewikiHost;
|
|
|
|
// Fetch and cache list of BreezeWiki hosts if first time,
|
|
|
|
// or if it has been 24 hrs since last refresh
|
|
|
|
if (!host || !hostOptions || !hostFetchTimestamp || (Date.now() - 86400000 > hostFetchTimestamp)) {
|
|
|
|
fetch('https://bw.getindie.wiki/instances.json')
|
2023-04-28 06:19:55 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (response.ok) {
|
|
|
|
return response.json();
|
|
|
|
}
|
|
|
|
throw new Error('Indie Wiki Buddy failed to get BreezeWiki data.');
|
|
|
|
}).then((breezewikiHosts) => {
|
|
|
|
breezewikiHosts = breezewikiHosts.filter(host =>
|
|
|
|
chrome.runtime.getManifest().version.localeCompare(host.iwb_version,
|
|
|
|
undefined,
|
|
|
|
{ numeric: true, sensitivity: 'base' }
|
|
|
|
) >= 0
|
|
|
|
);
|
2023-04-12 05:01:06 +00:00
|
|
|
// If host isn't set, or currently selected host is no longer available, select random host:
|
|
|
|
if (!host || !breezewikiHosts.some(item => item.instance === host)) {
|
|
|
|
// Check if BreezeWiki's main site is available
|
|
|
|
let breezewikiMain = breezewikiHosts.filter(host => host.instance === 'https://breezewiki.com');
|
|
|
|
if (breezewikiMain.length > 0) {
|
|
|
|
host = breezewikiMain[0].instance;
|
|
|
|
} else {
|
|
|
|
// If BreezeWiki.com is not available, set to a random mirror
|
2023-04-28 06:19:55 +00:00
|
|
|
try {
|
|
|
|
host = breezewikiHosts[Math.floor(Math.random() * breezewikiHosts.length)].instance;
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Indie Wiki Buddy failed to get BreezeWiki data: ' + e);
|
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-12 05:01:06 +00:00
|
|
|
// Populate dropdown selection of hosts
|
|
|
|
const breezewikiHostSelect = document.getElementById('breezewikiHostSelect');
|
2023-06-29 07:24:05 +00:00
|
|
|
while (breezewikiHostSelect.firstChild) {
|
|
|
|
// Remove any existing options
|
|
|
|
breezewikiHostSelect.removeChild(breezewikiHostSelect.lastChild);
|
|
|
|
}
|
2023-04-12 05:01:06 +00:00
|
|
|
for (var i = 0; i < breezewikiHosts.length; i++) {
|
|
|
|
let option = document.createElement('option');
|
|
|
|
option.value = breezewikiHosts[i].instance;
|
2023-08-07 06:44:21 +00:00
|
|
|
let textContent = breezewikiHosts[i].instance.replace('https://', '');
|
|
|
|
const numberOfPeriods = (textContent.match(/\./g)||[]).length;
|
2023-04-12 05:01:06 +00:00
|
|
|
if (numberOfPeriods > 1) {
|
2023-08-07 06:44:21 +00:00
|
|
|
textContent = textContent.substring(textContent.indexOf('.') + 1);
|
2023-04-12 05:01:06 +00:00
|
|
|
}
|
2023-08-07 06:44:21 +00:00
|
|
|
option.textContent = textContent;
|
2023-04-12 05:01:06 +00:00
|
|
|
breezewikiHostSelect.appendChild(option);
|
|
|
|
if (option.value === host) {
|
|
|
|
breezewikiHostSelect.value = host;
|
2023-02-07 07:30:20 +00:00
|
|
|
}
|
2023-02-07 23:54:23 +00:00
|
|
|
}
|
2023-04-12 05:01:06 +00:00
|
|
|
// Store BreezeWiki host details
|
|
|
|
chrome.storage.sync.set({ 'breezewikiHost': host });
|
|
|
|
chrome.storage.sync.set({ 'breezewikiHostOptions': breezewikiHosts });
|
|
|
|
chrome.storage.sync.set({ 'breezewikiHostFetchTimestamp': Date.now() });
|
2023-04-28 06:19:55 +00:00
|
|
|
}).catch((e) => {
|
|
|
|
console.log('Indie Wiki Buddy failed to get BreezeWiki data: ' + e);
|
2023-06-28 06:29:50 +00:00
|
|
|
|
|
|
|
// If fetch fails and no host is set, default to breezewiki.com:
|
|
|
|
if (!host) {
|
|
|
|
chrome.storage.sync.set({ 'breezewikiHost': 'https://breezewiki.com'});
|
|
|
|
}
|
2023-04-12 05:01:06 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// If currently selected host is no longer available, select random host:
|
|
|
|
if (!hostOptions.some(item => item.instance === host)) {
|
|
|
|
host = hostOptions[Math.floor(Math.random() * hostOptions.length)].instance;
|
|
|
|
}
|
|
|
|
// Populate dropdown selection of hosts
|
|
|
|
const breezewikiHostSelect = document.getElementById('breezewikiHostSelect');
|
2023-06-29 07:24:05 +00:00
|
|
|
while (breezewikiHostSelect.firstChild) {
|
|
|
|
// Remove any existing options
|
|
|
|
breezewikiHostSelect.removeChild(breezewikiHostSelect.lastChild);
|
|
|
|
}
|
2023-04-12 05:01:06 +00:00
|
|
|
for (var i = 0; i < hostOptions.length; i++) {
|
|
|
|
let option = document.createElement('option');
|
|
|
|
option.value = hostOptions[i].instance;
|
2023-08-07 06:44:21 +00:00
|
|
|
let textContent = hostOptions[i].instance.replace('https://', '');
|
|
|
|
const numberOfPeriods = (textContent.match(/\./g)||[]).length;
|
2023-04-12 05:01:06 +00:00
|
|
|
if (numberOfPeriods > 1) {
|
2023-08-07 06:44:21 +00:00
|
|
|
textContent = textContent.substring(textContent.indexOf('.') + 1);
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
2023-08-07 06:44:21 +00:00
|
|
|
option.textContent = textContent;
|
2023-04-12 05:01:06 +00:00
|
|
|
breezewikiHostSelect.appendChild(option);
|
|
|
|
if (option.value === host) {
|
|
|
|
breezewikiHostSelect.value = host;
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-12 05:01:06 +00:00
|
|
|
// Store BreezeWiki host details
|
|
|
|
chrome.storage.sync.set({ 'breezewikiHost': host });
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set power setting
|
2023-04-27 05:47:34 +00:00
|
|
|
function setPower(setting, storeSetting = true) {
|
|
|
|
if (storeSetting) {
|
2023-04-28 05:17:29 +00:00
|
|
|
chrome.storage.local.set({ 'power': setting });
|
2023-04-27 05:47:34 +00:00
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
var powerImage = document.getElementById('powerImage');
|
|
|
|
powerImage.src = 'images/power-' + setting + '.png';
|
2023-04-12 05:01:06 +00:00
|
|
|
powerImage.alt = 'Indie Wiki Buddy is ' + setting;
|
|
|
|
if (setting === 'on') {
|
|
|
|
document.getElementById('powerCheckbox').checked = true;
|
|
|
|
} else {
|
|
|
|
document.getElementById('powerCheckbox').checked = false;
|
|
|
|
}
|
2023-02-06 11:00:44 +00:00
|
|
|
|
|
|
|
chrome.runtime.sendMessage({
|
|
|
|
action: 'updateIcon',
|
|
|
|
value: setting
|
|
|
|
});
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set notifications setting
|
2023-04-27 05:47:34 +00:00
|
|
|
function setNotifications(setting, storeSetting = true) {
|
|
|
|
if (storeSetting) {
|
|
|
|
chrome.storage.sync.set({ 'notifications': setting });
|
|
|
|
}
|
2023-04-12 05:01:06 +00:00
|
|
|
if (setting === 'on') {
|
|
|
|
document.getElementById('notificationsCheckbox').checked = true;
|
|
|
|
} else {
|
|
|
|
document.getElementById('notificationsCheckbox').checked = false;
|
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 08:45:46 +00:00
|
|
|
// Set search filter setting
|
2023-04-27 05:47:34 +00:00
|
|
|
function setSearchFilter(setting, storeSetting = true) {
|
|
|
|
if (storeSetting) {
|
|
|
|
chrome.storage.sync.set({ 'searchFilter': setting });
|
|
|
|
}
|
2023-04-12 05:01:06 +00:00
|
|
|
if (setting === 'on') {
|
|
|
|
document.getElementById('searchFilterCheckbox').checked = true;
|
|
|
|
} else {
|
|
|
|
document.getElementById('searchFilterCheckbox').checked = false;
|
|
|
|
}
|
2023-04-09 08:45:46 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 00:48:53 +00:00
|
|
|
// Set BreezeWiki settings
|
2023-04-27 05:47:34 +00:00
|
|
|
function setBreezeWiki(setting, storeSetting = true) {
|
|
|
|
if (storeSetting) {
|
|
|
|
chrome.storage.sync.set({ 'breezewiki': setting });
|
|
|
|
}
|
2023-04-12 05:01:06 +00:00
|
|
|
if (setting === 'on') {
|
|
|
|
document.getElementById('breezewikiCheckbox').checked = true;
|
|
|
|
} else {
|
|
|
|
document.getElementById('breezewikiCheckbox').checked = false;
|
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
var breezewikiHost = document.getElementById('breezewikiHost');
|
|
|
|
if (setting === 'on') {
|
|
|
|
breezewikiHost.style.display = 'block';
|
|
|
|
chrome.storage.sync.get({ 'breezewikiHost': null }, function (host) {
|
|
|
|
if (!host.breezewikiHost) {
|
2023-02-06 12:23:21 +00:00
|
|
|
fetch('https://bw.getindie.wiki/instances.json')
|
2023-04-28 06:19:55 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (response.ok) {
|
|
|
|
return response.json();
|
|
|
|
}
|
|
|
|
throw new Error('Indie Wiki Buddy failed to get BreezeWiki data.');
|
|
|
|
}).then((breezewikiHosts) => {
|
|
|
|
breezewikiHosts = breezewikiHosts.filter(host =>
|
|
|
|
chrome.runtime.getManifest().version.localeCompare(host.iwb_version,
|
|
|
|
undefined,
|
|
|
|
{ numeric: true, sensitivity: 'base' }
|
|
|
|
) >= 0
|
|
|
|
);
|
2023-02-06 12:23:21 +00:00
|
|
|
// Check if BreezeWiki's main site is available
|
|
|
|
let breezewikiMain = breezewikiHosts.filter(host => host.instance === 'https://breezewiki.com');
|
|
|
|
if (breezewikiMain.length > 0) {
|
|
|
|
host.breezewikiHost = breezewikiMain[0].instance;
|
|
|
|
} else {
|
|
|
|
// If BreezeWiki.com is not available, set to a random mirror
|
2023-04-28 06:19:55 +00:00
|
|
|
try {
|
|
|
|
host.breezewikiHost = breezewikiHosts[Math.floor(Math.random() * breezewikiHosts.length)].instance;
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Indie Wiki Buddy failed to get BreezeWiki data: ' + e);
|
|
|
|
}
|
2023-02-06 12:23:21 +00:00
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
chrome.storage.sync.set({ 'breezewikiHost': host.breezewikiHost });
|
|
|
|
chrome.storage.sync.set({ 'breezewikiHostOptions': breezewikiHosts });
|
|
|
|
chrome.storage.sync.set({ 'breezewikiHostFetchTimestamp': Date.now() });
|
|
|
|
document.getElementById('breezewikiHostSelect').value = host.breezewikiHost;
|
2023-04-28 06:19:55 +00:00
|
|
|
}).catch((e) => {
|
|
|
|
console.log('Indie Wiki Buddy failed to get BreezeWiki data: ' + e);
|
2023-06-28 06:29:50 +00:00
|
|
|
|
|
|
|
// If fetch fails and no host is set, default to breezewiki.com:
|
|
|
|
if (!host) {
|
|
|
|
chrome.storage.sync.set({ 'breezewikiHost': 'https://breezewiki.com'});
|
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
document.getElementById('breezewikiHostSelect').value = host.breezewikiHost;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
breezewikiHost.style.display = 'none';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main function that runs on-load
|
|
|
|
document.addEventListener('DOMContentLoaded', function () {
|
2023-07-01 06:33:11 +00:00
|
|
|
notificationBanner = document.getElementById('notificationBanner');
|
|
|
|
|
|
|
|
// If running Chromium, show warning about service worker bug
|
|
|
|
if (navigator.userAgent.match(/Chrom[e|ium]/)) {
|
|
|
|
notificationBanner.style.display = 'block';
|
|
|
|
document.getElementById('notificationBannerBug').style.display = 'block';
|
|
|
|
}
|
|
|
|
|
|
|
|
loadBreezeWikiOptions();
|
|
|
|
|
2023-04-12 05:01:06 +00:00
|
|
|
// Listener for settings page in new tab:
|
|
|
|
document.getElementById('openSettings').addEventListener('click', function () {
|
2023-04-14 08:05:47 +00:00
|
|
|
chrome.tabs.create({'url': chrome.runtime.getURL('settings.html')});
|
2023-04-12 05:01:06 +00:00
|
|
|
window.close();
|
2023-02-06 00:48:53 +00:00
|
|
|
});
|
|
|
|
|
2023-04-09 08:45:46 +00:00
|
|
|
// Set setting toggle values:
|
2023-04-28 05:17:29 +00:00
|
|
|
chrome.storage.local.get({ 'power': 'on' }, function (item) {
|
2023-04-27 05:47:34 +00:00
|
|
|
setPower(item.power, false);
|
2023-02-06 00:48:53 +00:00
|
|
|
});
|
|
|
|
chrome.storage.sync.get({ 'notifications': 'on' }, function (item) {
|
2023-04-27 05:47:34 +00:00
|
|
|
setNotifications(item.notifications, false);
|
2023-02-06 00:48:53 +00:00
|
|
|
});
|
2023-04-09 08:45:46 +00:00
|
|
|
chrome.storage.sync.get({ 'searchFilter': 'on' }, function (item) {
|
2023-04-27 05:47:34 +00:00
|
|
|
setSearchFilter(item.searchFilter, false);
|
2023-04-09 08:45:46 +00:00
|
|
|
});
|
2023-02-06 00:48:53 +00:00
|
|
|
chrome.storage.sync.get({ 'breezewiki': 'off' }, function (item) {
|
2023-04-27 05:47:34 +00:00
|
|
|
setBreezeWiki(item.breezewiki, false);
|
2023-06-28 06:29:50 +00:00
|
|
|
|
|
|
|
// Load BreezeWiki options if BreezeWiki is enabled
|
|
|
|
if(item.breezewiki === 'on') {
|
|
|
|
loadBreezeWikiOptions();
|
|
|
|
}
|
2023-02-06 00:48:53 +00:00
|
|
|
});
|
|
|
|
|
2023-04-09 08:45:46 +00:00
|
|
|
// Add event listeners for setting toggles
|
2023-04-12 05:01:06 +00:00
|
|
|
document.getElementById('powerCheckbox').addEventListener('change', function () {
|
2023-04-28 05:17:29 +00:00
|
|
|
chrome.storage.local.get({ 'power': 'on' }, function (item) {
|
2023-02-06 00:48:53 +00:00
|
|
|
if (item.power === 'on') {
|
|
|
|
setPower('off');
|
|
|
|
} else {
|
|
|
|
setPower('on');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2023-04-12 05:01:06 +00:00
|
|
|
document.getElementById('notificationsCheckbox').addEventListener('change', function () {
|
2023-02-06 00:48:53 +00:00
|
|
|
chrome.storage.sync.get({ 'notifications': 'on' }, function (item) {
|
|
|
|
if (item.notifications === 'on') {
|
|
|
|
setNotifications('off');
|
|
|
|
} else {
|
|
|
|
setNotifications('on');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2023-04-12 05:01:06 +00:00
|
|
|
document.getElementById('searchFilterCheckbox').addEventListener('change', function () {
|
2023-04-09 08:45:46 +00:00
|
|
|
chrome.storage.sync.get({ 'searchFilter': 'on' }, function (item) {
|
|
|
|
if (item.searchFilter === 'on') {
|
|
|
|
setSearchFilter('off');
|
|
|
|
} else {
|
|
|
|
setSearchFilter('on');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2023-04-12 05:01:06 +00:00
|
|
|
document.getElementById('breezewikiCheckbox').addEventListener('change', function () {
|
2023-02-06 00:48:53 +00:00
|
|
|
chrome.storage.sync.get({ 'breezewiki': 'off' }, function (item) {
|
|
|
|
if (item.breezewiki === 'on') {
|
|
|
|
setBreezeWiki('off');
|
|
|
|
} else {
|
|
|
|
setBreezeWiki('on');
|
2023-06-28 06:29:50 +00:00
|
|
|
loadBreezeWikiOptions();
|
2023-02-06 00:48:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
var breezewikiHostSelect = document.getElementById("breezewikiHostSelect");
|
|
|
|
breezewikiHostSelect.addEventListener('change', function () {
|
|
|
|
chrome.storage.sync.set({ 'breezewikiHost': breezewikiHostSelect.value });
|
|
|
|
});
|
|
|
|
});
|