简体   繁体   English

Angular 2/4中的Chrome扩展

[英]Chrome extension in Angular 2/4

I am trying to create a chrome extension for google chrome browser using Angular 4. I just need the url and title of the page, where I am clicking on the browser icon(click event). 我正在尝试使用Angular 4为Google chrome浏览器创建一个chrome扩展。我只需要页面的URL和标题,即可在其中单击浏览器图标(click事件)。 So far I have done following things 到目前为止,我已经完成了以下操作

I have created a eventPage and that page is getting called when I click the icon of extension. 我创建了一个eventPage,当我单击扩展图标时,该页面将被调用。 Code=> 代码=>

if (typeof chrome.browserAction !== 'undefined') {
    chrome.browserAction.onClicked.addListener(function(tab) {
        listService.getBookmarks().then(bookmarkLists => {
            bookmarkLists = bookmarkLists;
            getSelectedTab(bookmarkLists);
        });
    });
} else {
    console.log('EventPage initialized');
}

function getSelectedTab(bookmarkLists) {
    chrome.tabs.getSelected(null, function(tab) {
        let newBookmark: Object = {
            name: tab.title,
            url: tab.url
        };
        bookmarkLists.push(newBookmark);
        listService.setBookmarks(bookmarkLists);
    });
}

listService.getBookmarks => listService has a method getBookmarks which already has some data as key, value pair. listService.getBookmarks => listService有一个getBookmarks方法,该方法已经有一些数据作为键,值对。

IF anybody can tell just how to get the URL and Title of the page where I am triggering click on the extension click. 如果任何人都可以说出如何获取我触发的页面的URL和标题,请单击扩展点击。

Suggested Edits Manifest.json 建议编辑 Manifest.json

{
  "manifest_version": 2,

  "name": "Extension",
  "description": "This extension ...",
  "version": "1.0",
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",

  "browser_action": {
    "default_popup": "index.html"
  },
  "permissions": [
    "storage",
    "tabs",
    "activeTab",
    "http://*/*",
    "https://*/*"
  ],

  "background": {
    "page": "index.html",
    "persistent": false
  },

  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}

This is how I got the url and title in extension 这是我在扩展名中获得URL和标题的方式

var that = this;
chrome.tabs.query({
    currentWindow: true,
    active: true
}, function(tabs) {
  console.log(tabs[0]);
  if (tabs.length > 0) {
    if (tabs[0].url !== undefined && tabs[0].url !== null && tabs[0].url !== '') {
      that.tabId = tabs[0].id;
      that.tabURL = tabs[0].url;
      that.tabTitle = tabs[0].title;
      that.favIconUrl = tabs[0].favIconUrl;
    }
  }
});

include this file on top of the component 将此文件包含在组件顶部

///<reference path="../../../node_modules/@types/chrome/index.d.ts" />

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

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