mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-12 18:39:58 +00:00
Add content-script-side functions to messageBridge
This commit is contained in:
@@ -1,38 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
|
|
||||||
const backgroundPort = browser.runtime.connect({
|
|
||||||
name: "shim"
|
|
||||||
});
|
|
||||||
|
|
||||||
backgroundPort.onMessage.addListener(message => {
|
|
||||||
const event = new CustomEvent("__castMessage", {
|
|
||||||
detail: JSON.stringify(message)
|
|
||||||
});
|
|
||||||
document.dispatchEvent(event);
|
|
||||||
});
|
|
||||||
|
|
||||||
let popupPort;
|
|
||||||
browser.runtime.onConnect.addListener(port => {
|
|
||||||
if (port.name === "popup") {
|
|
||||||
popupPort = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
port.onMessage.addListener(message => {
|
|
||||||
const event = new CustomEvent("__castMessage", {
|
|
||||||
detail: JSON.stringify(message)
|
|
||||||
});
|
|
||||||
document.dispatchEvent(event);
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
document.addEventListener("__castMessageResponse", ev => {
|
|
||||||
const [ destination ] = ev.detail.subject.split(":/");
|
|
||||||
if (destination === "popup") {
|
|
||||||
if (popupPort) {
|
|
||||||
popupPort.postMessage(ev.detail);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
backgroundPort.postMessage(ev.detail);
|
|
||||||
});
|
|
||||||
@@ -122,7 +122,7 @@ browser.webRequest.onBeforeRequest.addListener(
|
|||||||
case SENDER_SCRIPT_URL:
|
case SENDER_SCRIPT_URL:
|
||||||
// Content/Page script bridge
|
// Content/Page script bridge
|
||||||
await browser.tabs.executeScript(details.tabId, {
|
await browser.tabs.executeScript(details.tabId, {
|
||||||
file: "content.js"
|
file: "shim/content.js"
|
||||||
, frameId: details.frameId
|
, frameId: details.frameId
|
||||||
, runAt: "document_start"
|
, runAt: "document_start"
|
||||||
});
|
});
|
||||||
@@ -257,7 +257,7 @@ browser.runtime.onMessage.addListener(message => {
|
|||||||
// Defines window.chrome for site compatibility
|
// Defines window.chrome for site compatibility
|
||||||
browser.contentScripts.register({
|
browser.contentScripts.register({
|
||||||
allFrames: true
|
allFrames: true
|
||||||
, js: [{ file: "contentSetup.js" }]
|
, js: [{ file: "shim/contentSetup.js" }]
|
||||||
, matches: [ "<all_urls>" ]
|
, matches: [ "<all_urls>" ]
|
||||||
, runAt: "document_start"
|
, runAt: "document_start"
|
||||||
});
|
});
|
||||||
@@ -276,7 +276,7 @@ browser.menus.onClicked.addListener(async (info, tab) => {
|
|||||||
|
|
||||||
// Load cast setup script
|
// Load cast setup script
|
||||||
await browser.tabs.executeScript(tab.id, {
|
await browser.tabs.executeScript(tab.id, {
|
||||||
file: "content.js"
|
file: "shim/content.js"
|
||||||
, frameId
|
, frameId
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import { SessionStatus
|
|||||||
, ErrorCode
|
, ErrorCode
|
||||||
, VolumeControlType } from "../enums";
|
, VolumeControlType } from "../enums";
|
||||||
|
|
||||||
import { onMessage, sendMessage } from "../../messageBridge";
|
import { onMessage, sendMessageResponse } from "../../messageBridge";
|
||||||
|
|
||||||
import uuid from "uuid/v1";
|
import uuid from "uuid/v1";
|
||||||
|
|
||||||
@@ -182,7 +182,7 @@ export default class Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_sendMessage (subject, data = {}) {
|
_sendMessage (subject, data = {}) {
|
||||||
sendMessage({
|
sendMessageResponse({
|
||||||
subject
|
subject
|
||||||
, data
|
, data
|
||||||
, _id: this._id
|
, _id: this._id
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import { requestSession as requestSessionTimeout } from "../timeout";
|
|||||||
|
|
||||||
import state from "../state";
|
import state from "../state";
|
||||||
|
|
||||||
import { onMessage, sendMessage } from "../messageBridge";
|
import { onMessage, sendMessageResponse } from "../messageBridge";
|
||||||
|
|
||||||
|
|
||||||
const cast = {
|
const cast = {
|
||||||
@@ -90,7 +90,7 @@ cast.initialize = (
|
|||||||
|
|
||||||
state.apiConfig = apiConfig;
|
state.apiConfig = apiConfig;
|
||||||
|
|
||||||
sendMessage({
|
sendMessageResponse({
|
||||||
subject: "bridge:/discover"
|
subject: "bridge:/discover"
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -145,7 +145,7 @@ cast.requestSession = (
|
|||||||
sessionErrorCallback = errorCallback;
|
sessionErrorCallback = errorCallback;
|
||||||
|
|
||||||
// Open destination chooser
|
// Open destination chooser
|
||||||
sendMessage({
|
sendMessageResponse({
|
||||||
subject: "main:/openPopup"
|
subject: "main:/openPopup"
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -223,7 +223,7 @@ onMessage(message => {
|
|||||||
, [] // appImages
|
, [] // appImages
|
||||||
, selectedReceiver // receiver
|
, selectedReceiver // receiver
|
||||||
, (session) => {
|
, (session) => {
|
||||||
sendMessage({
|
sendMessageResponse({
|
||||||
subject: "popup:/close"
|
subject: "popup:/close"
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -257,7 +257,7 @@ onMessage(message => {
|
|||||||
* chooser.
|
* chooser.
|
||||||
*/
|
*/
|
||||||
case "shim:/popupReady": {
|
case "shim:/popupReady": {
|
||||||
sendMessage({
|
sendMessageResponse({
|
||||||
subject: "popup:/populateReceiverList"
|
subject: "popup:/populateReceiverList"
|
||||||
, data: {
|
, data: {
|
||||||
receivers: state.receiverList
|
receivers: state.receiverList
|
||||||
|
|||||||
34
ext/src/shim/content.js
Normal file
34
ext/src/shim/content.js
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
import { onMessageResponse, sendMessage } from "./messageBridge";
|
||||||
|
|
||||||
|
|
||||||
|
const backgroundPort = browser.runtime.connect({
|
||||||
|
name: "shim"
|
||||||
|
});
|
||||||
|
backgroundPort.onMessage.addListener(sendMessage);
|
||||||
|
|
||||||
|
let popupPort;
|
||||||
|
browser.runtime.onConnect.addListener(port => {
|
||||||
|
if (port.name === "popup") {
|
||||||
|
popupPort = port;
|
||||||
|
}
|
||||||
|
port.onMessage.addListener(sendMessage)
|
||||||
|
});
|
||||||
|
|
||||||
|
onMessageResponse(message => {
|
||||||
|
const [ destination ] = message.subject.split(":/");
|
||||||
|
switch (destination) {
|
||||||
|
case "popup": {
|
||||||
|
if (popupPort) {
|
||||||
|
popupPort.postMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
default: {
|
||||||
|
backgroundPort.postMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
import cast from "./cast";
|
import cast from "./cast";
|
||||||
import media from "./media";
|
import media from "./media";
|
||||||
|
|
||||||
import { onMessage, sendMessage } from "./messageBridge";
|
import { onMessage } from "./messageBridge";
|
||||||
|
|
||||||
|
|
||||||
if (!window.chrome) {
|
if (!window.chrome) {
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { PlayerState
|
|||||||
import _Error from "../../cast/classes/Error";
|
import _Error from "../../cast/classes/Error";
|
||||||
import { ErrorCode } from "../../cast/enums";
|
import { ErrorCode } from "../../cast/enums";
|
||||||
|
|
||||||
import { onMessage, sendMessage } from "../../messageBridge";
|
import { onMessage, sendMessageResponse } from "../../messageBridge";
|
||||||
|
|
||||||
import uuid from "uuid/v1";
|
import uuid from "uuid/v1";
|
||||||
|
|
||||||
@@ -93,7 +93,7 @@ export default class Media {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_sendMessage (subject, data) {
|
_sendMessage (subject, data) {
|
||||||
sendMessage({
|
sendMessageResponse({
|
||||||
subject
|
subject
|
||||||
, data
|
, data
|
||||||
, _id: this._id
|
, _id: this._id
|
||||||
|
|||||||
@@ -1,15 +1,40 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
export function onMessage (listener) {
|
export function onMessage (listener) {
|
||||||
document.addEventListener("__castMessage", ev => {
|
document.addEventListener("__castMessage", ev => {
|
||||||
listener(JSON.parse(ev.detail));
|
listener(JSON.parse(ev.detail));
|
||||||
});
|
|
||||||
|
/**
|
||||||
|
* TODO:
|
||||||
|
* Figure out a way to handle and stop propagation of this
|
||||||
|
* event to hide it from page scripts.
|
||||||
|
* Currently the event handler is set after the page loads the
|
||||||
|
* cast API, allowing pages set handlers before this script,
|
||||||
|
* intercept the event, and cancel it.
|
||||||
|
*/
|
||||||
|
ev.stopPropagation();
|
||||||
|
}, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sendMessageResponse (message) {
|
||||||
|
const event = new CustomEvent("__castMessageResponse", {
|
||||||
|
detail: JSON.stringify(message)
|
||||||
|
});
|
||||||
|
|
||||||
|
document.dispatchEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function onMessageResponse (listener) {
|
||||||
|
document.addEventListener("__castMessageResponse", ev => {
|
||||||
|
listener(JSON.parse(ev.detail));
|
||||||
|
}, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sendMessage (message) {
|
export function sendMessage (message) {
|
||||||
const event = new CustomEvent("__castMessageResponse", {
|
const event = new CustomEvent("__castMessage", {
|
||||||
detail: message
|
detail: JSON.stringify(message)
|
||||||
});
|
});
|
||||||
|
|
||||||
document.dispatchEvent(event);
|
document.dispatchEvent(event);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,13 +9,15 @@ module.exports = (env) => ({
|
|||||||
"main" : `${env.includePath}/main.js`
|
"main" : `${env.includePath}/main.js`
|
||||||
, "popup/bundle" : `${env.includePath}/popup/index.jsx`
|
, "popup/bundle" : `${env.includePath}/popup/index.jsx`
|
||||||
, "options/bundle" : `${env.includePath}/options/index.jsx`
|
, "options/bundle" : `${env.includePath}/options/index.jsx`
|
||||||
, "shim/bundle" : `${env.includePath}/shim/index.js`
|
|
||||||
, "updater/bundle" : `${env.includePath}/updater/index.jsx`
|
, "updater/bundle" : `${env.includePath}/updater/index.jsx`
|
||||||
, "content" : `${env.includePath}/content.js`
|
|
||||||
, "contentSetup" : `${env.includePath}/contentSetup.js`
|
|
||||||
, "mediaCast" : `${env.includePath}/mediaCast.js`
|
, "mediaCast" : `${env.includePath}/mediaCast.js`
|
||||||
, "mirroringCast" : `${env.includePath}/mirroringCast.js`
|
, "mirroringCast" : `${env.includePath}/mirroringCast.js`
|
||||||
, "compat/youtube" : `${env.includePath}/compat/youtube.js`
|
, "compat/youtube" : `${env.includePath}/compat/youtube.js`
|
||||||
|
|
||||||
|
// Shim entries
|
||||||
|
, "shim/bundle" : `${env.includePath}/shim/index.js`
|
||||||
|
, "shim/content" : `${env.includePath}/shim/content.js`
|
||||||
|
, "shim/contentSetup" : `${env.includePath}/shim/contentSetup.js`
|
||||||
}
|
}
|
||||||
, output: {
|
, output: {
|
||||||
filename: "[name].js"
|
filename: "[name].js"
|
||||||
|
|||||||
Reference in New Issue
Block a user