Skip to content

Commit

Permalink
Harden _Q.push wrapper in the amzn_apstag.js shim
Browse files Browse the repository at this point in the history
Apstag calls are queued onto the `window.apstag._Q` array and are then
called when the Apstag script is ready. Each queued call is usually an
array containing the API method name prefix and arguments, e.g.

    ["f", {slots: [...], timeout: 3000}, () => { ... }]

Some websites however seem to queue up the calls differently, with a
queued call looking something like this instead:

    [["f", Arguments([{slots: [...], timeout: 3000}, () => { ... }])]]

Since the original script handles that, the shim needs to as
well. Let's try our best here.
  • Loading branch information
kzar committed Mar 19, 2024
1 parent 57b338a commit 576625b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions surrogates/amzn_apstag.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,22 @@ if (!(window.apstag && window.apstag._getSlotIdToNameMapping)) {

window.apstagLOADED = true;

const isIterable =
a => Array.isArray(a) ||
a[Symbol.iterator] ||
a.toString() === "[object Arguments]";

_Q.push = function(prefix, args) {
// TODO: Check if this code path ever _isn't_ hit, seems like a bug in the
// original shim implementation?
if (isIterable(prefix) && typeof args === "undefined") {
[prefix, ...args] = Array.from(prefix);
}

if (args.length === 1 && isIterable(args[0])) {
args = Array.from(args[0]);
}

try {
switch (prefix) {
case "f":
Expand Down

0 comments on commit 576625b

Please sign in to comment.