Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #7272: Use symbol in scripts (#7273)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuba authored Apr 19, 2023
1 parent cf8cbe7 commit 175a649
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ window.__firefox__.includeOnce("RewardsReporting", function($) {
install();
}

function install() {
let sendMessage = $(function(method, url, data, referrerUrl) {
const install = () => {
const sendMessage = $(function(method, url, data, referrerUrl) {
$.postNativeMessage('$<message_handler>', {"securityToken": SECURITY_TOKEN, "data": {
method: method === undefined ? "GET" : method,
url: url,
Expand All @@ -26,29 +26,33 @@ window.__firefox__.includeOnce("RewardsReporting", function($) {
}});
})

let originalOpen = XMLHttpRequest.prototype.open;
let originalSend = XMLHttpRequest.prototype.send;
let originalFetch = window.fetch;
let originalSendBeacon = navigator.sendBeacon;
let originalImageSrc = Object.getOwnPropertyDescriptor(Image.prototype, "src");
const originalOpen = XMLHttpRequest.prototype.open;
const originalSend = XMLHttpRequest.prototype.send;
const originalFetch = window.fetch;
const originalSendBeacon = navigator.sendBeacon;
const originalImageSrc = Object.getOwnPropertyDescriptor(Image.prototype, "src");
const localURLProp = Symbol('url')
const localMethodProp = Symbol('method')
const localRefProp = Symbol('ref')
const localDataProp = Symbol('data')

XMLHttpRequest.prototype.open = $(function(method, url) {
const listener = function() {
sendMessage(this._method, this.responseURL === null ? this._url : this.responseURL, this._data, this._ref);
sendMessage(this[localMethodProp], this.responseURL === null ? this[localURLProp] : this.responseURL, this[localDataProp], this[localRefProp]);
};
this._method = method;
this._url = url;
this[localMethodProp] = method;
this[localURLProp] = url;
this.addEventListener('load', listener, true);
this.addEventListener('error', listener, true);
return originalOpen.apply(this, arguments);
}, /*overrideToString=*/false);

XMLHttpRequest.prototype.send = $(function(body) {
this._ref = null;
this._data = body;
this[localRefProp] = null;
this[localDataProp] = body;
if (body instanceof Document) {
this._ref = body.referrer;
this._data = null;
this[localRefProp] = body.referrer;
this[localDataProp] = null;
}
return originalSend.apply(this, arguments);
}, /*overrideToString=*/false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ window.__firefox__.execute(function($) {
// -------------------------------------------------
// Send ajax requests URLs to the host application
// -------------------------------------------------
const localURLProp = Symbol('url')
var xhrProto = XMLHttpRequest.prototype;
if (!originalOpen) {
originalOpen = xhrProto.open;
Expand All @@ -75,13 +76,16 @@ window.__firefox__.execute(function($) {
xhrProto.open = $(function(method, url) {
// Blocked async XMLHttpRequest are handled via RequestBlocking.js
// We only handle sync requests
this._shouldTrack = arguments[2] !== undefined && !arguments[2]
this._url = url;
if (arguments[2] === undefined || arguments[2]) {
return originalOpen.apply(this, arguments);
}

this[localURLProp] = url;
return originalOpen.apply(this, arguments);
}, /*overrideToString=*/false);

xhrProto.send = $(function(body) {
if (this._url === undefined || !this._shouldTrack) {
if (this[localURLProp] === undefined) {
return originalSend.apply(this, arguments);
}

Expand All @@ -91,7 +95,7 @@ window.__firefox__.execute(function($) {
// If this `XMLHttpRequest` instance fails to load, we
// can assume it has been blocked.
this._tpErrorHandler = $(function() {
sendMessage(this._url, "xmlhttprequest");
sendMessage(this[localURLProp], "xmlhttprequest");
});
this.addEventListener("error", this._tpErrorHandler);
}
Expand Down

0 comments on commit 175a649

Please sign in to comment.