【问题标题】:How is the Chrome Extension ID of an unpacked extension generated?解压后的扩展程序的 Chrome 扩展程序 ID 是如何生成的?
【发布时间】:2014-11-21 02:28:41
【问题描述】:

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

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

【问题讨论】:

  • 也就是说,你还需要吗?你的意思是,通过注入脚本,注入页面内容?
  • 我通过执行以下 JavaScript 来注入脚本:var s = document.createElement("script"); s.src = chrome.extension.getURL("api.js");,因为我需要脚本来访问它被注入到的任何页面的 window 对象。

标签: google-chrome-extension


【解决方案1】:

虽然我链接到 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;
}

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

【讨论】:

    【解决方案2】:

    Xan 的回答是正确的。但是,只是为了根据上面的 Chromium 代码对其进行扩展以供参考,可以使用此 python 代码计算扩展 id。

    import hashlib
    
    m = hashlib.sha256()
    m.update(bytes(PATH.encode('utf-8')))
    EXTID = ''.join([chr(int(i, base=16) + ord('a')) for i in m.hexdigest()][:32])
    

    【讨论】:

    • 在Windows上,路径需要编码为utf-16-le
    • 谢谢,但是如何在 c# 中做到这一点?
    【解决方案3】:

    MaybeNormalizePath 正在影响 Windows 用例:

    base::FilePath MaybeNormalizePath(const base::FilePath& path) {
    #if defined(OS_WIN)
      // Normalize any drive letter to upper-case. We do this for consistency with
      // net_utils::FilePathToFileURL(), which does the same thing, to make string
      // comparisons simpler.
      base::FilePath::StringType path_str = path.value();
      if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' &&
          path_str[1] == L':')
        path_str[0] = towupper(path_str[0]);
      return base::FilePath(path_str);
    #else
      return path;
    #endif
    }
    
    
    
    https://chromium.googlesource.com/chromium/chromium/+/refs/heads/trunk/extensions/common/id_util.cc
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2012-02-15
      相关资源
      最近更新 更多