ruffle/web/packages/extension/js/main.js

152 lines
4.1 KiB
JavaScript
Raw Normal View History

const {
2021-01-03 13:38:11 +00:00
getI18nString,
setSyncStorage,
getSyncStorage,
addStorageChangeListener,
reloadTab,
dictEquality,
tabQuery,
tabSendmessage,
openSettingsPage,
camelize,
} = require("./util.js");
2021-01-03 13:38:11 +00:00
let settingsDict = {},
tabSettings = {},
reloadButton,
activeTab;
2021-01-03 13:38:11 +00:00
function onSettingsChangeIntent() {
let isDifferent = !dictEquality(settingsDict, tabSettings);
2021-01-03 13:38:11 +00:00
if (reloadButton !== undefined) {
reloadButton.disabled = !isDifferent;
}
}
2021-01-03 13:38:11 +00:00
function bindBooleanSetting(checkboxElem) {
let name = checkboxElem.name,
label = checkboxElem.nextSibling;
2021-01-03 13:38:11 +00:00
label.textContent = getI18nString("settings_" + name);
name = camelize(name);
2021-01-03 13:38:11 +00:00
getSyncStorage(name, function (items) {
checkboxElem.checked = items[name];
settingsDict[name] = items[name];
onSettingsChangeIntent();
});
2021-01-03 13:38:11 +00:00
addStorageChangeListener(function (changes) {
if (Object.prototype.hasOwnProperty.call(changes, name)) {
2021-01-03 13:38:11 +00:00
checkboxElem.checked = changes[name].newValue;
settingsDict[name] = changes[name].newValue;
onSettingsChangeIntent();
}
});
2021-01-03 13:38:11 +00:00
checkboxElem.addEventListener("click", function () {
2019-11-01 22:21:50 +00:00
let setting = {};
2021-01-03 13:38:11 +00:00
setting[name] = checkboxElem.checked;
settingsDict[name] = setting[name];
onSettingsChangeIntent();
2019-11-01 22:21:50 +00:00
2021-01-03 13:38:11 +00:00
setSyncStorage(setting);
});
}
2021-01-03 13:38:11 +00:00
function bindSettingsApplyButton(elem) {
elem.textContent = getI18nString("action_" + elem.id);
elem.disabled = true;
elem.addEventListener("click", function () {
2021-01-03 13:38:11 +00:00
reloadTab(activeTab.id, function () {
window.setInterval(queryCurrentTab, 1000);
});
});
2021-01-03 13:38:11 +00:00
reloadButton = elem;
}
2021-01-03 13:38:11 +00:00
let gotStatus = false;
2020-06-04 21:06:00 +00:00
2021-01-03 13:38:11 +00:00
async function queryCurrentTab() {
let ruffleStatus = document.getElementById("ruffle_status");
if (ruffleStatus === null) {
/*debugger;*/
}
2021-01-03 13:38:11 +00:00
if (!gotStatus) {
ruffleStatus.textContent = getI18nString("status_init");
2020-06-04 21:06:00 +00:00
}
let tabs = null;
try {
2021-01-03 13:38:11 +00:00
tabs = await tabQuery({
currentWindow: true,
active: true,
});
if (tabs.length < 1) {
2021-01-03 13:38:11 +00:00
ruffleStatus.textContent = getI18nString("status_no_tabs");
return;
}
if (tabs.length > 1) {
console.warn(
"Got " + tabs.length + " tabs in response to active tab query"
);
}
} catch (e) {
2021-01-03 13:38:11 +00:00
ruffleStatus.textContent = getI18nString("status_tabs_error");
throw e;
}
try {
2021-01-03 13:38:11 +00:00
activeTab = tabs[0];
2021-01-03 13:38:11 +00:00
ruffleStatus.textContent = getI18nString("status_message_init");
2021-01-03 13:38:11 +00:00
let resp = await tabSendmessage(activeTab.id, {
action: "get_page_options",
});
2021-01-03 13:38:11 +00:00
tabSettings = resp.tabSettings;
onSettingsChangeIntent();
if (resp !== undefined && resp.loaded) {
2021-01-03 13:38:11 +00:00
ruffleStatus.textContent = getI18nString("status_result_running");
} else if (resp !== undefined && !resp.loaded) {
2021-01-03 13:38:11 +00:00
if (tabSettings.ruffleEnable) {
ruffleStatus.textContent = getI18nString(
"status_result_optout"
);
} else {
2021-01-03 13:38:11 +00:00
ruffleStatus.textContent = getI18nString(
"status_result_disabled"
);
}
} else {
2021-01-03 13:38:11 +00:00
ruffleStatus.textContent = getI18nString("status_result_error");
}
} catch (e) {
2021-01-03 13:38:11 +00:00
ruffleStatus.textContent = getI18nString("status_result_protected");
if (reloadButton) {
reloadButton.disabled = true;
}
throw e;
}
}
document.addEventListener("DOMContentLoaded", function () {
2021-01-03 13:38:11 +00:00
var settingsButton = document.getElementById("settingsbutton");
bindBooleanSetting(document.getElementById("ruffle_enable"));
bindBooleanSetting(document.getElementById("ignore_optout"));
bindSettingsApplyButton(document.getElementById("reload"));
settingsButton.innerHTML = getI18nString("open_settings_page");
settingsButton.onclick = openSettingsPage;
queryCurrentTab();
});