javascript - 如何使用 InboxSDK 在 Gmail 和其他网站上运行 chrome 扩展程序?
问题描述
我正在开发一个在 Gmail 上运行的带有 InboxSDK 的 Google Chrome 扩展程序。现在 Gmail 部分已准备就绪,我想通过不需要 InboxSDK 或 Gmail 功能的部分功能扩展到其他网站。我的想法是尝试加载 InboxSDK(它会检查支持的来源),如果没有加载,请跳过 Gmail 功能。
话虽如此,我遇到了一个问题,即尝试通过调用来加载 InboxSDK 会InboxSDK.load(2, "app_id")
在控制台中打印一条消息:InboxSDK: Unsupported origin https://stackoverflow.com
并停止执行超出该点的任何代码。
我不需要 Gmail 以外的其他来源的 InboxSDK。
是否可以在 Gmail 上使用 InboxSDK 而在其他网站上不使用 InboxSDK 运行相同的扩展程序?
我提供了一个简单的 TypeScript 代码来说明这种情况:
import * as InboxSDK from "inboxsdk"
(async function main() {
console.log("This will print on all websites")
const sdk = await InboxSDK.load(2, "APP_ID")
console.log("This will not be printed if the InboxSDK load fails")
})()
我看到的一个可能的解决方案是实现与 InboxSDK 相同的来源检查。我正在寻找一种不需要复制此 InboxSDK 功能的更好的解决方案。
解决方案
您如何使用不同的内容脚本在 gmail url 和其他内容上初始化您的代码。然后甚至不要在不需要的地方加载 inboxSDK。应该给你更好的控制,只在需要时加载 inboxSDK。这也可能会解决“不受支持的来源”的问题。
"content_scripts": [
{
"matches": ["https://mail.google.com/*"],
"js": [
"libs/inboxsdk.js",
"script/my_entry_with_inboxsdk.js"
],
"runt_at": "document_end"
},
{
"matches": ["https://*.everything-but-gmail.*"],
"js": [
"script/my_entry_without_inboxsdk.js"
],
"runt_at": "document_end"
}
],
推荐阅读
- keras - keras 损失是 nan 但准确度定义明确
- android - android中的phonegap启动画面是黑色的
- mysql - 查找具有 %latin1% 值的数据库的所有列
- function - babel register 的 firebase 部署错误
- android - AOSP 6 单音量控制
- c++ - 如何从函数返回 grpc::ClientContext?
- ruby-on-rails - Rails 路由优先级偏爱第一个
- elasticsearch - Logstash nginx 过滤器不适用于一半的行
- php - angularjs 从 mysql 加载数据,然后由 angularjs 显示
- objective-c - 如何在objective-c的nsmutablearray中存储长值