feature: only try sending events if the origins match

This commit is contained in:
DecentM 2018-02-03 22:38:59 +01:00
parent e8caa02e1a
commit 90399c53a8
No known key found for this signature in database
GPG Key ID: 6BDA2D2BC5EA5B10

View File

@ -7,24 +7,26 @@
'https://openusercss.com'
];
const sendPostMessage = message => {
allowedOrigins.forEach(origin => {
if (origin === location.origin) {
window.postMessage(message, origin);
}
});
};
const askHandshake = () => {
// Tell the page that we exist and that it should send the handshake
allowedOrigins.forEach(origin => {
window.postMessage({
type: 'ouc-begin-handshake'
}, origin);
sendPostMessage({
type: 'ouc-begin-handshake'
});
};
// Listen for queries by the site and respond with a callback object
const sendInstalledCallback = styleData => {
allowedOrigins.forEach(origin => {
if (origin === location.origin) {
window.postMessage({
type: 'ouc-is-installed-response',
style: styleData
}, origin);
}
sendPostMessage({
type: 'ouc-is-installed-response',
style: styleData
});
};
@ -90,15 +92,13 @@
// We send the handshake response, which includes the key we got, plus some
// additional metadata
allowedOrigins.forEach(origin => {
window.postMessage({
type: 'ouc-handshake-response',
key: event.data.key,
extension: {
name: manifest.name,
capabilities: reportedFeatures
}
}, origin);
sendPostMessage({
type: 'ouc-handshake-response',
key: event.data.key,
extension: {
name: manifest.name,
capabilities: reportedFeatures
}
});
};
@ -118,11 +118,9 @@
const sendInstallCallback = data => {
// Send an install callback to the site in order to let it know
// we were able to install the theme and it may display a success message
allowedOrigins.forEach(origin => {
window.postMessage({
type: 'ouc-install-callback',
key: data.key
}, origin);
sendPostMessage({
type: 'ouc-install-callback',
key: data.key
});
};