1

I am trying to use tensorflowjs in my chrome extension. But,I am not able to figure out how to do it. I tried to download tf.min.js file by going to this link : https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js I included this file in the manifest file:

{
  "name": "Getting Started Example",
  "version": "1.0",
  "description": "Build an Extension!",
  "permissions": ["activeTab", "declarativeContent", "storage","tabs", "<all_urls>"],
  "options_page": "options.html",
  "background": {
    "scripts": ["tf.min.js", "background.js"],
    "persistent": false
  },
  "page_action": {
    "default_popup": "popup.html",
    "default_icon": {
      "16": "images/get_started16.png",
      "32": "images/get_started32.png",
      "48": "images/get_started48.png",
      "128": "images/get_started128.png"
    }
  },
  "icons": {
    "16": "images/get_started16.png",
    "32": "images/get_started32.png",
    "48": "images/get_started48.png",
    "128": "images/get_started128.png"
  },
  "manifest_version": 2,
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}

As you can see, I've also set content_security_policy to script-src 'self' 'unsafe-eval'; object-src 'self'. But, I am always getting multiple warnings in the tf.min.js and errors in the content.js file(Registration of backend webgl failed), which is:

// Copyright 2018 The Chromium Authors. All rights reserved.
import * as tf from '@tensorflow/tfjs';

bth1.onclick = function scrapeThePage() {
    // Keep this function isolated - it can only call methods you set up in content scripts
    var htmlCode = document.documentElement.outerHTML;
    var btn = document.getElementById("mybtn1");

    var keywords = ['a','b','c'];

    var arr = [];

    for(let i = 0; i < keywords.length; i++){
    var reg = new RegExp(`${keywords[i]}`,'g')
    arr[i] = Math.log(1+(htmlCode.match(reg) || []).length);
    }

  btn.innerText(arr)

}

Am I downloading the file from the wrong website? How do I use tensorflowjs in chrome extensions?

1
  • Try their official extension sample, this may help you how to implement tensorflow js in your extension. Commented Aug 1, 2019 at 1:25

1 Answer 1

1

This is the reference I used for my first tensorflowjs code,

<html>
<head>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script> 
</head> 
<body> 
    <div id="output_field"></div> 
</body> 
<script> 
    async function learnLinear(){ 
        const model = tf.sequential(); 
        model.add(tf.layers.dense({units: 1, inputShape: [1]})); 
        model.compile({ loss: 'meanSquaredError', optimizer: 'sgd' }); 
        const xs = tf.tensor2d([-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0], [7, 1]); 
        const ys = tf.tensor2d([-10,  -7.0, -4.0,-1.0, 2.0, 5.0, 8.0], [7, 1]); 
        await model.fit(xs, ys, {epochs: 500}); 
        document.getElementById('output_field').innerText = model.predict(tf.tensor2d([10, 11], [2, 1])); 
    } 
    learnLinear(); 
</script> 
<html>

reference: https://medium.com/tensorflow/getting-started-with-tensorflow-js-50f6783489b2

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure whether it works with chrome extensions. It seems to violate a security policy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.