javascript - How can my extension's Content Script change the value of a script variable before the page reads it? - Sta

admin2025-04-18  4

I am writing an extension and using "world": "MAIN" in the manifest for my content script, and I want to be able to modify a variable in the source code before the page accesses it.

My specific use case: The source page contains an inline script:

<script type="text/javascript">
    window.gameData = {…}
</script>

where window.gameData is

{
"today":
{
"displayWeekday":"Thursday",
"outerLetters":["d","g","i","l","y","z"],
"answers":["dizzyingly","diddling","digging",…],
"id":18831,
…
},

"yesterday":
{
"displayWeekday":"Wednesday",
"outerLetters":["a","c","e","l","n","o"],
"answers":["conclave","covalence","alcove",…],
"id":16175,
…
},
…
}

I want to change the array at: window.gameData.today.answers before the page reads it.

Using Chrome DevTools, I located the spot in the page's script where the value is read, but I don't know how to determine when this occurs, and how to get in earlier than that. Here is where the value is read:

65489: function (e, t, n) {
n.d(t, {
    $g: function () { return s.$g },
    Be: function () { return _.B }
});
var r = n(74395)
    , o = n(22042);
if ((0, p.Z)(), o.G7)
    f.win.dataLayer = [];
else {
    const e = (0, i.pg)();
    e || ((0, i.He)()),
        f.win.addEventListener("load", (() => {
            (0, u.IX)()
        }))
}
const x = () => f.win.gameData
},
转载请注明原文地址:http://anycun.com/QandA/1744929982a89636.html