-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup-plugin-iitcplugin.js
138 lines (124 loc) · 4.15 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
127
128
129
130
131
132
133
134
135
136
137
138
import MagicString from "magic-string";
const plugin_info = `
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 };
info.buildName = '@build_name@';
info.dateTimeVersion = '@build_date@';
info.pluginId = '@plugin_id@';
`;
const framework = {
header: `
// ensure plugin framework is there, even if iitc is not yet loaded
if(typeof window.plugin !== 'function') window.plugin = function() {};
var setup;
function define(fun) { setup = fun; }
`,
footer: `
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();
`,
};
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 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);
}
const namePrefix = options.namePrefix === false ? "" : (options.namePrefix || "IITC plugin: ");
baseConf.name = namePrefix + 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;
const pluginInfo = plugin_info
.replace("@build_date@", buildDate)
.replace("@plugin_id@", pluginId)
.replace("@build_name@", options.buildName || "rollup");
return {
generateBundle() {
if (useMeta)
this.emitFile({
type: "asset",
fileName: pluginId + ".meta.js",
source: header,
});
},
renderChunk(code, renderedChunk, outputOptions) {
const magicString = new MagicString(code)
.prepend(
header +
"\n" +
pluginInfo +
(useWrapper ? wrapper.header : "") +
framework.header
)
.append(
framework.footer +
(useWrapper
? wrapper.footer + injection.replace("@plugin_id@", pluginId)
: noWrapper)
);
return {
code: magicString.toString(),
map: magicString.generateMap({ hires: true }),
};
},
};
}