FetFrumos
FetFrumos

Reputation: 5944

Flutter for web - how use Chrome Api?

I am creating my first Flutter app for web. I need using Chrome api. For example I need to use it:

chrome.runtime.onInstalled.addListener

   or

   chrome.tabs...

but unfortunately I did not find any information about it.

Is it possible?

Upvotes: 2

Views: 1428

Answers (2)

Xavier
Xavier

Reputation: 4005

You can use the chrome_extension package on pub.dev: https://pub.dev/packages/chrome_extension This is a typed Dart binding to all chrome.* APIs.

All the APIs are available and are generated from the Chrome IDL files.

Example using chrome.tabs.* API

import 'package:chrome_extension/tabs.dart';

void main() async {
  var tab =
      await chrome.tabs.query(QueryInfo(active: true, currentWindow: true));
}

Example using chrome.runtime.* API

import 'package:chrome_extension/runtime.dart';

void main() async {
  chrome.runtime.onInstalled.listen((e) {
    print('OnInstalled: ${e.reason}');
  });
}

Upvotes: 1

AlexApps99
AlexApps99

Reputation: 3584

Yes, the chrome.* APIs are JavaScript just like all the other web APIs, so you can use Dart's JS support.

That means using Dart's js library.

For example, you could bind a function something like this (untested, without any type annotation, just an example)

@JS('chrome.runtime.onInstalled.addListener')
external void addInstalledListener(Function callback);

Edit: If you'd rather pull in a dependency than roll your own, you can use something like chrome.dart.

import 'package:chrome/chrome_app.dart' as chrome;

void main() {
  chrome.runtime.getPlatformInfo().then((Map m) {
    print(m.toString());
  });
}

Upvotes: 3

Related Questions