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). 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. 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.

IF anybody can tell just how to get the URL and Title of the page where I am triggering click on the extension click.

Suggested Edits 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

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" />
链接地址: http://www.djcxy.com/p/66586.html

上一篇: 为什么它生病了

下一篇: 在Angular 2/4中的Chrome扩展