refactor usw content script (#1300)
* extract handlers to async functions * limit event source to the current window
This commit is contained in:
parent
e1b65ae21f
commit
c34b054642
|
@ -2,48 +2,42 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
(() => {
|
(() => {
|
||||||
const allowedOrigin = 'https://userstyles.world';
|
const ORIGIN = 'https://userstyles.world';
|
||||||
|
const HANDLERS = Object.assign(Object.create(null), {
|
||||||
|
|
||||||
const sendPostMessage = message => {
|
async 'usw-ready'() {
|
||||||
if (allowedOrigin === location.origin) {
|
send({type: 'usw-remove-stylus-button'});
|
||||||
window.postMessage(message, location.origin);
|
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});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
const onPageLoaded = event => {
|
async 'usw-style-info-request'(data) {
|
||||||
if (event.data
|
switch (data.requestType) {
|
||||||
&& allowedOrigin === event.origin
|
case 'installed': {
|
||||||
) {
|
const updateUrl = `${ORIGIN}/api/style/${data.styleID}.user.css`;
|
||||||
switch (event.data.type) {
|
const style = await API.styles.find({updateUrl});
|
||||||
case 'usw-ready': {
|
send({
|
||||||
sendPostMessage({type: 'usw-remove-stylus-button'});
|
type: 'usw-style-info-response',
|
||||||
|
data: {installed: Boolean(style), requestType: 'installed'},
|
||||||
if (location.pathname === '/api/oauth/style/new') {
|
});
|
||||||
const styleId = Number(new URLSearchParams(location.search).get('vendor_data'));
|
|
||||||
API.data.pop('usw' + styleId).then(data => {
|
|
||||||
sendPostMessage({type: 'usw-fill-new-style', data});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'usw-style-info-request': {
|
|
||||||
switch (event.data.requestType) {
|
|
||||||
case 'installed': {
|
|
||||||
API.styles.find({updateUrl: `https://userstyles.world/api/style/${event.data.styleID}.user.css`})
|
|
||||||
.then(style => {
|
|
||||||
sendPostMessage({
|
|
||||||
type: 'usw-style-info-response',
|
|
||||||
data: {installed: Boolean(style), requestType: 'installed'},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
window.addEventListener('message', onPageLoaded);
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function send(msg) {
|
||||||
|
window.postMessage(msg, ORIGIN);
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user