2020-06-18 11:29:55 +00:00
|
|
|
const {
|
|
|
|
get_i18n_string,
|
|
|
|
set_sync_storage,
|
|
|
|
get_sync_storage,
|
|
|
|
add_storage_change_listener,
|
|
|
|
reload_tab,
|
|
|
|
dict_equality,
|
|
|
|
tab_query,
|
|
|
|
tab_sendmessage,
|
|
|
|
open_settings_page,
|
|
|
|
} = require("./util.js");
|
|
|
|
|
2020-05-28 14:56:16 +00:00
|
|
|
let settings_dict = {},
|
|
|
|
tab_settings = {},
|
|
|
|
reload_button,
|
|
|
|
active_tab;
|
2019-11-02 20:50:37 +00:00
|
|
|
|
|
|
|
function on_settings_change_intent() {
|
|
|
|
let is_different = !dict_equality(settings_dict, tab_settings);
|
|
|
|
|
|
|
|
if (reload_button !== undefined) {
|
|
|
|
reload_button.disabled = !is_different;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 02:22:00 +00:00
|
|
|
function bind_boolean_setting(checkbox_elem) {
|
|
|
|
let name = checkbox_elem.name,
|
2019-11-02 16:29:29 +00:00
|
|
|
label = checkbox_elem.nextSibling;
|
2020-05-28 14:56:16 +00:00
|
|
|
|
2020-05-28 19:24:01 +00:00
|
|
|
label.textContent = get_i18n_string("settings_" + name);
|
2020-05-28 14:56:16 +00:00
|
|
|
|
2020-07-02 01:41:20 +00:00
|
|
|
get_sync_storage(name, function (items) {
|
|
|
|
checkbox_elem.checked = items[name];
|
2019-11-02 20:50:37 +00:00
|
|
|
settings_dict[name] = items[name];
|
|
|
|
on_settings_change_intent();
|
2019-08-27 02:22:00 +00:00
|
|
|
});
|
|
|
|
|
2020-05-28 19:24:01 +00:00
|
|
|
add_storage_change_listener(function (changes) {
|
2020-05-28 14:56:16 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(changes, name)) {
|
2020-07-02 01:41:20 +00:00
|
|
|
checkbox_elem.checked = changes[name].newValue;
|
2019-11-02 20:50:37 +00:00
|
|
|
settings_dict[name] = changes[name].newValue;
|
|
|
|
on_settings_change_intent();
|
2019-08-27 02:22:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-28 14:56:16 +00:00
|
|
|
checkbox_elem.addEventListener("click", function () {
|
2019-11-01 22:21:50 +00:00
|
|
|
let setting = {};
|
2020-07-02 01:41:20 +00:00
|
|
|
setting[name] = checkbox_elem.checked;
|
2019-11-02 20:50:37 +00:00
|
|
|
settings_dict[name] = setting[name];
|
|
|
|
on_settings_change_intent();
|
2019-11-01 22:21:50 +00:00
|
|
|
|
2020-05-28 19:24:01 +00:00
|
|
|
set_sync_storage(setting);
|
2019-08-27 02:22:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-02 20:50:37 +00:00
|
|
|
function bind_settings_apply_button(elem) {
|
2020-05-28 19:24:01 +00:00
|
|
|
elem.textContent = get_i18n_string("action_" + elem.id);
|
2019-11-02 20:50:37 +00:00
|
|
|
elem.disabled = true;
|
|
|
|
|
2020-05-28 14:56:16 +00:00
|
|
|
elem.addEventListener("click", function () {
|
2020-05-28 19:24:01 +00:00
|
|
|
reload_tab(active_tab.id, function () {
|
2020-05-30 17:49:59 +00:00
|
|
|
window.setInterval(query_current_tab, 1000);
|
2019-11-02 20:50:37 +00:00
|
|
|
});
|
|
|
|
});
|
2020-05-28 14:56:16 +00:00
|
|
|
|
2019-11-02 20:50:37 +00:00
|
|
|
reload_button = elem;
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:06:00 +00:00
|
|
|
let got_status = false;
|
|
|
|
|
2019-11-02 20:50:37 +00:00
|
|
|
async function query_current_tab() {
|
2019-10-06 20:04:00 +00:00
|
|
|
let ruffle_status = document.getElementById("ruffle_status");
|
|
|
|
if (ruffle_status === null) {
|
2020-05-28 14:56:16 +00:00
|
|
|
/*debugger;*/
|
2019-08-27 02:22:00 +00:00
|
|
|
}
|
2020-05-28 14:56:16 +00:00
|
|
|
|
2020-06-04 21:06:00 +00:00
|
|
|
if (!got_status) {
|
|
|
|
ruffle_status.textContent = get_i18n_string("status_init");
|
|
|
|
}
|
|
|
|
|
2019-10-06 20:04:00 +00:00
|
|
|
let tabs = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
tabs = await tab_query({
|
2020-05-28 14:56:16 +00:00
|
|
|
currentWindow: true,
|
|
|
|
active: true,
|
2019-10-06 20:04:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (tabs.length < 1) {
|
2020-05-28 19:24:01 +00:00
|
|
|
ruffle_status.textContent = get_i18n_string("status_no_tabs");
|
2019-10-06 20:04:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tabs.length > 1) {
|
2020-05-28 14:56:16 +00:00
|
|
|
console.warn(
|
|
|
|
"Got " + tabs.length + " tabs in response to active tab query"
|
|
|
|
);
|
2019-10-06 20:04:00 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2020-05-28 19:24:01 +00:00
|
|
|
ruffle_status.textContent = get_i18n_string("status_tabs_error");
|
2019-10-06 20:04:00 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-11-02 20:50:37 +00:00
|
|
|
active_tab = tabs[0];
|
2019-10-06 20:04:00 +00:00
|
|
|
|
2020-05-28 19:24:01 +00:00
|
|
|
ruffle_status.textContent = get_i18n_string("status_message_init");
|
2019-10-06 20:04:00 +00:00
|
|
|
|
2020-05-28 14:56:16 +00:00
|
|
|
let resp = await tab_sendmessage(active_tab.id, {
|
|
|
|
action: "get_page_options",
|
|
|
|
});
|
2019-11-02 20:50:37 +00:00
|
|
|
|
|
|
|
tab_settings = resp.tab_settings;
|
|
|
|
on_settings_change_intent();
|
|
|
|
|
2019-11-01 23:11:51 +00:00
|
|
|
if (resp !== undefined && resp.loaded) {
|
2020-05-28 19:24:01 +00:00
|
|
|
ruffle_status.textContent = get_i18n_string(
|
2020-05-28 14:56:16 +00:00
|
|
|
"status_result_running"
|
|
|
|
);
|
2019-11-01 23:11:51 +00:00
|
|
|
} else if (resp !== undefined && !resp.loaded) {
|
2020-07-02 01:41:20 +00:00
|
|
|
if (tab_settings.ruffle_enable) {
|
2020-05-28 19:24:01 +00:00
|
|
|
ruffle_status.textContent = get_i18n_string(
|
2020-05-28 14:56:16 +00:00
|
|
|
"status_result_optout"
|
|
|
|
);
|
2019-11-02 20:57:42 +00:00
|
|
|
} else {
|
2020-05-28 19:24:01 +00:00
|
|
|
ruffle_status.textContent = get_i18n_string(
|
2020-05-28 14:56:16 +00:00
|
|
|
"status_result_disabled"
|
|
|
|
);
|
2019-11-02 20:57:42 +00:00
|
|
|
}
|
2019-10-06 20:04:00 +00:00
|
|
|
} else {
|
2020-05-28 19:24:01 +00:00
|
|
|
ruffle_status.textContent = get_i18n_string("status_result_error");
|
2019-10-06 20:04:00 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2020-05-28 19:24:01 +00:00
|
|
|
ruffle_status.textContent = get_i18n_string("status_result_protected");
|
2020-05-30 17:30:45 +00:00
|
|
|
if (reload_button) {
|
|
|
|
reload_button.disabled = true;
|
|
|
|
}
|
2019-10-06 20:04:00 +00:00
|
|
|
throw e;
|
2019-08-27 02:22:00 +00:00
|
|
|
}
|
2019-11-02 20:50:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:56:16 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
2020-05-28 18:13:38 +00:00
|
|
|
var settings_button = document.getElementById("settingsbutton");
|
2019-11-02 20:50:37 +00:00
|
|
|
bind_boolean_setting(document.getElementById("ruffle_enable"));
|
|
|
|
bind_boolean_setting(document.getElementById("ignore_optout"));
|
|
|
|
bind_settings_apply_button(document.getElementById("reload"));
|
2020-05-28 19:24:01 +00:00
|
|
|
settings_button.innerHTML = get_i18n_string("open_settings_page");
|
2020-05-28 18:13:38 +00:00
|
|
|
settings_button.onclick = open_settings_page;
|
2019-11-02 20:50:37 +00:00
|
|
|
|
|
|
|
query_current_tab();
|
2020-05-28 14:56:16 +00:00
|
|
|
});
|