解压后的扩展程序的 Chrome 扩展程序 ID 是如何生成的? [英] How is the Chrome Extension ID of an unpacked extension generated?

查看:26
本文介绍了解压后的扩展程序的 Chrome 扩展程序 ID 是如何生成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与我的同事分享一个解压后的扩展.它在注入的脚本中使用方法 chrome.runtime.sendMessage(string extensionId, any message, object options, function responseCallback).为此,我需要提前知道扩展 ID.

I would like to share an unpacked extension with my colleagues. It uses the method chrome.runtime.sendMessage(string extensionId, any message, object options, function responseCallback) in an injected script. For that I need to know the extension ID in advance.

解压后的扩展程序 ID 在不同系统上是否会有所不同,或者我可以对我在扩展程序菜单中找到的扩展程序进行硬编码吗?

Will the extension ID of the unpacked extension will be different on different systems or can I hard code the one I have found in my extension menu?

推荐答案

虽然我链接到 this question 解释了如何为解压缩的扩展固定"ID,这将解决 OP 面临的实际问题,问题本身(如标题所述)很有趣.

While I linked to this question that explains how to "pin" an ID for an unpacked extension, which would solve the practical problem OP faces, the question itself (as stated in the title) is interesting.

如果我们查看 Chromium 源,我们将看到 ID 只是扩展的绝对路径的 SHA 哈希(可能是标准化的,无论如何).代码亮点:

If we look at the Chromium source, we will see that the ID is simply a SHA hash of a (maybe-normalized, whatever that means) absolute path to the extension. Highlights from the code:

// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// chromium/src/chrome/browser/extensions/unpacked_installer.cc
int UnpackedInstaller::GetFlags() {
  std::string id = crx_file::id_util::GenerateIdForPath(extension_path_);
  /* ... */
}

// chromium/src/components/crx_file/id_util.cc
std::string GenerateIdForPath(const base::FilePath& path) {
  base::FilePath new_path = MaybeNormalizePath(path);
  std::string path_bytes =
      std::string(reinterpret_cast<const char*>(new_path.value().data()),
                  new_path.value().size() * sizeof(base::FilePath::CharType));
  return GenerateId(path_bytes);
}

std::string GenerateId(const std::string& input) {
  uint8 hash[kIdSize];
  crypto::SHA256HashString(input, hash, sizeof(hash));
  std::string output =
      base::StringToLowerASCII(base::HexEncode(hash, sizeof(hash)));
  ConvertHexadecimalToIDAlphabet(&output);

  return output;
}

因此,它应该只依赖于扩展文件夹的绝对文件系统路径.

As such, it should ONLY depend on the absolute filesystem path to the extension folder.

这篇关于解压后的扩展程序的 Chrome 扩展程序 ID 是如何生成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆