[英]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.
似乎整体花费的时间较少,
但第一次打电话相当于其他电话。
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.