Skip to content
This repository was archived by the owner on Mar 14, 2024. It is now read-only.
This repository was archived by the owner on Mar 14, 2024. It is now read-only.

ScriptInjection Documentation Error? #839

Closed

Description

@hallzy

⚠️ This is for issues with the https://developer.chrome.com site, not Chromium itself.
If you want to file a bug with Chromium (the open-source project behind Google Chrome and other browsers), please use https://crbug.com.

Describe the bug

The documentation for the ScriptInjection doesn't seem to be correct.

If I provide a func I get the error:

Uncaught TypeError: Error in invocation of scripting.executeScript(scripting.ScriptInjection injection, optional function callback): Error at parameter 'injection': Unexpected property: 'func'.
    at <anonymous>:1:26

I figured out that the actual key needed is function, not func, but I have the same problem with args:

VM172:1 Uncaught TypeError: Error in invocation of scripting.executeScript(scripting.ScriptInjection injection, optional function callback): Error at parameter 'injection': Unexpected property: 'args'.
    at <anonymous>:1:26

To Reproduce
Steps to reproduce the behavior:

  • Setup a basic Chrome extension using Manifest 3 and the scripting permission
  • Either in the extension console, or in a source file run the below code:
let x = chrome.scripting.executeScript({ target: { tabId: 557 }, args: [ 5 ], function: (arg) => arg, })

Using a correct tabId, and you should see the error.

Expected behavior

Error should not happen, and if I understand the documentation correctly, the function should return a promise that resolves with a value of 5. Perhaps the documentation is wrong and not referencing the correct key name, and knowing that might make this work.

Screenshots
If applicable, add screenshots to help explain your problem.

region-20210601-110848

Desktop (please complete the following information):

  • OS: Ubuntu 20.04
  • Browser: Chrome and Brave
  • Version:
    • Chrome: 90.0.4430.93 and 91.0.4472.77
    • Brave: 1.23.75 Chromium: 90.0.4430.93

Activity

  1. added
    bugSomething on the site is broken!
    on Jun 1, 2021
  2. robdodson commented on Jun 2, 2021

    @robdodson
    Contributor

    @dotproto do the docs seem incorrect here?

  3. hallzy commented on Jun 2, 2021

    @hallzy
    Author

    I found these changes in the chromium repo

    Replacement of function with func: chromium/chromium@1f6eb20

    Addition of args: chromium/chromium@71ee034

    Looks like the func change was made for version 92.0.4484.0 and the args change is part of 92.0.4514.0

    It looks like the latest stable version is 91.0.4472.77. I just installed the unstable version of chrome to test it out which is at 92.0.4515.40 (ahead of the func and args changes and it seems to be working).

    So I guess the documentation is ahead of the stable chrome release right now.

  4. dotproto commented on Jun 3, 2021

    @dotproto
    Contributor

    @hallzy, you've hit the nail on the head. Our docs reflect the tip of the main branch in Chromium.

    For a bit more context, the first implementation of function support in this API used the property name function for the function you pass in. This had a major drawback, though, in that this effectively prohibited developers from using object shorthand; since "function" is a reserved keyword in JS, devs can't create variable named "function", but they can assign properties with that name.

    In order to accommodate developers using object shorthand, we decided to renamed function to func and arguments to args. Unfortunately this broke a few users and caused a short term disconnect between Chrome Stable and Beta/Canary, but in the long term this should make it easier to write extensions with idiomatic JS patterns.

    let func = () => { console.log('done') };
    let tabId = tab.id;
    chrome.scripting.executeScript({ target: { tabId }, func });
  5. hallzy commented on Jun 3, 2021

    @hallzy
    Author

    Thanks. Yeah, I noticed there was an explanation in the Chromium code about that so that all makes sense. Was just very confused that the documented keys didn't work.

    I'll close this. Might be useful to have chrome version numbers on stuff like that in the documentation to avoid confusion, but maybe changes like this aren't all that common anyways.

    I'll just test my extension using unstable for now and add a minimum chrome version to the manifest and leave it at that

    Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething on the site is broken!

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions