如何生成解压扩展程序的Chrome扩展程序ID?

mri*_*z_p 5 google-chrome-extension

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

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

Xan*_*Xan 14

虽然我链接到这个问题,解释了如何"解锁"解包扩展的ID,这将解决OP面临的实际问题,但问题本身(如标题中所述)很有趣.

如果我们查看Chromium源代码,我们将看到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;
}
Run Code Online (Sandbox Code Playgroud)

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