add fake header before linking to USW

This commit is contained in:
tophf 2022-09-11 20:44:19 +03:00
parent cda606e7cc
commit 527d7c0fbc

View File

@ -35,7 +35,7 @@ const uswApi = (() => {
const maxKeyLen = meta.reduce((res, [k]) => Math.max(res, k.length), 0); const maxKeyLen = meta.reduce((res, [k]) => Math.max(res, k.length), 0);
return [ return [
'/* ==UserStyle==', '/* ==UserStyle==',
...meta.map(([k, v]) => `${k}${' '.repeat(maxKeyLen - k.length + 2)}${v || ''}`), ...meta.map(([k, v]) => v && `${k}${' '.repeat(maxKeyLen - k.length + 2)}${v}`).filter(Boolean),
'==/UserStyle== */', '==/UserStyle== */',
].join('\n') + '\n\n'; ].join('\n') + '\n\n';
} }
@ -77,14 +77,15 @@ const uswApi = (() => {
*/ */
async publish(id, sourceCode) { async publish(id, sourceCode) {
const style = await API.styles.get(id); const style = await API.styles.get(id);
const code = style.usercssData ? sourceCode
: fakeUsercssHeader(style) + sourceCode;
const data = (style._usw || {}).token const data = (style._usw || {}).token
? style._usw ? style._usw
: await linkStyle(style, sourceCode); : await linkStyle(style, code);
const header = style.usercssData ? '' : fakeUsercssHeader(style);
return uswFetch(`style/${data.id}`, data.token, { return uswFetch(`style/${data.id}`, data.token, {
method: 'POST', method: 'POST',
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
body: JSON.stringify({code: header + sourceCode}), body: JSON.stringify({code}),
}); });
}, },