Skip to content

Commit

Permalink
Move widget object construction into bg. page
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostwords committed Sep 29, 2021
1 parent 7cdb69f commit cf0b61d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
36 changes: 6 additions & 30 deletions src/js/contentscripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
});

Expand Down
28 changes: 25 additions & 3 deletions src/js/webrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1440,21 +1440,43 @@ 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) {
// message the content script if it's ready for messages
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;
Expand All @@ -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;
Expand Down

0 comments on commit cf0b61d

Please sign in to comment.