Skip to content

Commit

Permalink
Chrome plugin compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
coldsource committed Aug 10, 2020
1 parent 113369d commit d361816
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 12 deletions.
26 changes: 20 additions & 6 deletions browser-plugin/background.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
if(browser === undefined)
{
var browser = chrome;

function lsget(key) {
return new Promise((resolve, reject) => {
chrome.storage.local.get(key, (data) => {
resolve(data);
});
});
}
}
else
var lsget = browser.storage.local.get;

browser.browserAction.onClicked.addListener(() => {
browser.tabs.create({
url: "/htdocs/index.html"
});
browser.tabs.create({
url: "/htdocs/index.html"
});
});

browser.runtime.onInstalled.addListener(() => {
browser.storage.local.get().then( (data) => {
console.log(data);
lsget('clusters').then( (data) => {
if(data.clusters===undefined)
{
browser.tabs.create({
url: "/htdocs/index.html?loc=settings"
});
}
});
});
});
19 changes: 17 additions & 2 deletions htdocs/js/src/components/base/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,27 @@ import {PageInstancesStatistics} from '../../pages/instances-statistics.js';
import {Page404} from '../../pages/404.js';
import {PageAuth} from '../../pages/auth.js';
import {PageSettings} from '../../pages/settings.js';
import {LS} from '../../utils/local-storage.js';

export class App extends React.Component {
constructor(props) {
super(props);

this.wrap_context = typeof(browser)=='undefined'?'server':'extension';
this.wrap_context = 'server';
if(typeof(chrome)!='undefined')
{
// Chrome
if(chrome.storage!==undefined)
this.wrap_context = 'extension';
}
else
{
// Firefrox
if(typeof(browser)!='undefined' && browser.storage!==undefined)
this.wrap_context = 'extension';
}

App.wrap_context = this.wrap_context;

App.global = {instance: this};

Expand Down Expand Up @@ -157,7 +172,7 @@ export class App extends React.Component {
else
{
// Plugin configuration, load configuration from browser storage
browser.storage.local.get().then( (data) => {
LS.get().then( (data) => {
if(data.clusters===undefined)
return reject("No configuration found");

Expand Down
5 changes: 3 additions & 2 deletions htdocs/js/src/pages/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {App} from '../components/base/app.js';
import {evQueueCluster} from '../evqueue/evqueue-cluster.js';
import {HeaderMenu} from '../components/menus/header.js';
import {EnvSelector} from '../components/base/env-selector.js';
import {LS} from '../utils/local-storage.js';

export class PageAuth extends React.Component {
constructor(props) {
Expand All @@ -48,9 +49,9 @@ export class PageAuth extends React.Component {
this.connect = this.connect.bind(this);

// Auto login in browser plugin configuration
if(typeof(browser)!='undefined' && env!==null)
if(App.wrap_context=='extension' && env!==null)
{
browser.storage.local.get().then( (data) => {
LS.get().then( (data) => {
if(data.clusters!==undefined && data.clusters[env]!==undefined)
{
let user = data.clusters[env].user;
Expand Down
5 changes: 3 additions & 2 deletions htdocs/js/src/pages/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {App} from '../components/base/app.js';
import {CryptoJS} from '../evqueue/cryptojs/core.js';
import {Tabs} from '../ui/tabs.js';
import {Tab} from '../ui/tab.js';
import {LS} from '../utils/local-storage.js';

export class PageSettings extends React.Component {
constructor(props) {
Expand All @@ -37,7 +38,7 @@ export class PageSettings extends React.Component {
this.save = this.save.bind(this);
this.onChange = this.onChange.bind(this);

browser.storage.local.get().then( (data) => {
LS.get().then( (data) => {
if(data.clusters!==undefined)
{
for(let name in data.clusters)
Expand Down Expand Up @@ -134,7 +135,7 @@ export class PageSettings extends React.Component {
clusters[name].password_clear = '';
}

browser.storage.local.set({clusters: config_clusters});
LS.set({clusters: config_clusters});
this.setState({clusters: clusters});
App.notice("Configuration saved");
}
Expand Down
41 changes: 41 additions & 0 deletions htdocs/js/src/utils/local-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This file is part of evQueue
*
* evQueue is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* evQueue is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with evQueue. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Thibault Kummer
*/

'use strict';

export class LS {
static get(key) {
if(typeof(browser)=='undefined')
{
return new Promise((resolve, reject) => {
chrome.storage.local.get(key, (data) => {
resolve(data);
});
});
}

return browser.storage.local.get(key);
}

static set(obj) {
if(typeof(browser)=='undefined')
return chrome.storage.local.set(obj);
return browser.storage.local.set(obj);
}
}

0 comments on commit d361816

Please sign in to comment.