Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import tensorflow.js in chrome extension

I am developing a chrome extension, where I use my trained keras model. For this I need to import a library tensorflow.js. How should I do that?

I tried to import tensorflow.js in my project by two ways:

  1. in background.js by import * as tf from '@tensorflow/tfjs';

  2. I downloaded tf.min.js and tried to add it in my manifest.json

manifest.json

{
  "manifest_version": 2,
  "name": "my_project",
  "version": "0.1",

  "background": {
        "scripts": ["background.js", "tf.min.js"]
  },
  "content_scripts": [
      {
        "matches": [
          "<all_urls>"
        ],
        "js": ["jquery-3.1.1.min.js","content.js"]
      }
   ]
}

In the first case the error was "unexpected token *";

and in the second case the error was Uncaught (in promise) ReferenceError: tf is not defined.

What did I do wrong?

like image 624
my name Avatar asked Oct 31 '25 10:10

my name


1 Answers

I downloaded tf.min.js and tried to add it in my manifest.json

Try putting the tf.min.js first, like this:

{
  "background": {
    "scripts": ["tf.min.js", "background.js"]
  }
}

The scripts are loaded in the order you specify them and if you want to be able to use things from tf.min.js inside of background.js, tf.min.js has to be loaded first.

like image 127
Niklas Higi Avatar answered Nov 02 '25 00:11

Niklas Higi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!