google-chrome-extension - 解压后的扩展程序的 Chrome 扩展程序 ID 是如何生成的?

标签 google-chrome-extension

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

解压后的扩展 ID 在不同的系统上会有所不同,还是我可以硬编码在我的扩展菜单中找到的那个?

最佳答案

虽然我链接到 this question这解释了如何为未打包的扩展“固定”一个 ID,这将解决 OP 面临的实际问题,这个问题本身(如标题中所述)很有趣。

如果我们查看 Chromium source ,我们将看到 ID 只是扩展的绝对路径(可能是规范化的,无论这意味着什么)的 SHA 哈希。代码亮点:

// 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;
}

因此,它应该只取决于扩展文件夹的绝对文件系统路径。

关于google-chrome-extension - 解压后的扩展程序的 Chrome 扩展程序 ID 是如何生成的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26053434/

相关文章:

css - 在将 Select 标记的文本颜色设置为黑色的 iframe 中编辑 CSS?

javascript - 如何在 Chrome 扩展程序中获取特定于 PC 的 UUID

javascript - Chrome 扩展程序定时重定向

php - CSS/HTML/PHP 显示交替数组列表(Google Chrome 扩展)

google-chrome - 控制 “Oops! Chrome could not find … ”页面?

google-chrome-extension - 首次使用 chrome.storage.sync.get 的选项问题

javascript - chrome 打包应用重新加载 html

google-chrome - Chrome 扩展命令(键盘快捷键)不起作用

javascript - Chrome扩展程序如何检测特定选项卡中的域已更改

javascript - 在 Chrome 应用中设置 XHR header 'Referer'