Keep same ID when converting from userscript in Chrome

前端 未结 1 1130

I\'m currently updating a userscript to a chrome extension, but since the new ID is different it won\'t update the old extension, but add a new one.

Is there a way t

1条回答
  •  隐瞒了意图╮
    2021-01-15 02:49

    Userscripts are converted to Chrome extensions when you load it. Follow these steps to create and maintain an extension with the same ID:

    1. Install the userscript.
    2. Visit chrome://extensions/ and activate developer mode.
    3. Scroll down to your user script, and find the ID: Userscript 1.0 Description of user script ID: hjcgfecgldpgebeeflnjldogphkilfdo
      Notice that the version of your userscript is 1.0 (by default, overriden via @version).
    4. Next, visit the extensions directory of your Chrome/Chromium user profile. The default paths can be found here. If you launch Chrome/chromium using the --user-data-dir=... flag, then visit ....
      Once you're inside the directory, look for a directory with the ID found at 3.
    5. Copy the whole directory to a different location, eg /tmp/ or %tmp%.
    6. Use the "Load unpacked extension" button, and select the first subdirectory of the previously copied directory: The name of this subdirectory is based on the version.
    7. Scroll down to see that a converted userscript is a true extension (it loads!):
      Userscript 1.0 Description of user script ID: hjcgfecgldpgebeeflnjldogphkilfdo Loaded from: /tmp/hjcgfecgldpgebeeflnjldogphkilfdo/1.0_0

    Okay, steps 6-7 are optional, they were only included to show that a userscript is converted to a true extension, with a special flag set. You can also notice the red warning about the manifest version. To fix that, edit manifest.json, and add "manifest_version": 2,. So, in our case:

        ...
        "converted_from_user_script": true,
        "description": "Description of user script",
        "key": "eYxnPzfSPtfL3ji4nQX3ujTXpzz3YQ6dVlvHWf1gvW8=",
        "name": "Userscript",
        "version": "1.0",
        "manifest_version": 2
    }

    Now, you've got a Chrome extension which behaves like a Chrome extension with some extra flavour. Follow the steps from the official documentation to update your package:

    Updating a package

    To create an updated version of your extension:

    1. Increase the version number in manifest.json.
    2. Bring up the Extensions management page by going to this URL: chrome://extensions
    3. Click the Pack extension button. A dialog appears.
    4. In the Extension root directory field, specify the path to the extension's folder—for example, c:\myext.
    5. In the Private key file field, specify the location of the already generated .pem file for this extension—for example, c:\myext.pem.
    6. Click OK.

    Uploading a previously packaged extension to the Chrome Web Store

    You can use the Chrome Developer Dashboard to upload an extension that you've previously packaged yourself. However, unless you take special steps, the extension's ID in the Chrome Web Store will be different from its ID in the package you created. This different ID might be a problem if you've distributed your extension package, because it allows users to install multiple versions of your extension, each with its own local data.

    If you want to keep the extension ID the same, follow these steps:

    1. Rename the private key that was generated when you created the .crx file to key.pem.
    2. Put key.pem in the top directory of your extension.
    3. Compress that directory into a ZIP file.
    4. Upload the ZIP file using the Chrome Developer Dashboard.

    Appendum: Keeping the same ID in unpacked / packed extensions.

    The extensionID of an extension can be controlled via the "key" parameter in the manifest file. The easiest way to have the same ID for the unpacked and packed extension is also stated in the documentation:

    key This value can be used to control the unique ID of an extension, app, or theme when it is loaded during development.

    Note: You don't usually need to use this value. Instead, write your code so that the key value doesn't matter by using relative paths and chrome.extension.getURL().

    To get a suitable key value, first install your extension from a .crx file (you may need to upload your extension or package it manually). Then, in your user data directory, look in the file Default/Extensions///manifest.json. You will see the key value filled in there.

    When the key field does not exists, it's randomly generated. Then, the extensionID is generated from this key. The algorithm for generating the extensionID is explained here. By the nature of the algorithm, you cannot perform it in the reverse order (extensionID -> key).

    0 讨论(0)
提交回复
热议问题