【问题标题】:Access to chrome extension through angular2 and typescript通过 angular2 和 typescript 访问 chrome 扩展
【发布时间】:2017-01-05 14:17:54
【问题描述】:

尝试使用 angular2 和 typescript 进行 chrome 扩展,我陷入了如何访问 chrome API(尤其是 chrome.bookmarks)的困境。

我确实可以通过关注angular2 chrome extension chrome.runtime.sendMessage from typescript 访问 chrome 对象

但即使我可以访问 chrome,我也无法访问 chrome.bookmarks

manifest.json

{
    "manifest_version": 2,
    "name": "x",
    "short_name": "x",
    "description": "x",
    "version": "0.1.0",
    "author": "x",
    "permissions": [
        "bookmarks",
        "https://ajax.googleapis.com/"
    ]
}

然后,我似乎应该按照这里的建议通过@types 访问 chrome Using chrome extension apis in typescript

但我找不到方法。我应该进口什么? import { Chrome, filesystem, filewriter } from '@types'; ?我在这里迷路了,我找不到有关此的文档(这让我相信使用 angular2 进行 chrome 扩展不是一个好主意)

【问题讨论】:

  • 请向我们展示如何来回。在哪里(我希望背景)你正在寻找书签调用。

标签: javascript angular typescript google-chrome-extension


【解决方案1】:

chrome 服务在运行时(在浏览器中)可供您的应用使用,而您没有(或实际上无法导入它)。对我有用的是声明一个 var 以避免编译错误:

declare var chrome;

然后在每个处理 chrome 的代码段中,首先检查它是否存在(以避免开发时间崩溃):

funcThatUsesChrome(args) {
   if(chrome){
      chrome.doSomeAction();
   }
}

【讨论】:

  • 您的意思是在浏览器中作为扩展时?因为该应用程序确实在浏览器中运行 (localhost:3000)。而且,用户@types/chrome 的方式是什么?
  • 是的,这就是我的意思。只有当您的应用程序作为扩展程序运行时,Chrome 才可用。我不熟悉@types/chrome 解决方案。
猜你喜欢
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 2016-11-21
  • 2012-06-14
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多