forked from le-jeu/iitc-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup-plugin-iitcplugin.js
126 lines (115 loc) · 3.73 KB
/
rollup-plugin-iitcplugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const framework = `
// ensure plugin framework is there, even if iitc is not yet loaded
if(typeof window.plugin !== 'function') window.plugin = function() {};
`;
const wrapper = {
header: "function wrapper(plugin_info) {\n",
footer:
"setup.info = plugin_info; //add the script info data to the function as a property\n}\n",
};
const noWrapper = "setup.info = info;\n";
const injection = `
// inject code into site context
var info = {};
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) info.script = { version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description };
var script = document.createElement('script');
// if on last IITC mobile, will be replaced by wrapper(info)
var mobile = \`script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');'));
(document.body || document.head || document.documentElement).appendChild(script);\`;
// detect if mobile
if (mobile.startsWith('script')) {
script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');'));
script.appendChild(document.createTextNode('//# sourceURL=iitc:///plugins/@[email protected]'));
(document.body || document.head || document.documentElement).appendChild(script);
} else {
// mobile string
wrapper(info);
}
`;
export default function metablock(options = {}) {
const pluginId = options.id;
const baseConf = {
author: "anonymous",
name: "New plugin",
category: "Misc",
version: "0.1.0",
description: "custom plugin",
id: pluginId,
namespace: "https://github.com/IITC-CE/ingress-intel-total-conversion",
updateURL: false,
downloadURL: false,
match: "https://intel.ingress.com/*",
grant: "none",
};
if (options.meta) {
Object.keys(options.meta).forEach((key) => {
if (key in baseConf) baseConf[key] = options.meta[key];
});
}
const buildDate = new Date().toISOString();
if (options.timestamp) {
baseConf.version += "-" + buildDate.replace(/[-:]/g, "").slice(0, 15);
}
if (!options.withoutNamePrefix)
baseConf.name = "IITC plugin: " + baseConf.name;
if (options.downloadRoot) {
if (options.downloadRoot.slice(-1) !== "/") options.downloadRoot += "/";
baseConf.downloadURL = options.downloadRoot + pluginId + ".user.js";
baseConf.updateURL =
options.downloadRoot +
pluginId +
(options.updateMeta ? ".meta.js" : ".user.js");
}
const lines = [];
lines.push("// ==UserScript==");
for (const key in baseConf) {
if (baseConf[key]) {
lines.push(`// @${key.padEnd(13, " ")} ${baseConf[key]}`);
}
}
lines.push("// ==/UserScript==");
const header = lines.join("\n");
const useMeta = options.updateMeta;
const useWrapper = !options.noWrapper;
return {
banner() {
return (
header +
"\n" +
(useWrapper ? wrapper.header : "") +
framework
);
},
footer() {
return (
(useWrapper
? wrapper.footer + injection.replace("@plugin_id@", pluginId)
: noWrapper)
);
},
generateBundle() {
if (useMeta)
this.emitFile({
type: "asset",
fileName: pluginId + ".meta.js",
source: header,
});
},
resolveId (source, _, options ) {
if (options.isEntry && source.includes(pluginId)) {
return "iitc-plugin-entry-point"
}
return null;
},
load ( id ) {
if (id === 'iitc-plugin-entry-point') {
return `import setup from "./src/${pluginId}";
if(!window.bootPlugins) window.bootPlugins = [];
window.bootPlugins.push(setup);
// if IITC has already booted, immediately run the 'setup' function
if(window.iitcLoaded && typeof setup === 'function') setup();`;
}
return null;
}
};
}