zotero-plugin 3.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/create-element.d.ts +17 -0
- package/create-element.js +72 -0
- package/logger.d.ts +21 -0
- package/logger.js +99 -0
- package/package.json +6 -2
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare const NAMESPACE: {
|
|
2
|
+
XUL: string;
|
|
3
|
+
HTML: string;
|
|
4
|
+
};
|
|
5
|
+
type Handler = (event?: any) => void | Promise<void>;
|
|
6
|
+
export declare class Elements {
|
|
7
|
+
private prefix;
|
|
8
|
+
private document;
|
|
9
|
+
static all: WeakRef<Elements>[];
|
|
10
|
+
static removeAll(): void;
|
|
11
|
+
private className;
|
|
12
|
+
constructor(prefix: string, document: Document);
|
|
13
|
+
serialize(node: HTMLElement): string;
|
|
14
|
+
create(name: string, attrs?: Record<string, number | string | Handler | HTMLElement[]>): HTMLElement;
|
|
15
|
+
remove(): void;
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Elements = exports.NAMESPACE = void 0;
|
|
4
|
+
exports.NAMESPACE = {
|
|
5
|
+
XUL: 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul',
|
|
6
|
+
HTML: 'http://www.w3.org/1999/xhtml',
|
|
7
|
+
};
|
|
8
|
+
class Elements {
|
|
9
|
+
static removeAll() {
|
|
10
|
+
for (const ref of this.all) {
|
|
11
|
+
try {
|
|
12
|
+
const elements = ref.deref();
|
|
13
|
+
if (elements)
|
|
14
|
+
elements.document.querySelectorAll(`.${elements.className}`).forEach(e => e.remove());
|
|
15
|
+
}
|
|
16
|
+
catch (_a) { }
|
|
17
|
+
}
|
|
18
|
+
this.all = [];
|
|
19
|
+
}
|
|
20
|
+
constructor(prefix, document) {
|
|
21
|
+
this.prefix = prefix;
|
|
22
|
+
this.document = document;
|
|
23
|
+
this.className = `${prefix}-${Math.random()}-${Date.now()}`.replace('.', '-');
|
|
24
|
+
if (this.document.createXULElement)
|
|
25
|
+
Elements.all.push(new WeakRef(this));
|
|
26
|
+
}
|
|
27
|
+
serialize(node) {
|
|
28
|
+
const s = new XMLSerializer();
|
|
29
|
+
return s.serializeToString(node);
|
|
30
|
+
}
|
|
31
|
+
create(name, attrs = {}) {
|
|
32
|
+
const children = attrs.$ || [];
|
|
33
|
+
delete attrs.$;
|
|
34
|
+
const namespace = name.startsWith('html:') ? exports.NAMESPACE.HTML : exports.NAMESPACE.XUL;
|
|
35
|
+
name = name.replace('html:', '');
|
|
36
|
+
const elt = this.document.createXULElement
|
|
37
|
+
? this.document[namespace === exports.NAMESPACE.XUL ? 'createXULElement' : 'createElement'](name)
|
|
38
|
+
: this.document.createElementNS(namespace, name);
|
|
39
|
+
attrs.class = `${this.className} ${attrs.class || ''}`.trim();
|
|
40
|
+
for (const [a, v] of Object.entries(attrs)) {
|
|
41
|
+
if (typeof v === 'string') {
|
|
42
|
+
elt.setAttribute(a, v);
|
|
43
|
+
}
|
|
44
|
+
else if (typeof v === 'number') {
|
|
45
|
+
elt.setAttribute(a, `${v}`);
|
|
46
|
+
}
|
|
47
|
+
else if (a.startsWith('on') && typeof v === 'function') {
|
|
48
|
+
elt.addEventListener(a.replace('on', ''), event => {
|
|
49
|
+
var _a, _b;
|
|
50
|
+
;
|
|
51
|
+
(_b = (_a = v(event)) === null || _a === void 0 ? void 0 : _a.catch) === null || _b === void 0 ? void 0 : _b.call(_a, err => {
|
|
52
|
+
throw err;
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
throw new Error(`unexpected attribute ${a} of type ${typeof v}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
for (const child of children) {
|
|
61
|
+
elt.appendChild(child);
|
|
62
|
+
}
|
|
63
|
+
return elt;
|
|
64
|
+
}
|
|
65
|
+
remove() {
|
|
66
|
+
for (const elt of Array.from(this.document.getElementsByClassName(this.className))) {
|
|
67
|
+
elt.remove();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.Elements = Elements;
|
|
72
|
+
Elements.all = [];
|
package/logger.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const discard: {
|
|
2
|
+
log(): void;
|
|
3
|
+
error(): void;
|
|
4
|
+
warn(): void;
|
|
5
|
+
debug(): void;
|
|
6
|
+
info(): void;
|
|
7
|
+
clear(): void;
|
|
8
|
+
dir(): void;
|
|
9
|
+
table(): void;
|
|
10
|
+
};
|
|
11
|
+
export declare function format(...msg: any[]): string;
|
|
12
|
+
export declare class Logger {
|
|
13
|
+
#private;
|
|
14
|
+
id: string;
|
|
15
|
+
phase: string;
|
|
16
|
+
constructor(id: string);
|
|
17
|
+
debug(...msg: any[]): void;
|
|
18
|
+
info(...msg: any[]): void;
|
|
19
|
+
error(...msg: any[]): void;
|
|
20
|
+
dump(msg: string, error?: Error): void;
|
|
21
|
+
}
|
package/logger.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-empty-function, no-restricted-syntax */
|
|
3
|
+
var _Logger_instances, _Logger_prefix;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.Logger = exports.discard = void 0;
|
|
6
|
+
exports.format = format;
|
|
7
|
+
const tslib_1 = require("tslib");
|
|
8
|
+
exports.discard = {
|
|
9
|
+
log() { },
|
|
10
|
+
error() { },
|
|
11
|
+
warn() { },
|
|
12
|
+
debug() { },
|
|
13
|
+
info() { },
|
|
14
|
+
clear() { },
|
|
15
|
+
dir() { },
|
|
16
|
+
table() { },
|
|
17
|
+
};
|
|
18
|
+
function stringifyXPCOM(obj) {
|
|
19
|
+
if (!obj.QueryInterface)
|
|
20
|
+
return '';
|
|
21
|
+
if (obj.message)
|
|
22
|
+
return `[XPCOM error ${obj.message}]`;
|
|
23
|
+
if (obj.name)
|
|
24
|
+
return `[XPCOM object ${obj.name}]`;
|
|
25
|
+
return '[XPCOM object]';
|
|
26
|
+
}
|
|
27
|
+
function stringifyError(obj) {
|
|
28
|
+
if (obj instanceof Error)
|
|
29
|
+
return `[error: ${obj.message || '<unspecified error>'}\n${obj.stack}]`;
|
|
30
|
+
// guess it is an errorevent
|
|
31
|
+
if (obj.error instanceof Error && obj.message)
|
|
32
|
+
return `[errorevent: ${obj.message} ${stringifyError(obj.error)}]`;
|
|
33
|
+
if (typeof ErrorEvent !== 'undefined' && obj instanceof ErrorEvent)
|
|
34
|
+
return `[errorevent: ${obj.message || '<unspecified errorevent>'}]`;
|
|
35
|
+
return '';
|
|
36
|
+
}
|
|
37
|
+
function replacer() {
|
|
38
|
+
const seen = new WeakSet();
|
|
39
|
+
return (key, value) => {
|
|
40
|
+
if (typeof value === 'object' && value !== null) {
|
|
41
|
+
if (seen.has(value))
|
|
42
|
+
return '[Circular]';
|
|
43
|
+
seen.add(value);
|
|
44
|
+
}
|
|
45
|
+
if (value === null)
|
|
46
|
+
return value;
|
|
47
|
+
if (value instanceof Set)
|
|
48
|
+
return [...value];
|
|
49
|
+
if (value instanceof Map)
|
|
50
|
+
return Object.fromEntries(value);
|
|
51
|
+
switch (typeof value) {
|
|
52
|
+
case 'string':
|
|
53
|
+
case 'number':
|
|
54
|
+
case 'boolean':
|
|
55
|
+
return value;
|
|
56
|
+
case 'object':
|
|
57
|
+
return stringifyXPCOM(value) || stringifyError(value) || value;
|
|
58
|
+
}
|
|
59
|
+
if (Array.isArray(value))
|
|
60
|
+
return value;
|
|
61
|
+
return undefined;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function to_s(obj) {
|
|
65
|
+
if (typeof obj === 'string')
|
|
66
|
+
return obj;
|
|
67
|
+
return JSON.stringify(obj, replacer(), 2);
|
|
68
|
+
}
|
|
69
|
+
function format(...msg) {
|
|
70
|
+
return msg.map(to_s).join(' ');
|
|
71
|
+
}
|
|
72
|
+
class Logger {
|
|
73
|
+
constructor(id) {
|
|
74
|
+
_Logger_instances.add(this);
|
|
75
|
+
this.id = id;
|
|
76
|
+
this.phase = '';
|
|
77
|
+
}
|
|
78
|
+
debug(...msg) {
|
|
79
|
+
Zotero.debug(`${tslib_1.__classPrivateFieldGet(this, _Logger_instances, "m", _Logger_prefix).call(this)}${format(...msg)}\n`);
|
|
80
|
+
}
|
|
81
|
+
info(...msg) {
|
|
82
|
+
Zotero.debug(`${tslib_1.__classPrivateFieldGet(this, _Logger_instances, "m", _Logger_prefix).call(this)}${format(...msg)}\n`);
|
|
83
|
+
}
|
|
84
|
+
error(...msg) {
|
|
85
|
+
Zotero.debug(`${tslib_1.__classPrivateFieldGet(this, _Logger_instances, "m", _Logger_prefix).call(this, true)}${format(...msg)}\n`);
|
|
86
|
+
}
|
|
87
|
+
dump(msg, error) {
|
|
88
|
+
if (error) {
|
|
89
|
+
dump(`${tslib_1.__classPrivateFieldGet(this, _Logger_instances, "m", _Logger_prefix).call(this, error)}${format(msg, error)}\n`);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
dump(`${tslib_1.__classPrivateFieldGet(this, _Logger_instances, "m", _Logger_prefix).call(this)}${format(msg)}\n`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.Logger = Logger;
|
|
97
|
+
_Logger_instances = new WeakSet(), _Logger_prefix = function _Logger_prefix(error) {
|
|
98
|
+
return `{${error ? 'error: ' : ''}${this.phase}${this.id}} `;
|
|
99
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zotero-plugin",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "Zotero plugin builder",
|
|
5
5
|
"homepage": "https://github.com/retorquere/zotero-plugin/wiki",
|
|
6
6
|
"bin": {
|
|
@@ -100,7 +100,11 @@
|
|
|
100
100
|
"version.d.ts",
|
|
101
101
|
"version.js",
|
|
102
102
|
"debug-log.js",
|
|
103
|
-
"debug-log.d.ts"
|
|
103
|
+
"debug-log.d.ts",
|
|
104
|
+
"create-element.js",
|
|
105
|
+
"create-element.d.ts",
|
|
106
|
+
"logger.js",
|
|
107
|
+
"logger.d.ts"
|
|
104
108
|
],
|
|
105
109
|
"bugs": {
|
|
106
110
|
"url": "https://github.com/retorquere/zotero-plugin/issues"
|