简体   繁体   English

通过angular2和Typescript访问chrome扩展

[英]Access to chrome extension through angular2 and typescript

Trying to do an chrome extension with angular2 and typescript, I am stuck in how to get access to the chrome API (chrome.bookmarks in particular). 尝试使用angular2和typescript进行chrome扩展时,我陷入了如何访问chrome API(特别是chrome.bookmarks)的困境。

I do have access to the chrome object by following angular2 chrome extension chrome.runtime.sendMessage from typescript 我确实可以通过遵循TypeScript中的angular2 chrome扩展chrome.runtime.sendMessage来访问chrome对象

But even though I can access to chrome, I cannot to chrome.bookmarks 但是即使我可以访问chrome,也无法访问chrome.bookmarks

manifest.json manifest.json

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

And then, it seems that I should be accessing chrome through @types as suggested here Using chrome extension apis in typescript 然后,似乎我应该按照此处建议通过@types访问chrome。在typescript中使用chrome扩展api

But I can't find how. 但是我找不到方法。 Should I import what? 我应该进口什么吗? import { Chrome, filesystem, filewriter } from '@types'; ? I'm lost here, and I cannot find documentation about that (what makes me believe it is not a good idea to use angular2 for a chrome extension) 我在这里迷路了,而且我找不到关于它的文档(这使我相信使用angular2进行chrome扩展不是一个好主意)

The chrome service is made available to your app at run time (when in the browser), and you don't have (or actually can't import it). chrome服务可在运行时(在浏览器中)供您的应用使用,而您没有(或实际上无法导入)。 What worked for me was to declare a var to avoid compilation errors: 对我有用的是声明一个var以避免编译错误:

declare var chrome;

and then in each code segment that deals with chrome, to first check if it exists (to avoid dev time crashes): 然后在处理chrome的每个代码段中,首先检查其是否存在(以避免开发时间崩溃):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM