From cf0b61d21b4c922a47507aae4dfc67f361aa145e Mon Sep 17 00:00:00 2001 From: Alexei Date: Wed, 29 Sep 2021 16:07:30 -0400 Subject: [PATCH] Move widget object construction into bg. page --- src/js/contentscripts/utils.js | 36 ++++++---------------------------- src/js/webrequest.js | 28 +++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/js/contentscripts/utils.js b/src/js/contentscripts/utils.js index c84b2ce790..2b28a10912 100644 --- a/src/js/contentscripts/utils.js +++ b/src/js/contentscripts/utils.js @@ -67,36 +67,12 @@ if (window.top != window) { } document.addEventListener("pbSurrogateMessage", function (e) { - switch (e.detail.type) { - - case "widgetFromSurrogate": { - let data = e.detail.widgetData; - - if (data.name == "Rumble Video Player") { - let script_url = `https://rumble.com/embedJS/${encodeURIComponent(data.pubCode)}.${encodeURIComponent(data.args[1].video)}/?url=${encodeURIComponent(document.location.href)}&args=${encodeURIComponent(JSON.stringify(data.args))}`; - chrome.runtime.sendMessage({ - type: "widgetFromSurrogate", - widget: { - name: data.name, - buttonSelectors: ["div#" + data.args[1].div], - scriptSelectors: [`script[src='${script_url}']`], - replacementButton: { - "unblockDomains": ["rumble.com"], - "type": 4 - }, - directLinkUrl: `https://rumble.com/embed/${encodeURIComponent(data.pubCode)}.${encodeURIComponent(data.args[1].video)}/` - } - }); - - } - - break; - } - - default: { - break; - } - + if (e.detail.type == "widgetFromSurrogate") { + chrome.runtime.sendMessage({ + type: "widgetFromSurrogate", + data: e.detail.widgetData, + frameUrl: window.FRAME_URL + }); } }); diff --git a/src/js/webrequest.js b/src/js/webrequest.js index df32394a4c..6f7b5a1e05 100644 --- a/src/js/webrequest.js +++ b/src/js/webrequest.js @@ -1440,6 +1440,28 @@ function dispatcher(request, sender, sendResponse) { // proxies surrogate script-initiated widget replacement messages // from one content script to another case "widgetFromSurrogate": { + let data = request.data, + widget; + + if (data.name == "Rumble Video Player") { + let script_url = `https://rumble.com/embedJS/${encodeURIComponent(data.pubCode)}.${encodeURIComponent(data.args[1].video)}/?url=${encodeURIComponent(request.frameUrl)}&args=${encodeURIComponent(JSON.stringify(data.args))}`; + + widget = { + name: data.name, + buttonSelectors: ["div#" + data.args[1].div], + scriptSelectors: [`script[src='${script_url}']`], + replacementButton: { + "unblockDomains": ["rumble.com"], + "type": 4 + }, + directLinkUrl: `https://rumble.com/embed/${encodeURIComponent(data.pubCode)}.${encodeURIComponent(data.args[1].video)}/` + }; + } + + if (!widget) { + return; + } + let frameData = badger.getFrameData(sender.tab.id, sender.frameId); if (frameData.widgetReplacementReady) { @@ -1447,14 +1469,14 @@ function dispatcher(request, sender, sendResponse) { chrome.tabs.sendMessage(sender.tab.id, { type: "replaceWidgetFromSurrogate", frameId: sender.frameId, - widget: request.widget + widget }); } else { // save the message for later otherwise if (!frameData.hasOwnProperty("widgetQueue")) { frameData.widgetQueue = []; } - frameData.widgetQueue.push(request.widget); + frameData.widgetQueue.push(widget); } break; @@ -1474,7 +1496,7 @@ function dispatcher(request, sender, sendResponse) { chrome.tabs.sendMessage(sender.tab.id, { type: "replaceWidgetFromSurrogate", frameId: sender.frameId, - widget: widget + widget }); } delete frameData.widgetQueue;