Skip to content

Commit cafe6a9

Browse files
authored
Don't show Usabilla button if it's blocked (#8208)
Address #7784
1 parent d96bbc6 commit cafe6a9

1 file changed

Lines changed: 44 additions & 39 deletions

File tree

src/NuGetGallery/Scripts/gallery/common.js

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -463,51 +463,56 @@
463463
};
464464

465465
nuget.enableUsabilla = function (obfuscatedPath) {
466-
// If there is an obfuscated path, hook into the outgoing AJAX request containing the feedback and obfuscate
467-
// the URL data. This approach was provided by the Usabilla technical support.
468-
if (obfuscatedPath) {
469-
var obfuscatedUrl = document.createElement('a');
470-
obfuscatedUrl.href = window.location.href;
471-
if (obfuscatedPath.substring(0, 1) != "/") {
472-
obfuscatedUrl.pathname = "/" + obfuscatedPath;
473-
} else {
474-
obfuscatedUrl.pathname = obfuscatedPath;
475-
}
476-
477-
window.usabilla_live("setEventCallback", function (category, action, label, value, eventdata) {
478-
if (action != "Feedback:Open") {
479-
return;
466+
// Only enable Usabilla if the API is available and the button container is in the DOM. If the user has
467+
// external scripts blocked or trackers blocks, Usabilla might not be loaded at all. This method was confirmed
468+
// with Usabilla support staff.
469+
if (window.usabilla_live !== undefined && $(".usabilla_live_button_container").length > 0) {
470+
// If there is an obfuscated path, hook into the outgoing AJAX request containing the feedback and obfuscate
471+
// the URL data. This approach was provided by the Usabilla technical support.
472+
if (obfuscatedPath) {
473+
var obfuscatedUrl = document.createElement('a');
474+
obfuscatedUrl.href = window.location.href;
475+
if (obfuscatedPath.substring(0, 1) != "/") {
476+
obfuscatedUrl.pathname = "/" + obfuscatedPath;
477+
} else {
478+
obfuscatedUrl.pathname = obfuscatedPath;
480479
}
481480

482-
function sendWithObfuscation(vData) {
483-
if (vData) {
484-
var data = JSON.parse(vData);
485-
data.url = obfuscatedUrl.href;
486-
vData = JSON.stringify(data);
487-
arguments[0] = vData;
481+
window.usabilla_live("setEventCallback", function (category, action, label, value, eventdata) {
482+
if (action != "Feedback:Open") {
483+
return;
488484
}
489-
realSend.apply(this, arguments);
490-
}
491485

492-
var realSend = XMLHttpRequest.prototype.send;
493-
var ub_window = document.getElementById("lightningjs-frame-usabilla_live_feedback").contentWindow;
494-
ub_window.XMLHttpRequest.prototype.send = sendWithObfuscation;
495-
if (window.XDomainRequest) {
496-
realSend = XDomainRequest.prototype.send;
497-
ub_window.XDomainRequest.prototype.send = sendWithObfuscation;
498-
}
499-
});
500-
}
486+
function sendWithObfuscation(vData) {
487+
if (vData) {
488+
var data = JSON.parse(vData);
489+
data.url = obfuscatedUrl.href;
490+
vData = JSON.stringify(data);
491+
arguments[0] = vData;
492+
}
493+
realSend.apply(this, arguments);
494+
}
501495

502-
// Hide the default feedback button.
503-
window.usabilla_live("hide");
496+
var realSend = XMLHttpRequest.prototype.send;
497+
var ub_window = document.getElementById("lightningjs-frame-usabilla_live_feedback").contentWindow;
498+
ub_window.XMLHttpRequest.prototype.send = sendWithObfuscation;
499+
if (window.XDomainRequest) {
500+
realSend = XDomainRequest.prototype.send;
501+
ub_window.XDomainRequest.prototype.send = sendWithObfuscation;
502+
}
503+
});
504+
}
504505

505-
// Wire-up and show the custom feedback button.
506-
document.getElementById("usbl-integrated-button").addEventListener("click", function (event) {
507-
event.preventDefault();
508-
window.usabilla_live("click");
509-
});
510-
document.getElementById("usabilla-button").style.display = "block";
506+
// Hide the default feedback button.
507+
window.usabilla_live("hide");
508+
509+
// Wire-up and show the custom feedback button.
510+
document.getElementById("usbl-integrated-button").addEventListener("click", function (event) {
511+
event.preventDefault();
512+
window.usabilla_live("click");
513+
});
514+
document.getElementById("usabilla-button").style.display = "block";
515+
}
511516
};
512517

513518
window.nuget = nuget;

0 commit comments

Comments
 (0)