mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-12 10:39:57 +00:00
Remove compat scripts and move API URL redirection to contentSetup
This commit is contained in:
@@ -1,60 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
|
|
||||||
// Insert script before first script is run
|
|
||||||
document.addEventListener("beforescriptexecute", function onBeforeScriptExecute () {
|
|
||||||
document.removeEventListener("beforescriptexecute", onBeforeScriptExecute);
|
|
||||||
|
|
||||||
const scriptElement = document.createElement("script");
|
|
||||||
scriptElement.src = browser.runtime.getURL("vendor/webcomponents-lite.min.js");
|
|
||||||
document.head.prepend(scriptElement);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
const EXT_SENDER_SCRIPT_URLS = [
|
|
||||||
"chrome-extension://pkedcjkdefgpdelpbcmbmeomcjbeemfm/cast_sender.js"
|
|
||||||
, "chrome-extension://enhhojjnijigcajfphajepfemndkmdlo/cast_sender.js"
|
|
||||||
];
|
|
||||||
|
|
||||||
const SENDER_SCRIPT_URL = "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js";
|
|
||||||
|
|
||||||
|
|
||||||
// Store reference to original function
|
|
||||||
const _createElement = document.createElement;
|
|
||||||
|
|
||||||
function createElement () {
|
|
||||||
// Call original function
|
|
||||||
const element = _createElement.apply(this, arguments);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* If the new element being created is a <script> element,
|
|
||||||
* replace the src property setter to intercept the new value.
|
|
||||||
*
|
|
||||||
* If it matches Chrome's cast extension sender script URL,
|
|
||||||
* replace it with the standard API URL, the request for which
|
|
||||||
* is handled in the main script.
|
|
||||||
*/
|
|
||||||
if (element.nodeName === "SCRIPT") {
|
|
||||||
const { get, set } = Reflect.getOwnPropertyDescriptor(
|
|
||||||
Reflect.getPrototypeOf(element.wrappedJSObject), "src");
|
|
||||||
|
|
||||||
Reflect.defineProperty(element.wrappedJSObject, "src", {
|
|
||||||
configurable: true
|
|
||||||
, enumerable: true
|
|
||||||
, get
|
|
||||||
|
|
||||||
, set: exportFunction(function (value) {
|
|
||||||
if (EXT_SENDER_SCRIPT_URLS.includes(value)) {
|
|
||||||
return set.call(this, SENDER_SCRIPT_URL);
|
|
||||||
}
|
|
||||||
return set.call(this, value);
|
|
||||||
}, window)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return element;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Redefine page's document.createElement function
|
|
||||||
exportFunction(createElement, document, {
|
|
||||||
defineAs: "createElement"
|
|
||||||
});
|
|
||||||
4
ext/src/global.d.ts
vendored
4
ext/src/global.d.ts
vendored
@@ -7,8 +7,8 @@ declare const APPLICATION_NAME: string;
|
|||||||
declare const APPLICATION_VERSION: string;
|
declare const APPLICATION_VERSION: string;
|
||||||
|
|
||||||
|
|
||||||
declare interface Window {
|
declare interface Object {
|
||||||
wrappedJSObject: typeof Window;
|
wrappedJSObject: Object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -31,3 +31,6 @@ export function getWindowCenteredProps (
|
|||||||
, top: Math.floor(centerY - height / 2)
|
, top: Math.floor(centerY - height / 2)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const REMOTE_MATCH_PATTERN_REGEX = /^(?:(?:(\*|https?|ftp):\/\/(\*|(?:\*\.(?:[^\/\*:]\.?)+(?:[^\.])|[^\/\*:]*))?)(\/.*)|<all_urls>)$/;
|
||||||
|
|||||||
@@ -179,13 +179,15 @@ async function onBeforeSendHeaders (
|
|||||||
// Find and rewrite the User-Agent header
|
// Find and rewrite the User-Agent header
|
||||||
for (const header of details.requestHeaders) {
|
for (const header of details.requestHeaders) {
|
||||||
if (header.name.toLowerCase() === "user-agent") {
|
if (header.name.toLowerCase() === "user-agent") {
|
||||||
// TODO: Move this somewhere else
|
|
||||||
|
// TODO: Remove need for this
|
||||||
if (host.value === "www.youtube.com") {
|
if (host.value === "www.youtube.com") {
|
||||||
header.value = getChromeUserAgent(os, true);
|
header.value = getChromeUserAgent(os, true);
|
||||||
} else {
|
break;
|
||||||
header.value = currentUAString;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
header.value = currentUAString;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -195,6 +197,9 @@ async function onBeforeSendHeaders (
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates any extension state based on options changes.
|
||||||
|
*/
|
||||||
async function onOptionsUpdated (alteredOptions?: Array<(keyof Options)>) {
|
async function onOptionsUpdated (alteredOptions?: Array<(keyof Options)>) {
|
||||||
const { options } = await browser.storage.sync.get("options");
|
const { options } = await browser.storage.sync.get("options");
|
||||||
|
|
||||||
@@ -203,29 +208,35 @@ async function onOptionsUpdated (alteredOptions?: Array<(keyof Options)>) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const registerFunctions = {
|
/**
|
||||||
onBeforeSendHeaders () {
|
* Adds a webRequest listener that intercepts and modifies user agent based on
|
||||||
browser.webRequest.onBeforeSendHeaders.addListener(
|
*/
|
||||||
onBeforeSendHeaders
|
function register_userAgentWhitelist () {
|
||||||
, { urls: options.userAgentWhitelistEnabled
|
browser.webRequest.onBeforeSendHeaders.addListener(
|
||||||
? options.userAgentWhitelist
|
onBeforeSendHeaders
|
||||||
: [] }
|
, { urls: options.userAgentWhitelistEnabled
|
||||||
, [ "blocking", "requestHeaders" ]);
|
? options.userAgentWhitelist
|
||||||
}
|
: [] }
|
||||||
};
|
, [ "blocking", "requestHeaders" ]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function unregister_userAgentWhitelist () {
|
||||||
|
browser.webRequest.onBeforeSendHeaders.removeListener(
|
||||||
|
onBeforeSendHeaders);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (!alteredOptions) {
|
if (!alteredOptions) {
|
||||||
// If no altered properties specified, register all listeners
|
// If no altered properties specified, register all listeners
|
||||||
for (const func of Object.values(registerFunctions)) {
|
register_userAgentWhitelist();
|
||||||
func();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (alteredOptions.includes("userAgentWhitelist")
|
if (alteredOptions.includes("userAgentWhitelist")
|
||||||
|| alteredOptions.includes("userAgentWhitelistEnabled")) {
|
|| alteredOptions.includes("userAgentWhitelistEnabled")) {
|
||||||
|
|
||||||
browser.webRequest.onBeforeSendHeaders.removeListener(
|
unregister_userAgentWhitelist();
|
||||||
onBeforeSendHeaders);
|
register_userAgentWhitelist();
|
||||||
registerFunctions.onBeforeSendHeaders();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (alteredOptions.includes("mirroringEnabled")) {
|
if (alteredOptions.includes("mirroringEnabled")) {
|
||||||
|
|||||||
@@ -27,13 +27,7 @@
|
|||||||
, "background": {
|
, "background": {
|
||||||
"scripts": [ "main.js" ]
|
"scripts": [ "main.js" ]
|
||||||
}
|
}
|
||||||
, "content_scripts": [
|
|
||||||
{
|
|
||||||
"matches": [ "https://www.youtube.com/*" ]
|
|
||||||
, "js": [ "compat/youtube.js" ]
|
|
||||||
, "run_at": "document_start"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
, "content_security_policy": "CONTENT_SECURITY_POLICY"
|
, "content_security_policy": "CONTENT_SECURITY_POLICY"
|
||||||
, "default_locale": "en"
|
, "default_locale": "en"
|
||||||
, "manifest_version": 2
|
, "manifest_version": 2
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import Bridge from "./Bridge";
|
|||||||
import EditableList from "./EditableList";
|
import EditableList from "./EditableList";
|
||||||
|
|
||||||
import getBridgeInfo, { BridgeInfo } from "../lib/getBridgeInfo";
|
import getBridgeInfo, { BridgeInfo } from "../lib/getBridgeInfo";
|
||||||
|
import { REMOTE_MATCH_PATTERN_REGEX } from "../lib/utils";
|
||||||
|
|
||||||
|
|
||||||
const _ = browser.i18n.getMessage;
|
const _ = browser.i18n.getMessage;
|
||||||
@@ -26,8 +27,6 @@ browser.runtime.getPlatformInfo()
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
const MATCH_PATTERN_REGEX = /^(?:(?:(\*|https?|ftp):\/\/((?:\*\.|[^\/\*])+)|(file):\/\/\/?(?:\*\.|[^\/\*])+)(\/.*)|<all_urls>)$/;
|
|
||||||
|
|
||||||
function getInputValue (input: HTMLInputElement) {
|
function getInputValue (input: HTMLInputElement) {
|
||||||
switch (input.type) {
|
switch (input.type) {
|
||||||
case "checkbox":
|
case "checkbox":
|
||||||
@@ -236,7 +235,7 @@ class OptionsApp extends Component<{}, OptionsAppState> {
|
|||||||
</div>
|
</div>
|
||||||
<EditableList data={ this.state.options.userAgentWhitelist }
|
<EditableList data={ this.state.options.userAgentWhitelist }
|
||||||
onChange={ this.handleWhitelistChange }
|
onChange={ this.handleWhitelistChange }
|
||||||
itemPattern={ MATCH_PATTERN_REGEX }
|
itemPattern={ REMOTE_MATCH_PATTERN_REGEX }
|
||||||
itemPatternError={ this.getWhitelistItemPatternError } />
|
itemPatternError={ this.getWhitelistItemPatternError } />
|
||||||
</div>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|||||||
@@ -4,3 +4,40 @@ const _window = (window.wrappedJSObject as any);
|
|||||||
|
|
||||||
_window.chrome = cloneInto({}, window);
|
_window.chrome = cloneInto({}, window);
|
||||||
_window.navigator.presentation = cloneInto({}, window);
|
_window.navigator.presentation = cloneInto({}, window);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const EXT_SENDER_SCRIPT_URLS = [
|
||||||
|
"chrome-extension://pkedcjkdefgpdelpbcmbmeomcjbeemfm/cast_sender.js"
|
||||||
|
, "chrome-extension://enhhojjnijigcajfphajepfemndkmdlo/cast_sender.js"
|
||||||
|
];
|
||||||
|
|
||||||
|
const SENDER_SCRIPT_URL = "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js";
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace the src property setter on <script> elements to
|
||||||
|
* intercept the new value.
|
||||||
|
*
|
||||||
|
* If it matches one of Chrome's cast extension sender script
|
||||||
|
* URLs, replace it with the standard API URL, the request for
|
||||||
|
* which is handled in the main script.
|
||||||
|
*/
|
||||||
|
const { get, set } = Reflect.getOwnPropertyDescriptor(
|
||||||
|
HTMLScriptElement.prototype.wrappedJSObject, "src");
|
||||||
|
|
||||||
|
Reflect.defineProperty(
|
||||||
|
HTMLScriptElement.prototype.wrappedJSObject, "src", {
|
||||||
|
|
||||||
|
configurable: true
|
||||||
|
, enumerable: true
|
||||||
|
, get
|
||||||
|
|
||||||
|
, set: exportFunction(function (value) {
|
||||||
|
if (EXT_SENDER_SCRIPT_URLS.includes(value)) {
|
||||||
|
return set.call(this, SENDER_SCRIPT_URL);
|
||||||
|
}
|
||||||
|
|
||||||
|
return set.call(this, value);
|
||||||
|
}, window)
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,23 +1,22 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const webpack = require("webpack");
|
const webpack = require("webpack");
|
||||||
const CopyWebpackPlugin = require("copy-webpack-plugin");
|
const CopyWebpackPlugin = require("copy-webpack-plugin");
|
||||||
|
|
||||||
module.exports = (env) => ({
|
module.exports = (env) => ({
|
||||||
entry: {
|
entry: {
|
||||||
"main" : `${env.includePath}/main.ts`
|
"main": `${env.includePath}/main.ts`
|
||||||
, "popup/bundle" : `${env.includePath}/popup/index.tsx`
|
, "popup/bundle": `${env.includePath}/popup/index.tsx`
|
||||||
, "options/bundle" : `${env.includePath}/options/index.tsx`
|
, "options/bundle": `${env.includePath}/options/index.tsx`
|
||||||
, "updater/bundle" : `${env.includePath}/updater/index.tsx`
|
, "updater/bundle": `${env.includePath}/updater/index.tsx`
|
||||||
, "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`
|
|
||||||
|
|
||||||
// Shim entries
|
// Shim entries
|
||||||
, "shim/bundle" : `${env.includePath}/shim/index.ts`
|
, "shim/bundle": `${env.includePath}/shim/index.ts`
|
||||||
, "shim/content" : `${env.includePath}/shim/content.ts`
|
, "shim/content": `${env.includePath}/shim/content.ts`
|
||||||
, "shim/contentSetup" : `${env.includePath}/shim/contentSetup.ts`
|
, "shim/contentSetup": `${env.includePath}/shim/contentSetup.ts`
|
||||||
}
|
}
|
||||||
, output: {
|
, output: {
|
||||||
filename: "[name].js"
|
filename: "[name].js"
|
||||||
@@ -25,12 +24,12 @@ module.exports = (env) => ({
|
|||||||
}
|
}
|
||||||
, plugins: [
|
, plugins: [
|
||||||
new webpack.DefinePlugin({
|
new webpack.DefinePlugin({
|
||||||
"EXTENSION_NAME" : JSON.stringify(env.extensionName)
|
"EXTENSION_NAME": JSON.stringify(env.extensionName)
|
||||||
, "EXTENSION_ID" : JSON.stringify(env.extensionId)
|
, "EXTENSION_ID": JSON.stringify(env.extensionId)
|
||||||
, "EXTENSION_VERSION" : JSON.stringify(env.extensionVersion)
|
, "EXTENSION_VERSION": JSON.stringify(env.extensionVersion)
|
||||||
, "MIRRORING_APP_ID" : JSON.stringify(env.mirroringAppId)
|
, "MIRRORING_APP_ID": JSON.stringify(env.mirroringAppId)
|
||||||
, "APPLICATION_NAME" : JSON.stringify(env.applicationName)
|
, "APPLICATION_NAME": JSON.stringify(env.applicationName)
|
||||||
, "APPLICATION_VERSION" : JSON.stringify(env.applicationVersion)
|
, "APPLICATION_VERSION": JSON.stringify(env.applicationVersion)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Copy static assets
|
// Copy static assets
|
||||||
|
|||||||
Reference in New Issue
Block a user