Redirect cast extension chrome-extension:// URLs to regular API URL

This commit is contained in:
hensm
2019-03-13 07:51:38 +00:00
parent 00e923c8ce
commit 870a759251
4 changed files with 80 additions and 8 deletions

View File

@@ -8,3 +8,53 @@ document.addEventListener("beforescriptexecute", function onBeforeScriptExecute
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"
});

View File

@@ -7,18 +7,28 @@ const PLATFORM_LINUX = "Mozilla/5.0 (X11; Linux x86_64";
const UA_PREFIX = "Mozilla/5.0";
const UA_CHROME = "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36";
const UA_CHROME_LEGACY = "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2883.87 Safari/537.36";
const UA_SAFARI = "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15";
export function getChromeUserAgent (platform: string): string {
let platformComponent: string;
function getPlatformComponent (platform: string) {
switch (platform) {
case "mac": platformComponent = PLATFORM_MAC; break;
case "win": platformComponent = PLATFORM_WIN; break;
case "linux": platformComponent = PLATFORM_LINUX; break;
case "mac": return PLATFORM_MAC; break;
case "win": return PLATFORM_WIN; break;
case "linux": return PLATFORM_LINUX; break;
}
}
return `${UA_PREFIX} (${platformComponent}) ${UA_CHROME}`;
export function getChromeUserAgent (
platform: string
, legacy: boolean = false): string {
const platformComponent = getPlatformComponent(platform);
const browserComponent = legacy
? UA_CHROME_LEGACY
: UA_CHROME;
return `${UA_PREFIX} (${platformComponent}) ${browserComponent}`;
}
export function getSafariUserAgent (platform: string): string {

View File

@@ -173,10 +173,19 @@ async function onBeforeSendHeaders (
currentUAString = getChromeUserAgent(os);
}
const host = details.requestHeaders.find(
(header: any) => header.name === "Host");
// Find and rewrite the User-Agent header
for (const header of details.requestHeaders) {
if (header.name.toLowerCase() === "user-agent") {
header.value = currentUAString;
// TODO: Move this somewhere else
if (host.value === "www.youtube.com") {
header.value = getChromeUserAgent(os, true);
} else {
header.value = currentUAString;
}
break;
}
}

View File

@@ -1,3 +1,6 @@
"use strict";
(window.wrappedJSObject as any).chrome = cloneInto({}, window);
const _window = (window.wrappedJSObject as any);
_window.chrome = cloneInto({}, window);
_window.navigator.presentation = cloneInto({}, window);