如何从 JavaScript 获取我的扩展程序的 id?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6373117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:27:47  来源:igfitidea点击:

How to get my extension's id from JavaScript?

javascriptgoogle-chrome-extension

提问by wong2

I'm writing a Chrome extension, I need to get my extension's id in my code, so I don't need to change it manually every time. How can I do this?

我正在编写一个 Chrome 扩展程序,我需要在我的代码中获取我的扩展程序的 id,所以我不需要每次都手动更改它。我怎样才能做到这一点?

回答by serg

You can get it like this (no extra permissions required) in two different ways:

您可以通过两种不同的方式获得它(不需要额外的权限):

  1. Using runtime api: var myid = chrome.runtime.id;

  2. Using i18n api: var myid = chrome.i18n.getMessage("@@extension_id");

  1. 使用运行时 api: var myid = chrome.runtime.id;

  2. 使用 i18n api: var myid = chrome.i18n.getMessage("@@extension_id");

but you don't need it for opening pages, as chrome.tabs.create()(and some others) understand relative paths.

但是您不需要它来打开页面,因为chrome.tabs.create()(和其他一些人)了解相对路径。

So to open index.htmlfrom your extension folder you should just use:

所以index.html要从你的扩展文件夹打开,你应该使用:

chrome.tabs.create({url: "index.html"});

回答by WesleyJohnson

If you're doing stuff with localization, it looks like the extension mechanics offer some placeholdersfor accessing your extension ID:

如果你正在做本地化的事情,看起来扩展机制提供了一些用于访问你的扩展 ID 的占位符

If you're just trying to access URLs for local files to your extension, you can just use chrome.extension.getURL("some file name");

如果您只是尝试访问本地文件的 URL 到您的扩展程序,您可以使用 chrome.extension.getURL("some file name");

If you have another reason for actually needing to know the id of the extension, I'm not sure there is a straight forward way of getting it from within the extension itself. The two ways that come to me off the top of my head are using chrome.extension.getURL("some file name")and then parsing out the extension id from that returned URL - or using chrome.management.getAll()and looping through all the installed extensions until you find yours using a match on nameand then accessing the id:

如果您有另一个原因实际上需要知道扩展程序的 id,我不确定是否有直接的方法可以从扩展程序本身中获取它。我想到的两种方法是使用chrome.extension.getURL("some file name")然后从返回的 URL 中解析出扩展 id - 或者使用chrome.management.getAll()并循环遍历所有已安装的扩展,直到您使用匹配找到你的扩展name,然后访问id

回答by Philipp Cla?en

In WebExtensions, you have two options:

在 WebExtensions 中,您有两个选择:

  1. chrome.runtime.id
  2. chrome.i18n.getMessage("@@extension_id")
  1. chrome.runtime.id
  2. chrome.i18n.getMessage("@@extension_id")

In Chrome and Opera, they will return the same value, but in Firefox there is a difference.

在 Chrome 和 Opera 中,它们将返回相同的值,但在 Firefox 中则有所不同。

In Firefox, chrome.runtime.idwill return the so called "Extension ID", but chrome.i18n.getMessage("@@extension_id")will return the "Internal UUID". The extension ID is the same for all users, but the internal UUID is created when the extension is installed and is unique per user.

在 Firefox 中,chrome.runtime.id将返回所谓的“扩展 ID”,但chrome.i18n.getMessage("@@extension_id")会返回“内部 UUID”。扩展 ID 对于所有用户都是相同的,但内部 UUID 在安装扩展时创建,并且每个用户都是唯一的。

Depending on the context, the extension ID will not be what you want. For example, Firefox uses the internal UUID to fill the originheader, not the extension ID.

根据上下文,扩展 ID 将不是您想要的。例如,Firefox 使用内部 UUID 来填充origin标头,而不是扩展 ID。

Example 1: Ghostery in Firefox 61

示例 1:Firefox 61 中的 Ghostery

chrome.runtime.id                        --> "[email protected]"
chrome.i18n.getMessage("@@extension_id") --> "e3225586-81a0-47c3-8612-d95fb0c2a609"

For fetchrequests from within the extension, Firefox will add the header

对于fetch来自扩展中的请求,Firefox 将添加标头

origin: moz-extension://e3225586-81a0-47c3-8612-d95fb0c2a609

Example 2: Ghostery in Chrome

示例 2:Chrome 中的 Ghostery

chrome.runtime.id                        --> "mlomiejdfkolichcflejclcbmpeaniij"
chrome.i18n.getMessage("@@extension_id") --> "mlomiejdfkolichcflejclcbmpeaniij" 

For fetchrequests from within the extension, Chrome will add the header

对于fetch来自扩展程序中的请求,Chrome 将添加标头

origin: chrome-extension://mlomiejdfkolichcflejclcbmpeaniij