Skip to content

Commit

Permalink
Fixed export of module functions (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
modos189 authored May 1, 2022
1 parent aa5492b commit cccefc6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 34 deletions.
4 changes: 4 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,8 @@ export function clearWait() {
wait_timeout_id = null;
}

export function isSet(value) {
return typeof value !== 'undefined' && value !== null;
}

export const _check_url_match_pattern = check_url_match_pattern;
21 changes: 18 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3

import { Manager } from './manager.js';
export { Manager };
import {
parseMeta,
ajaxGet,
getUniqId,
getUID,
check_meta_match_pattern,
wait,
clearWait
} from './helpers.js';

module.exports = {
Manager: Manager
export {
Manager,
parseMeta,
ajaxGet,
getUniqId,
getUID,
check_meta_match_pattern,
wait,
clearWait
};
50 changes: 21 additions & 29 deletions src/manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3

import * as helpers from './helpers.js';
import * as migrations from './migrations.js';
import {parseMeta, ajaxGet, getUID, wait, clearWait, isSet} from './helpers.js';

export class Manager {
constructor(config) {
Expand Down Expand Up @@ -62,18 +62,18 @@ export class Manager {
} catch {
// Ignore if there is no message receiver
}
await helpers.wait(seconds);
await wait(seconds);
}

clearInterval(this.progress_interval_id);
this.progress_interval_id = setInterval(async () => {
await this._showProgress(true);
this.progressbar(true);
}, 300);
try {
const response = await helpers.ajaxGet(url, variant);
const response = await ajaxGet(url, variant);
if (response) {
clearInterval(this.progress_interval_id);
await this._showProgress(false);
this.progressbar(false);
}
return response;
} catch {
Expand All @@ -85,14 +85,6 @@ export class Manager {
}
}

async _showProgress(value) {
try {
this.progressbar(value);
} catch {
// Ignore if there is no message receiver
}
}

async checkUpdates(force) {
const local = await this.storage.get([
'channel',
Expand Down Expand Up @@ -133,10 +125,10 @@ export class Manager {
if (this.channel === 'local') update_check_interval = 5; // check every 5 seconds

if (
local[this.channel + '_last_modified'] === null ||
local.last_check_update === null
!isSet(local[this.channel + '_last_modified']) ||
!isSet(local.last_check_update)
) {
helpers.clearWait();
clearWait();
clearTimeout(this.update_timeout_id);
this.update_timeout_id = null;
await this._downloadMeta(local, null);
Expand All @@ -146,7 +138,7 @@ export class Manager {
update_check_interval -
local.last_check_update;
if (time_delta >= 0 || force) {
helpers.clearWait();
clearWait();
clearTimeout(this.update_timeout_id);
this.update_timeout_id = null;
const last_modified = await this._getUrl(
Expand Down Expand Up @@ -242,7 +234,7 @@ export class Manager {
if ('plugins' in categories[cat]) {
Object.keys(categories[cat]['plugins']).forEach(id => {
const plugin = categories[cat]['plugins'][id];
plugin['uid'] = helpers.getUID(plugin);
plugin['uid'] = getUID(plugin);
plugin['status'] = 'off';
plugin['category'] = cat;
plugins[plugin['uid']] = plugin;
Expand Down Expand Up @@ -308,7 +300,7 @@ export class Manager {
// download meta info
const response_meta = await this._getUrl(plugin['updateURL'] + hash);
if (response_meta) {
let meta = helpers.parseMeta(response_meta);
let meta = parseMeta(response_meta);
// if new version
if (
meta &&
Expand Down Expand Up @@ -337,7 +329,7 @@ export class Manager {

async _updateLocalPlugins(plugins_flat, plugins_local) {
// If no plugins installed
if (plugins_local === null) return {};
if (!isSet(plugins_local)) return {};

// Iteration local plugins
for (const uid of Object.keys(plugins_local)) {
Expand Down Expand Up @@ -365,8 +357,8 @@ export class Manager {
let plugins_local = local[this.channel + '_plugins_local'];
let plugins_user = local[this.channel + '_plugins_user'];

if (plugins_local === null) plugins_local = {};
if (plugins_user === null) plugins_user = {};
if (!isSet(plugins_local)) plugins_local = {};
if (!isSet(plugins_user)) plugins_user = {};

if (action === 'on') {
if (
Expand All @@ -381,7 +373,7 @@ export class Manager {
plugins_local[uid]['status'] = 'on';
}

await this.inject_user_script(
this.inject_user_script(
plugins_flat[uid]['user'] === true
? plugins_user[uid]['code']
: plugins_local[uid]['code']
Expand All @@ -402,7 +394,7 @@ export class Manager {
plugins_local[uid] = plugins_flat[uid];
plugins_local[uid]['code'] = response;

await this.inject_user_script(plugins_local[uid]['code']);
this.inject_user_script(plugins_local[uid]['code']);

await this._save({
plugins_flat: plugins_flat,
Expand Down Expand Up @@ -455,13 +447,13 @@ export class Manager {
let plugins_local = local[this.channel + '_plugins_local'];
let plugins_user = local[this.channel + '_plugins_user'];

if (plugins_local === null) plugins_local = {};
if (plugins_user === null) plugins_user = {};
if (!isSet(plugins_local)) plugins_local = {};
if (!isSet(plugins_user)) plugins_user = {};

scripts.forEach(script => {
let meta = script['meta'];
const code = script['code'];
const plugin_uid = helpers.getUID(meta);
const plugin_uid = getUID(meta);

if (plugin_uid === null) throw new Error('The plugin has an incorrect ==UserScript== header');

Expand Down Expand Up @@ -514,8 +506,8 @@ export class Manager {
plugins_user
) {
let data = {};
if (plugins_local === null) plugins_local = {};
if (plugins_user === null) plugins_user = {};
if (!isSet(plugins_local)) plugins_local = {};
if (!isSet(plugins_user)) plugins_user = {};

if (raw_plugins['Obsolete'] !== undefined) delete raw_plugins['Obsolete'];
if (raw_plugins['Deleted'] !== undefined) delete raw_plugins['Deleted'];
Expand Down
6 changes: 4 additions & 2 deletions src/migrations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3

import {isSet} from './helpers.js';

export async function migrate(storage) {

const migrates = [
Expand All @@ -13,7 +15,7 @@ export async function migrate(storage) {
'storage_version'
]);

if (local['storage_version'] === null && local['lastversion'] !== null) {
if (!isSet(local['storage_version']) && isSet(local['lastversion'])) {
local['storage_version'] = 0;
}

Expand All @@ -33,7 +35,7 @@ export async function migrate(storage) {

async function migration_0001(local) {
for (let channel of Object.keys(local)) {
if (local[channel] === null) continue;
if (!isSet(local[channel])) continue;

for (let plugin of Object.keys(local[channel])) {
const plugin_obj = local[channel][plugin];
Expand Down
3 changes: 3 additions & 0 deletions test/manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ describe('manage.js integration tests', function () {
inject_user_script: function callBack(data){
expect(data).to.include('// ==UserScript==');
},
progressbar: function callBack(is_show){
expect(is_show).to.be.oneOf([true, false]);
},
is_daemon: false,
};
manager = new Manager(params);
Expand Down

0 comments on commit cccefc6

Please sign in to comment.