Skip to content

Commit

Permalink
Fix activation when 2 types of same widget on page
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostwords committed Oct 22, 2021
1 parent d595658 commit f79fc58
Showing 1 changed file with 28 additions and 36 deletions.
64 changes: 28 additions & 36 deletions src/js/contentscripts/socialwidgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function init(response) {
}

// widget replacement initiated by a surrogate script
// TODO add selenium test
} else if (request.type == "replaceWidgetFromSurrogate") {
if (request.frameId === FRAME_ID) {
replaceIndividualButton(request.widget);
Expand Down Expand Up @@ -204,18 +205,16 @@ function _createButtonReplacement(widget, callback) {
function _createWidgetReplacement(widget, trackerElem, callback) {
let replacementEl;

// in-place widget type:
// in-place widget types:
//
// type 3:
// reinitialize the widget by reinserting its element's HTML
if (widget.replacementButton.type == 3) {
replacementEl = createReplacementWidget(
widget, trackerElem, reinitializeWidgetAndUnblockTracker);

// in-place widget type:
//
// type 4:
// reinitialize the widget by reinserting its element's HTML
// and activating associated scripts
} else if (widget.replacementButton.type == 4) {
replacementEl = createReplacementWidget(
widget, trackerElem, replaceWidgetAndReloadScripts);
if ([3, 4].includes(widget.replacementButton.type)) {
replacementEl = createReplacementWidget(widget, trackerElem);
}

callback(replacementEl);
Expand Down Expand Up @@ -273,39 +272,30 @@ function replaceButtonWithHtmlCodeAndUnblockTracker(button, widget_name, html) {
* Unblocks the given widget and replaces our replacement placeholder
* with the original third-party widget element.
*
* Reruns scripts defined in scriptSelectors, if any.
*
* The teardown to the initialization defined in createReplacementWidget().
*/
function reinitializeWidgetAndUnblockTracker(widget) {
function restoreWidget(widget) {
let name = widget.name;
unblockTracker(name, function () {
// restore all widgets of this type
WIDGET_ELS[name].forEach(data => {
data.parent.replaceChild(data.widget, data.replacement);
});
WIDGET_ELS[name] = [];
});
}

/**
* Similar to reinitializeWidgetAndUnblockTracker() above,
* but also reruns scripts defined in scriptSelectors.
*/
function replaceWidgetAndReloadScripts(widget) {
let name = widget.name;

if (widget.scriptSelectors.some(i => i.includes("onload\\=vueRecaptchaApiLoaded"))) {
// we can't do "in-place" activation; reload the page instead
unblockTracker(name, function () {
location.reload();
});
return;
if (widget.scriptSelectors) {
if (widget.scriptSelectors.some(i => i.includes("onload\\=vueRecaptchaApiLoaded"))) {
// we can't do "in-place" activation; reload the page instead
unblockTracker(name, function () {
location.reload();
});
return;
}
}

unblockTracker(name, function () {
// restore all widgets of this type
WIDGET_ELS[name].forEach(data => {
data.parent.replaceChild(data.widget, data.replacement);
reloadScripts(data.scriptSelectors);
if (data.scriptSelectors) {
reloadScripts(data.scriptSelectors);
}
});
WIDGET_ELS[name] = [];
});
Expand Down Expand Up @@ -399,7 +389,7 @@ function _make_id(prefix) {
return prefix + "-" + Math.random().toString().replace(".", "");
}

function createReplacementWidget(widget, elToReplace, activationFn) {
function createReplacementWidget(widget, elToReplace) {
let name = widget.name;

let widgetFrame = document.createElement('iframe');
Expand Down Expand Up @@ -572,11 +562,13 @@ function createReplacementWidget(widget, elToReplace, activationFn) {
onceButton.addEventListener("click", function (e) {
if (!e.isTrusted) { return; }
e.preventDefault();
activationFn(widget);
restoreWidget(widget);
}, { once: true });

siteButton.addEventListener("click", function (e) {
if (!e.isTrusted) { return; }
if (!e.isTrusted) {
return;
}

e.preventDefault();

Expand All @@ -586,7 +578,7 @@ function createReplacementWidget(widget, elToReplace, activationFn) {
type: "allowWidgetOnSite",
widgetName: name
}, function () {
activationFn(widget);
restoreWidget(widget);
});
}, { once: true });

Expand Down

0 comments on commit f79fc58

Please sign in to comment.