如何检测是否从 Chrome 商店安装了用户脚本? [英] How to detect if a userscript is installed from the Chrome Store?

查看:97
本文介绍了如何检测是否从 Chrome 商店安装了用户脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的 Greasemonkey/UserScript 有可用更新时,我想通知用户.但是,当用户从 Chrome 网上应用店安装脚本时,我不想打扰,因为它具有自动更新功能.

I want to notify the user when an update for my Greasemonkey/UserScript is available. However when the user has installed the script from the Chrome Web Store, I don't want to bother because it has auto-update functionality.

我首先考虑使用 $.browser==chrome,但也有可能是 Chrome 用户使用 Tampermonkey 安装了它.(此外,如果站点更新 jQuery,$.browser 将停止工作)

I first thought about using $.browser==chrome but it is also possible that a Chrome user has installed it using Tampermonkey. (Furthermore if the site would update jQuery, $.browser would stop working)

那么,是否可以检测到它是通过 Chrome 网上应用店安装的 UserScript?

So, is it possible to detect that it is a UserScript installed through the Chrome Web Store?

推荐答案

最好让用户知道有更新可用,而不用担心平台. 也不清楚如何这个脚本是跨浏览器的.您可能不得不求助于浏览器嗅探(通常不推荐)才能绝对确定.

Probably best to just let the user know that an update is available, and not worry about the platform. Also it's not clear how cross-browser this script is. You may have to resort to browser sniffing (not usually recommended) to be absolutely sure.

您可以使用GM_infoscriptHandler 的scriptHandler 属性code> object,如果您只关心 Chrome 和/或 Firefox:

You can use the scriptHandler property of the GM_info object, if you are only concerned about Chrome and/or Firefox:

// ==UserScript==
// @name     _Rough script handler detector
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// ==/UserScript==
var scriptEngine;

if (typeof GM_info === "undefined") {
    scriptEngine = "plain Chrome (Or Opera, or scriptish, or Safari, or rarer)";
    // See https://stackoverflow.com/a/2401861/331508 for optional browser sniffing code.
}
else {
    scriptEngine = GM_info.scriptHandler  ||  "Greasemonkey";
}
console.log ('This userscript is running on ' + scriptEngine + '.');

哪个产量:
篡改猴子:

此用户脚本在 Tampermonkey 上运行.

This userscript is running on Tampermonkey.

Greasemonkey(严格来说是 Firefox):

此用户脚本在 Greasemonkey 上运行.

This userscript is running on Greasemonkey.

来自网上商店或其他的 Chrome:

此用户脚本在普通 Chrome(或 Opera、脚本、Safari 或更稀有)上运行.

This userscript is running on plain Chrome (Or Opera, or scriptish, or Safari, or rarer).

因为目前只有大型引擎(Greasemonkey 和 Tampermonkey)支持 GM_info,如果您的脚本特别是跨浏览器,您将需要进行浏览器嗅探以在极少数情况下进行区分.
有关不需要 jQuery 的浏览器嗅探代码,请参阅此答案.

Because only the big engines (Greasemonkey and Tampermonkey) currently support GM_info, if your userscript is especially cross-browser, you will need to do browser sniffing to differentiate in rarer cases.
See this answer for browser-sniffing code that doesn't require jQuery.

注意:使用GM_info不需要@grant指令.

Note: A @grant directive is not needed to use GM_info.

这篇关于如何检测是否从 Chrome 商店安装了用户脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆