【问题标题】:How do I make a chrome extension with InboxSDK run both on Gmail and on other websites?如何使用 InboxSDK 在 Gmail 和其他网站上运行 chrome 扩展程序?
【发布时间】:2019-10-31 23:39:44
【问题描述】:

我正在开发一个在 Gmail 上运行的带有 InboxSDK 的 Google Chrome 扩展程序。现在 Gmail 部分已准备就绪,我想通过不需要 InboxSDK 或 Gmail 功能的部分功能扩展到其他网站。我的想法是尝试加载 InboxSDK(它检查支持的来源),如果没有加载,请跳过 Gmail 功能。

话虽如此,我遇到了一个问题,即尝试通过调用 InboxSDK.load(2, "app_id") 来加载 InboxSDK 会在控制台中打印一条消息: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 功能的更好的解决方案。

【问题讨论】:

    标签: javascript google-chrome-extension inboxsdk


    【解决方案1】:

    你如何使用不同的内容脚本来初始化你的 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"
    
    }
    

    ],

    【讨论】:

      猜你喜欢
      • 2018-10-08
      • 1970-01-01
      • 2012-05-03
      • 2018-09-24
      • 2015-03-09
      • 2023-04-09
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多