Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using chrome api with React.js

I've been trying to make a simple Chrome Extension using React. The manifest looks something like this:

{
  "name": "New Tab",
  "version": "1.0",
  "manifest_version": 2,
  "description": "A minimalist replacement for Chrome's New Tab page.",
  "chrome_url_overrides": {
    "newtab": "index.html"
  },
  "permissions": ["tabs", "topSites"]
}

I'm not sure how to actually use the Chrome topSites API in React. I've been looking at the documentation https://developer.chrome.com/extensions/topSites but it doesn't provide much insight. I'm trying to do something like this to get an array of data.

chrome.topSites.get(console.log)

However,

'chrome' is not defined  no-undef
like image 982
Austin Wolfe Avatar asked Jul 18 '18 21:07

Austin Wolfe


People also ask

Can you make Chrome Extensions With React?

Normally, when you create a Chrome extension, you need to create a manifest. json file, but we're in luck! Create React App creates a manifest. json file by default — you can find it in your public folder.

Does Chrome support React?

React Native for Web is designed and tested for recent mobile and desktop browsers, for touch and mouse and keyboard interactions. The browsers with known support include: Chrome 60+


2 Answers

You can define chrome by adding /*global chrome*/ in the top of the file.

For Example

    /*global chrome*/
import React, { Component } from 'react';
import './App.css';

    class App extends Component {
        componentWillMount() {
            chrome.storage.local.set({ 'data' : 'Your data'}, function(result) {
                console.log(" Data saved ")
            });
        }

        render() {
            return (
              <div className="App">
              </div>
            );
        }
    }

    export default App;
like image 196
Mansi Teharia Avatar answered Oct 22 '22 11:10

Mansi Teharia


You need to disable the ESLint rule no-undef and then enable it again:

/* eslint-disable no-undef */
function callback(val) {
    console.log(val)
}

chrome.topSites.get(callback);
/* eslint-enable no-undef */
like image 21
Ivor Avatar answered Oct 22 '22 11:10

Ivor