Extension at least starts now

This commit is contained in:
Tamius Han 2018-08-07 23:31:28 +02:00
parent 966f8a034d
commit da18f5a5c4
9 changed files with 104 additions and 106 deletions

View File

@ -1,11 +1,12 @@
var _bd_usebrowser = "firefox";
var _bd_isFirefox = false;
var _bd_isFirefox = true;
var _bd_isChrome = false;
var _bd_isEdge = false; // we'll see if FF
try{
// todo: find something that works in firefox but not in edge (or vice-versa)
// todo: find something that works in firefox but not in edge (or vice-versa)
// note that this function returns a promise! and is broken for some reason
var browserinfo = browser.runtime.getBrowserInfo();
// we don't need to actually check because only firefox supports that.

View File

@ -8,11 +8,17 @@ class CommsClient {
this.port = browser.runtime.connect({name: name});
} else if (BrowserDetect.chrome) {
this.port = chrome.runtime.connect({name: name});
} else if (BrowserDetect.edge) {
this.port = browser.runtime.connect({name: name})
}
console.log("BrowserDetect", BrowserDetect)
console.log("!! port", this.port)
var ths = this;
this.port.onMessage.addListener(m => ths.processReceivedMessage(m));
this.hasSettings = false;
this.settings = new Settings();
}
setPageInfo(pageInfo){
@ -136,6 +142,7 @@ class CommsClient {
class CommsServer {
constructor(server) {
this.server = server;
this.settings = server.settings;
this.ports = [];
var ths = this;
@ -230,25 +237,25 @@ class CommsServer {
this.sendToActive(message);
} else if (message.cmd === 'set-stretch-default') {
this.settings.active.stretch.initialMode = message.mode;
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: 'reload-settings', newConf: this.settings.active});
} else if (message.cmd === 'set-ar') {
this.sendToActive(message);
} else if (message.cmd === 'set-custom-ar') {
this.settings.active.keyboard.shortcuts.q.arg = message.ratio;
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: 'reload-settings', newConf: this.settings.active});
} else if (message.cmd === 'set-video-float') {
this.sendToActive(message);
this.settings.active.miscFullscreenthis.settings.videoFloat = message.newFloat;
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: 'reload-settings', newConf: this.settings.active});
} else if (message.cmd === 'autoar-start') {
this.sendToActive(message);
} else if (message.cmd === "autoar-enable") { // LEGACY - can be removed prolly?
this.settings.active.arDetect.mode = "blacklist";
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: 'reload-settings', newConf: this.settings.active});
} else if (message.cmd === "autoar-disable") { // LEGACY - can be removed prolly?
this.settings.active.arDetect.mode = "disabled";
@ -257,7 +264,7 @@ class CommsServer {
} else {
this.settings.active.arDetect.disabledReason = 'User disabled';
}
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: 'reload-settings', newConf: this.settings.active});
} else if (message.cmd === "autoar-set-interval") {
if(Debug.debug)
@ -266,40 +273,40 @@ class CommsServer {
// set fairly liberal limit
var timeout = message.timeout < 4 ? 4 : message.timeout;
this.settings.active.arDetect.timer_playing = timeout;
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: 'reload-settings', newConf: this.settings.active});
} else if (message.cmd === "set-autoar-defaults") {
this.settings.active.arDetect.mode = message.mode;
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: "reload-settings", sender: "uwbg"})
} else if (message.cmd === "set-autoar-for-site") {
if (this.settings.active.sites[this.server.currentSite]) {
this.settings.active.sites[this.server.currentSite].arStatus = message.mode;
this.settings.save(this.settings.active);
this.settings.save();
} else {
this.settings.active.sites[this.server.currentSite] = {
status: "default",
arStatus: message.mode,
statusEmbedded: "default"
};
this.settings.save(this.settings.active);
this.settings.save();
}
this.sendToAll({cmd: "reload-settings", sender: "uwbg"});
} else if (message.cmd === "set-extension-defaults") {
this.settings.active.extensionMode = message.mode;
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: "reload-settings", sender: "uwbg"})
} else if (message.cmd === "set-extension-for-site") {
if (this.settings.active.sites[this.server.currentSite]) {
this.settings.active.sites[this.server.currentSite].status = message.mode;
this.settings.save(this.settings.active);
this.settings.save();
} else {
this.settings.active.sites[this.server.currentSite] = {
status: message.mode,
arStatus: "default",
statusEmbedded: message.mode
};
this.settings.save(this.settings.active);
this.settings.save();
console.log("SAVING PER-SITE OPTIONS,", this.server.currentSite, this.settings.active.sites[this.server.currentSite])
}
this.sendToAll({cmd: "reload-settings", sender: "uwbg"});
@ -323,7 +330,7 @@ class CommsServer {
Promise.resolve(ret);
} else if (message.cmd === "autoar-enable") {
this.settings.active.arDetect.mode = "blacklist";
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: "reload-settings", sender: "uwbg"})
if(Debug.debug){
console.log("[uw-bg] autoar set to enabled (blacklist). evidenz:", this.settings.active);
@ -335,7 +342,7 @@ class CommsServer {
} else {
this.settings.active.arDetect.disabledReason = 'User disabled';
}
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: 'reload-settings', newConf: this.settings.active});
if(Debug.debug){
console.log("[uw-bg] autoar set to disabled. evidenz:", this.settings.active);
@ -347,7 +354,7 @@ class CommsServer {
// set fairly liberal limit
var timeout = message.timeout < 4 ? 4 : message.timeout;
this.settings.active.arDetect.timer_playing = timeout;
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: 'reload-settings', newConf: this.settings.active});
}
}
@ -362,7 +369,7 @@ class CommsServer {
// return true;
} else if (message.cmd === "autoar-enable") {
this.settings.active.arDetect.mode = "blacklist";
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: "reload-settings", sender: "uwbg"})
if(Debug.debug){
console.log("[uw-bg] autoar set to enabled (blacklist). evidenz:", this.settings.active);
@ -374,7 +381,7 @@ class CommsServer {
} else {
this.settings.active.arDetect.disabledReason = 'User disabled';
}
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: 'reload-settings', newConf: this.settings.active});
if(Debug.debug){
console.log("[uw-bg] autoar set to disabled. evidenz:", this.settings.active);
@ -386,7 +393,7 @@ class CommsServer {
// set fairly liberal limit
var timeout = message.timeout < 4 ? 4 : message.timeout;
this.settings.active.arDetect.timer_playing = timeout;
this.settings.save(this.settings.active);
this.settings.save();
this.sendToAll({cmd: 'reload-settings', newConf: this.settings.active});
}
}

View File

@ -1,7 +1,7 @@
class Settings {
constructor() {
this.active = {};
constructor(activeSettings) {
this.active = activeSettings ? activeSettings : {};
this.default = ExtensionConf;
}
@ -13,7 +13,8 @@ class Settings {
}
// if there's no settings saved, return default settings.
if(! settings || (Object.keys(newSettings).length === 0 && newSettings.constructor === Object)) {
if(! settings || (Object.keys(settings).length === 0 && settings.constructor === Object)) {
console.log("NO SETTINGS")
this.setDefaultSettings();
this.active = this.getDefaultSettings();
return this.active;
@ -23,7 +24,16 @@ class Settings {
this.active = settings;
// check if extension has been updated. If not, return settings as they were retreived
const uwVersion = runtime.getManifest().version;
var uwVersion;
if (BrowserDetect.firefox) {
uwVersion = browser.runtime.getManifest().version;
} else if (BrowserDetect.chrome) {
uwVersion = chrome.runtime.getManifest().version;
} else if (BrowserDetect.edge) {
uwVersion = browser.runtime.getManifest().version;
}
if(settings.version === uwVersion) {
if(Debug.debug) {
console.log("[Settings::init] extension was saved with current version of ultrawidify (", uwVersion, "). Returning object as-is.");
@ -40,20 +50,34 @@ class Settings {
async get() {
if (BrowserDetect.firefox || BrowserDetect.edge) {
return browser.storage.sync.get('uw-settings');
const ret = browser.storage.sync.get('uw-settings');
return ret['uw-settings'];
} else if (BrowserDetect.chrome) {
return chrome.storage.sync.get('uw-settings');
const ret = chrome.storage.sync.get('uw-settings');
return ret['uw-settings'];
}
}
async set(extensionConf) {
if (BrowserDetect.firefox || BrowserDetect.edge) {
return browser.storage.sync.set('uw-settings', extensionConf);
return browser.storage.sync.set( {'uw-settings': extensionConf});
} else if (BrowserDetect.chrome) {
return chrome.storage.sync.set('uw-settings', extensionConf);
return chrome.storage.sync.set( {'uw-settings': extensionConf});
}
}
async setActive(activeSettings) {
this.active = activeSettings;
}
async setProp(prop, value) {
this.active[prop] = value;
}
async save() {
this.set(this.active);
}
getDefaultSettings() {
return JSON.parse(JSON.stringify(this.default));
}
@ -87,6 +111,8 @@ class Settings {
site = window.location.hostname;
if (!site) {
console.log("[Settings::canStartExtension] window.location.hostname is null or undefined:", window.location.hostname)
console.log("active settings:", this.active)
return false;
}
}
@ -105,19 +131,22 @@ class Settings {
"\nCan extension be started?", cse
);
}
try{
// if site is not defined, we use default mode:
if (! this.active.sites[site]) {
return this.active.extensionMode === "blacklist";
}
if(this.active.extensionMode === "blacklist") {
return this.active.sites[site] !== "disabled";
return this.active.sites[site].status !== "disabled";
} else if (this.active.extensionMode === "whitelist") {
return this.active.sites[site] === "enabled";
return this.active.sites[site].status === "enabled";
} else {
return false;
}
}catch(e){
console.log("THIS?", this)
}
}

View File

@ -1,50 +0,0 @@
// setopt, getopt, delopt. Shrani oz. dobi oz. briše stvari iz skladišča
// setopt, getopt, delopt. They set/get/delete stuff from the storage
var _sm_setopt = function(item){
return browser.storage.local.set(item);
}
var _sm_getopt = function(prop, callback){
if(BrowserDetect.usebrowser == "chrome")
return browser.storage.local.get(prop, callback);
else
return browser.storage.local.get(prop).then(callback);
}
var _sm_chrome_getopt_wrapper = async function(prop){
return new Promise(function (resolve, reject){
browser.storage.local.get(prop, function(response){
resolve(response);
});
});
}
var _sm_getopt_async = async function(prop){
if(Debug.debug && Debug.debugStorage)
console.log("[StorageManager::_sm_getopt_async] requesting prop",prop,"from localStorage.");
if(BrowserDetect.usebrowser == "chrome"){
var ret = await _sm_chrome_getopt_wrapper(prop);
return ret;
}
else{
var ret = await browser.storage.local.get(prop);
if(Debug.debug && Debug.debugStorage)
console.log("[StorageManager::_sm_getopt_async] got prop", prop, "; value: ", ret);
return ret;
}
}
var _sm_delopt = function(item){
return browser.storage.local.remove(item);
}
var StorageManager = {
setopt: _sm_setopt,
getopt: _sm_getopt,
delopt: _sm_delopt,
getopt_async: _sm_getopt_async
}

View File

@ -150,7 +150,7 @@ class ArDetector {
this.canvasImageDataRowLength = cwidth << 2;
this.noLetterboxCanvasReset = false;
if(forceStart || canStartAutoAr() ) {
if(forceStart || this.settings.canStartAutoAr() ) {
this.start();
}
}

View File

@ -15,7 +15,9 @@ class UWServer {
}
async setup() {
await Settings.init();
this.settings = new Settings();
await this.settings.init();
this.comms = new CommsServer(this);

View File

@ -15,6 +15,7 @@ if(Debug.debug){
var pageInfo;
var comms;
var settings;
async function init(){
if(Debug.debug)
@ -23,32 +24,36 @@ async function init(){
comms = new CommsClient('content-client-port');
// load settings
var settingsLoaded = await comms.requestSettings();
if(!settingsLoaded){
if(Debug.debug) {
console.log("[uw::main] failed to get settings (settingsLoaded=",settingsLoaded,") Waiting for settings the old fashioned way");
}
comms.requestSettings_fallback();
await comms.waitForSettings();
if(Debug.debug){
console.log("[uw::main] settings loaded.");
}
}
// var settingsLoaded = await comms.requestSettings();
// if(!settingsLoaded){
// if(Debug.debug) {
// console.log("[uw::main] failed to get settings (settingsLoaded=",settingsLoaded,") Waiting for settings the old fashioned way");
// }
// comms.requestSettings_fallback();
// await comms.waitForSettings();
// if(Debug.debug){
// console.log("[uw::main] settings loaded.");
// }
// }
if(Debug.debug)
console.log("[uw::main] configuration should be loaded now");
// if(Debug.debug)
// console.log("[uw::main] configuration should be loaded now");
settings = new Settings();
await settings.init();
console.log("SETTINGS SHOULD BE LOADED NOW!", settings)
// če smo razširitev onemogočili v nastavitvah, ne naredimo ničesar
// If extension is soft-disabled, don't do shit
if(! canStartExtension()){
if(! settings.canStartExtension()){
if(Debug.debug) {
console.log("[uw::init] EXTENSION DISABLED, THEREFORE WONT BE STARTED")
}
return;
}
pageInfo = new PageInfo();
pageInfo = new PageInfo(comms, settings);
comms.setPageInfo(pageInfo);
if(Debug.debug){

View File

@ -2,6 +2,11 @@
"manifest_version": 2,
"name": "Ultrawidify",
"version": "3.0.2a",
"applications": {
"gecko": {
"id": "{cf02b1a7-a01a-4e37-a609-516a283f1ed3}"
}
},
"icons": {
"32":"res/icons/uw-32.png",
@ -16,16 +21,12 @@
"js/conf/Debug.js",
"js/lib/BrowserDetect.js",
"js/conf/Settings.active..js",
"js/run/GlobalVars.js",
"js/lib/StorageManager.js",
"js/lib/Comms.js",
"js/conf/ExtensionConf.js",
"js/lib/ObjectCopy.js",
"js/lib/Settings.js",
"js/conf/SitesConf.js",
"js/conf/Status.js",
"js/lib/Comms.js",
"js/lib/EdgeDetect.js",
"js/lib/GuardLine.js",
@ -54,7 +55,7 @@
"js/conf/Debug.js",
"js/lib/BrowserDetect.js",
"js/conf/Settings.active..js",
"js/conf/ExtensionConf.js",
"js/lib/StorageManager.js",
"js/lib/Comms.js",

View File

@ -241,6 +241,9 @@ function openMenu(menu){
}
for(var m in Menu){
if(Menu[m] === null)
continue; //todo: remove menus that are no longer there
Menu[m].classList.add("hidden");
}
for(var m in MenuTab){