2021-06-29 09:36:59 +00:00
|
|
|
/* global API */// msg.js
|
2021-04-30 13:39:07 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
(() => {
|
2021-07-30 14:04:15 +00:00
|
|
|
const ORIGIN = 'https://userstyles.world';
|
|
|
|
const HANDLERS = Object.assign(Object.create(null), {
|
2021-04-30 13:39:07 +00:00
|
|
|
|
2021-07-30 14:04:15 +00:00
|
|
|
async 'usw-ready'() {
|
|
|
|
send({type: 'usw-remove-stylus-button'});
|
|
|
|
if (location.pathname === '/api/oauth/style/new') {
|
|
|
|
const styleId = Number(new URLSearchParams(location.search).get('vendor_data'));
|
|
|
|
const data = await API.data.pop('usw' + styleId);
|
|
|
|
send({type: 'usw-fill-new-style', data});
|
|
|
|
}
|
|
|
|
},
|
2021-06-29 09:36:59 +00:00
|
|
|
|
2021-07-30 14:04:15 +00:00
|
|
|
async 'usw-style-info-request'(data) {
|
|
|
|
switch (data.requestType) {
|
|
|
|
case 'installed': {
|
|
|
|
const updateUrl = `${ORIGIN}/api/style/${data.styleID}.user.css`;
|
|
|
|
const style = await API.styles.find({updateUrl});
|
|
|
|
send({
|
|
|
|
type: 'usw-style-info-response',
|
|
|
|
data: {installed: Boolean(style), requestType: 'installed'},
|
|
|
|
});
|
2021-07-30 12:52:27 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-06-29 09:36:59 +00:00
|
|
|
}
|
2021-07-30 14:04:15 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener('message', ({data, source, origin}) => {
|
|
|
|
// Some browser don't reveal `source` to extensions e.g. Firefox
|
|
|
|
if (data && (source ? source === window : origin === ORIGIN)) {
|
|
|
|
const fn = HANDLERS[data.type];
|
|
|
|
if (fn) fn(data);
|
2021-04-30 13:39:07 +00:00
|
|
|
}
|
2021-07-30 14:04:15 +00:00
|
|
|
});
|
2021-04-30 13:39:07 +00:00
|
|
|
|
2021-07-30 14:04:15 +00:00
|
|
|
function send(msg) {
|
|
|
|
window.postMessage(msg, ORIGIN);
|
|
|
|
}
|
2021-04-30 13:39:07 +00:00
|
|
|
})();
|