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

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

问题描述

我想与同事分享一个未包装的扩展。它在注入的脚本中使用方法 chrome.runtime.sendMessage(字符串extensionId,任何消息,对象选项,函数responseCallback)。为此,我需要提前知道扩展名。



解压扩展名的扩展名会在不同的系统上有所不同,或者我可以硬编码我拥有的扩展名发现在我的扩展菜单?

解决方案

当我链接到这个问题,它解释了如何为一个解压后的扩展插入一个ID,这将解决OP面临的实际问题,这个问题本身在标题中)很有趣。



如果我们看 Chromium source ,我们会看到ID仅仅是扩展的规范化绝对路径的SHA哈希值。来自代码的亮点:



//版权所有2014 The Chromium作者。版权所有。
//使用此源代码由BSD样式的许可证管理,该许可证可以是LICENSE文件中的
//。
$ b $ //铬/ 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(输入,散列,sizeof(散列));
std :: string output =
ase :: StringToLowerASCII(base :: HexEncode(hash,sizeof(hash)));
ConvertHexadecimalToIDAlphabet(& output);

返回输出;





$ b

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


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.

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?

解决方案

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.

If we look at the Chromium source, we will see that the ID is simply a SHA hash of a normalized 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天全站免登陆