简体   繁体   English

检索Google Chrome扩展程序ID的最佳方法是什么?

[英]What is the best way to retrieve a Google Chrome Extension ID?

I know two ways of getting Google Chrome Extension ID: 我知道获取Google Chrome扩展程序ID的两种方法:

chrome.app.getDetails().id;

chrome.i18n.getMessage('@@extension_id');

Are there any drawbacks regarding one of those? 其中一个有任何缺点吗?

I tend to use the first one since it's shorter but who knows. 我倾向于使用第一个,因为它更短但谁知道。 I may be wrong. 我可能错了。

---- EDIT ---- ----编辑----

Since I'm the only who cares, here's what I did to benchmark those: 因为我是唯一关心的人,所以这就是我做的基准测试:

console.time('t1');
for (var i=0; i < 10000; i++) { chrome.app.getDetails().id; }; 
console.timeEnd('t1');

console.time('t2');
for (var i=0; i < 10000; i++) { chrome.i18n.getMessage('@@extension_id'); }; 
console.timeEnd('t2');

Here are the results: 结果如下:

t1: 5190.766ms

t2: 860.697ms

It looks like using i18n is so much faster overall but the first one is faster at the beginning and after 3 or 4 executions, i18n is better. 看起来使用i18n总体上要快得多,但第一个在开始时更快,在执行3或4次之后,i18n更好。

Try to go through the similar query: How to reference the version information in a Google Chrome extension? 尝试通过类似的查询: 如何在Google Chrome扩展程序中引用版本信息?

There you can get a broader idea of the first call, you are using. 在那里,你可以更广泛地了解你正在使用的第一个电话。

    chrome.app.getDetails().id;

In the end, I created a small Chrome Extension to benchmark three known ways to retrieve Chrome Extension ID: 最后,我创建了一个小型Chrome扩展程序来测试三种已知的方法来检索Chrome扩展ID:

  • chrome.app.getDetails().id;
  • chrome.i18n.getMessage('@@extension_id');
  • chrome.extension.getURL('/').slice(19, -1);
  • chrome.runtime.id;

As you can see in the result below, chrome.extension.getURL('/').slice(19, -1); 正如您在下面的结果中看到的那样, chrome.extension.getURL('/').slice(19, -1); seems to spend less time overall but the first calls are equivalent to the other ones. 似乎整体花费的时间较少, 第一次打电话相当于其他电话。

谷歌Chrome 27.0.1430.0(官方版本186115)开发的基准图像

As we can see, chrome.runtime.id wins . 我们可以看到, chrome.runtime.id获胜

Note #1: You can download the zip archive of the extension here 注意#1:您可以在此处下载扩展程序的zip存档

Note #2: I used performance.now() to get an accurate timing. 注意#2:我使用performance.now()来获得准确的计时。

Note #3: I added chrome.runtime.id to the benchmark tool. 注意#3:我将chrome.runtime.id添加到基准测试工具中。 Thanks Konstantin Smolyanin! 谢谢Konstantin Smolyanin!

It seems that since chrome 22 the best way to get the extension ID is built-in property chrome.runtime.id . 似乎从chrome 22获取扩展ID的最佳方式是内置属性chrome.runtime.id See documentation. 见文档。

console.log(chrome.runtime.id);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为Chrome扩展程序存储阵列的最佳方法是什么? - What is the best way to store an array for a Chrome extension? 在 Chrome 扩展脚本之间共享功能的最佳方式是什么? - What is the best way to share functions between Chrome extension scripts? 开发购物优惠扩展功能的最佳方法是什么? - What is the best way to develop shopping offers chrome extension? 使用Chrome扩展程序检索Google表格数据 - Retrieve Google Sheets data with Chrome extension 有没有办法获得谷歌浏览器扩展程序的绝对路径 - Is there a way to get the absolute path of a google chrome extension 在注入的脚本和Google Chrome扩展程序代码/内容脚本之间传递消息的最安全方法是什么? - What is the most secure way of passing messages between an injected script and Google Chrome extension code/content script? 有没有办法在 google chrome 扩展程序中增加变量而无需打开扩展程序? - Is there a way to increment a variable in a google chrome extension without having to open the extension? Chrome 扩展:在浏览器中存储文件的最佳方式(非磁盘) - Chrome extension: Best way to store files in the browser (non-disk) Chrome扩展程序,这是将消息从注入的脚本发送到后台的最佳方法 - Chrome extension, best way to send messages from injected script to background 在Chrome扩展程序中传递异步消息的最佳设计模式是什么? - What is the best design pattern for asynchronous message passing in a Chrome extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM