Bug 1930440 - add a JS invention for online.singaporepools.com to prevent a browser-unsupported alert; r=denschub,webcompat-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D228929
This commit is contained in:
Thomas Wisniewski 2024-11-20 14:07:43 +00:00
parent 59eec6fb3e
commit d8a41aae2c
7 changed files with 137 additions and 6 deletions

View File

@ -1214,6 +1214,20 @@ const AVAILABLE_INJECTIONS = [
],
},
},
{
id: "1930440",
platform: "all",
domain: "online.singaporepools.com",
bug: "1930440",
contentScripts: {
matches: ["*://online.singaporepools.com/*"],
js: [
{
file: "injections/js/bug1930440-online.singaporepools.com-prevent-unsupported-alert.js",
},
],
},
},
];
module.exports = AVAILABLE_INJECTIONS;

View File

@ -0,0 +1,34 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* globals exportFunction */
"use strict";
/**
* online.singaporepools.com - Shows an 'unsupported browser' alert
* Bug #1930440 - https://bugzilla.mozilla.org/show_bug.cgi?id=1930440
* WebCompat issue #143685 - https://webcompat.com/issues/143685
*
* We can intercept the call to alert and hide it.
*/
console.info(
"window.alert is being overriden for compatibility reasons. See https://bugzilla.mozilla.org/show_bug.cgi?id=1923286 for details."
);
const originalAlert = Object.getOwnPropertyDescriptor(
window.wrappedJSObject,
"alert"
).value;
Object.defineProperty(window.wrappedJSObject, "alert", {
get: exportFunction(function (msg) {
if (!msg.includes("unsupported browser")) {
originalAlert.apply(this, arguments);
}
}, window),
set: exportFunction(function () {}, window),
});

View File

@ -119,6 +119,7 @@ FINAL_TARGET_FILES.features["webcompat@mozilla.org"]["injections"]["js"] += [
"injections/js/bug1923286-bing.com-image-click-fix.js",
"injections/js/bug1924500-www.tiktok.com-fix-captcha-slider.js",
"injections/js/bug1928216-voice.google.com-permissions.query.js",
"injections/js/bug1930440-online.singaporepools.com-prevent-unsupported-alert.js",
]
FINAL_TARGET_FILES.features["webcompat@mozilla.org"]["shims"] += [

View File

@ -1214,6 +1214,20 @@ const AVAILABLE_INJECTIONS = [
],
},
},
{
id: "1930440",
platform: "all",
domain: "online.singaporepools.com",
bug: "1930440",
contentScripts: {
matches: ["*://online.singaporepools.com/*"],
js: [
{
file: "injections/js/bug1930440-online.singaporepools.com-prevent-unsupported-alert.js",
},
],
},
},
];
module.exports = AVAILABLE_INJECTIONS;

View File

@ -0,0 +1,34 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* globals exportFunction */
"use strict";
/**
* online.singaporepools.com - Shows an 'unsupported browser' alert
* Bug #1930440 - https://bugzilla.mozilla.org/show_bug.cgi?id=1930440
* WebCompat issue #143685 - https://webcompat.com/issues/143685
*
* We can intercept the call to alert and hide it.
*/
console.info(
"window.alert is being overriden for compatibility reasons. See https://bugzilla.mozilla.org/show_bug.cgi?id=1923286 for details."
);
const originalAlert = Object.getOwnPropertyDescriptor(
window.wrappedJSObject,
"alert"
).value;
Object.defineProperty(window.wrappedJSObject, "alert", {
get: exportFunction(function (msg) {
if (!msg.includes("unsupported browser")) {
originalAlert.apply(this, arguments);
}
}, window),
set: exportFunction(function () {}, window),
});

View File

@ -729,7 +729,7 @@ class Client:
async def disable_window_alert(self):
return await self.make_preload_script("window.alert = () => {}")
async def await_alert(self, text):
async def await_alert(self, text, timeout=None):
if not hasattr(self, "alert_preload_script"):
self.alert_preload_script = await self.make_preload_script(
"""
@ -741,16 +741,27 @@ class Client:
"alert_detector",
)
return self.alert_preload_script.run(
"""(msg) => new Promise(done => {
"""(msg, timeout) => new Promise(done => {
const interval = 200;
let count = 0;
const to = setInterval(() => {
if (window.__alerts.includes(msg)) {
clearInterval(to);
done();
for (const a of window.__alerts) {
if (a.includes(msg)) {
clearInterval(to);
done(a);
return;
}
}
}, 200);
count += interval;
if (timeout && timeout * 1000 < count) {
clearInterval(to);
done(false);
}
}, interval);
})
""",
text,
timeout,
await_promise=True,
)

View File

@ -0,0 +1,23 @@
import pytest
URL = "https://online.singaporepools.com/en/lottery"
UNSUPPORTED_ALERT = "unsupported browser"
async def get_alert(client):
alert = await client.await_alert(UNSUPPORTED_ALERT, timeout=10)
await client.navigate(URL)
return await alert
@pytest.mark.asyncio
@pytest.mark.with_interventions
async def test_enabled(client):
assert not await get_alert(client)
@pytest.mark.asyncio
@pytest.mark.without_interventions
async def test_disabled(client):
assert await get_alert(client)