2020-10-09 19:35:46 +02:00
|
|
|
import { PluginManifest } from './utils/types';
|
|
|
|
import ViewController from './ViewController';
|
2020-11-05 18:58:23 +02:00
|
|
|
import shim from '../../shim';
|
2020-10-09 19:35:46 +02:00
|
|
|
import { ViewHandle } from './utils/createViewHandle';
|
2020-10-21 01:23:55 +02:00
|
|
|
import { ContentScriptType } from './api/types';
|
2020-11-05 18:58:23 +02:00
|
|
|
import Logger from '../../Logger';
|
2020-10-22 15:51:59 +02:00
|
|
|
const EventEmitter = require('events');
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-11-19 17:25:02 +02:00
|
|
|
const logger = Logger.create('Plugin');
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
interface ViewControllers {
|
2020-11-12 21:29:22 +02:00
|
|
|
[key: string]: ViewController;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 01:23:55 +02:00
|
|
|
export interface ContentScript {
|
2020-11-12 21:29:22 +02:00
|
|
|
id: string;
|
|
|
|
path: string;
|
2020-10-21 01:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface ContentScripts {
|
2020-11-12 21:13:28 +02:00
|
|
|
[type: string]: ContentScript[];
|
2020-10-21 01:23:55 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
export default class Plugin {
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private baseDir_: string;
|
|
|
|
private manifest_: PluginManifest;
|
|
|
|
private scriptText_: string;
|
|
|
|
private viewControllers_: ViewControllers = {};
|
|
|
|
private contentScripts_: ContentScripts = {};
|
|
|
|
private dispatch_: Function;
|
|
|
|
private eventEmitter_: any;
|
2020-11-19 14:34:49 +02:00
|
|
|
private devMode_: boolean = false;
|
2021-01-12 01:33:10 +02:00
|
|
|
private messageListener_: Function = null;
|
|
|
|
private contentScriptMessageListeners_: Record<string, Function> = {};
|
2021-01-24 17:51:35 +02:00
|
|
|
private dataDir_: string;
|
|
|
|
private dataDirCreated_: boolean = false;
|
2020-11-12 21:13:28 +02:00
|
|
|
|
2021-01-24 17:51:35 +02:00
|
|
|
constructor(baseDir: string, manifest: PluginManifest, scriptText: string, dispatch: Function, dataDir: string) {
|
2020-10-09 19:35:46 +02:00
|
|
|
this.baseDir_ = shim.fsDriver().resolve(baseDir);
|
|
|
|
this.manifest_ = manifest;
|
|
|
|
this.scriptText_ = scriptText;
|
2020-10-21 01:23:55 +02:00
|
|
|
this.dispatch_ = dispatch;
|
2021-01-24 17:51:35 +02:00
|
|
|
this.dataDir_ = dataDir;
|
2020-10-22 15:51:59 +02:00
|
|
|
this.eventEmitter_ = new EventEmitter();
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public get id(): string {
|
2020-11-19 14:34:49 +02:00
|
|
|
return this.manifest.id;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-19 14:34:49 +02:00
|
|
|
public get devMode(): boolean {
|
|
|
|
return this.devMode_;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-19 14:34:49 +02:00
|
|
|
public set devMode(v: boolean) {
|
|
|
|
this.devMode_ = v;
|
2020-11-15 16:18:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public get manifest(): PluginManifest {
|
2020-10-09 19:35:46 +02:00
|
|
|
return this.manifest_;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public get scriptText(): string {
|
2020-10-09 19:35:46 +02:00
|
|
|
return this.scriptText_;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public get baseDir(): string {
|
2020-10-09 19:35:46 +02:00
|
|
|
return this.baseDir_;
|
|
|
|
}
|
|
|
|
|
2021-01-24 17:51:35 +02:00
|
|
|
public async dataDir(): Promise<string> {
|
|
|
|
if (this.dataDirCreated_) return this.dataDir_;
|
|
|
|
|
|
|
|
if (!(await shim.fsDriver().exists(this.dataDir_))) {
|
|
|
|
await shim.fsDriver().mkdir(this.dataDir_);
|
|
|
|
this.dataDirCreated_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.dataDir_;
|
|
|
|
}
|
|
|
|
|
2020-11-13 19:09:28 +02:00
|
|
|
public get viewCount(): number {
|
|
|
|
return Object.keys(this.viewControllers_).length;
|
|
|
|
}
|
|
|
|
|
2020-12-11 14:29:21 +02:00
|
|
|
public on(eventName: string, callback: Function) {
|
2020-10-22 15:51:59 +02:00
|
|
|
return this.eventEmitter_.on(eventName, callback);
|
|
|
|
}
|
|
|
|
|
2020-12-11 14:29:21 +02:00
|
|
|
public off(eventName: string, callback: Function) {
|
2020-10-22 15:51:59 +02:00
|
|
|
return this.eventEmitter_.removeListener(eventName, callback);
|
|
|
|
}
|
|
|
|
|
2020-12-11 14:29:21 +02:00
|
|
|
public emit(eventName: string, event: any = null) {
|
2020-10-22 15:51:59 +02:00
|
|
|
return this.eventEmitter_.emit(eventName, event);
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public async registerContentScript(type: ContentScriptType, id: string, path: string) {
|
2020-10-21 01:23:55 +02:00
|
|
|
if (!this.contentScripts_[type]) this.contentScripts_[type] = [];
|
|
|
|
|
|
|
|
const absolutePath = shim.fsDriver().resolveRelativePathWithinDir(this.baseDir, path);
|
|
|
|
|
2020-10-22 15:51:59 +02:00
|
|
|
if (!(await shim.fsDriver().exists(absolutePath))) throw new Error(`Could not find content script at path ${absolutePath}`);
|
|
|
|
|
2020-10-21 01:23:55 +02:00
|
|
|
this.contentScripts_[type].push({ id, path: absolutePath });
|
|
|
|
|
2020-11-19 17:25:02 +02:00
|
|
|
logger.debug(`"${this.id}": Registered content script: ${type}: ${id}: ${absolutePath}`);
|
2020-10-21 01:23:55 +02:00
|
|
|
|
|
|
|
this.dispatch_({
|
|
|
|
type: 'PLUGIN_CONTENT_SCRIPTS_ADD',
|
|
|
|
pluginId: this.id,
|
|
|
|
contentScript: {
|
|
|
|
type: type,
|
|
|
|
id: id,
|
|
|
|
path: absolutePath,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public contentScriptsByType(type: ContentScriptType): ContentScript[] {
|
2020-10-21 01:23:55 +02:00
|
|
|
return this.contentScripts_[type] ? this.contentScripts_[type] : [];
|
|
|
|
}
|
|
|
|
|
2021-01-12 01:33:10 +02:00
|
|
|
public contentScriptById(id: string): ContentScript {
|
|
|
|
for (const type in this.contentScripts_) {
|
|
|
|
const cs = this.contentScripts_[type];
|
|
|
|
for (const c of cs) {
|
|
|
|
if (c.id === id) return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public addViewController(v: ViewController) {
|
2020-11-13 19:09:28 +02:00
|
|
|
if (this.viewControllers_[v.handle]) throw new Error(`View already added or there is already a view with this ID: ${v.handle}`);
|
2020-10-09 19:35:46 +02:00
|
|
|
this.viewControllers_[v.handle] = v;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public viewController(handle: ViewHandle): ViewController {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (!this.viewControllers_[handle]) throw new Error(`View not found: ${handle}`);
|
|
|
|
return this.viewControllers_[handle];
|
|
|
|
}
|
|
|
|
|
2020-11-13 19:09:28 +02:00
|
|
|
public deprecationNotice(goneInVersion: string, message: string) {
|
2020-11-19 17:25:02 +02:00
|
|
|
logger.warn(`"${this.id}": DEPRECATION NOTICE: ${message} This will stop working in version ${goneInVersion}.`);
|
2020-11-13 19:09:28 +02:00
|
|
|
}
|
|
|
|
|
2021-01-12 01:33:10 +02:00
|
|
|
public emitMessage(message: any) {
|
|
|
|
if (!this.messageListener_) return;
|
|
|
|
return this.messageListener_(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public onMessage(callback: any) {
|
|
|
|
this.messageListener_ = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
public onContentScriptMessage(id: string, callback: any) {
|
|
|
|
if (!this.contentScriptById(id)) {
|
|
|
|
// The script could potentially be registered later on, but still
|
|
|
|
// best to print a warning to notify the user of a possible bug.
|
|
|
|
logger.warn(`onContentScriptMessage: No such content script: ${id}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.contentScriptMessageListeners_[id] = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
public emitContentScriptMessage(id: string, message: any) {
|
|
|
|
if (!this.contentScriptMessageListeners_[id]) return;
|
|
|
|
return this.contentScriptMessageListeners_[id](message);
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|