Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade definition files #11

Merged
merged 6 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
steps:
- uses: subosito/flutter-action@v2
with:
channel: 'stable'
channel: 'master'
- uses: actions/checkout@v4
- run: dart --version
- run: |
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.2.0

- Regenerate the bindings based on Chrome v116
- Update JS binding for Dart 3.2
- Require Dart SDK >= 3.2

## 0.1.2

- Update the readme to give some tips on how to build Chrome extensions with Flutter.
Expand Down
66 changes: 55 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,64 @@ The JS interop is build on top of `dart:js_interop` (static interop) which make

### Example

#### `chrome.tabs`
```dart
import 'package:chrome_extension/tabs.dart';

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

#### `chrome.alarms`
```dart
import 'package:chrome_extension/alarms.dart';
import 'package:chrome_extension/power.dart';
import 'package:chrome_extension/storage.dart';

void main() async {
// Use the chrome.power API
await chrome.alarms.create('MyAlarm', AlarmCreateInfo(delayInMinutes: 2));

var alarm = await chrome.alarms.get('MyAlarm');
print(alarm!.name);
}
```

#### `chrome.power`
```dart
import 'package:chrome_extension/power.dart';

void main() {
chrome.power.requestKeepAwake(Level.display);
}
```

#### `chrome.runtime`
```dart
import 'dart:js_interop';
import 'package:chrome_extension/runtime.dart';

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

chrome.runtime.onMessage.listen((e) {
e.sendResponse.callAsFunction(null, {'the_response': 1}.jsify());
});
}
```

#### `chrome.storage`
```dart
import 'package:chrome_extension/storage.dart';

// Use the chrome.storage API
void main() async {
await chrome.storage.local.set({'mykey': 'value'});
var values = await chrome.storage.local.get(null /* all */);
print(values['mykey']);

// Use the chrome.alarms API
await chrome.alarms.create('MyAlarm', AlarmCreateInfo(delayInMinutes: 2));
}
```

Expand Down Expand Up @@ -124,14 +166,16 @@ void main() async {

## Tips to build Chrome extensions with Flutter

Here are some personal tips to build Chrome extension using the Flutter UI framework.

#### Develop the app using Flutter Desktop

In order to develop in a comfortable environment with hot-reload,
most of the app can be developed using Flutter desktop.
most of the app (the UI part) should be developed using Flutter desktop.

This will require an abstraction layer between the UI and the `chrome_extension` APIs.
This requires an abstraction layer between the UI and the `chrome_extension` APIs.

A fake implementation of this abstraction layer is used in the Desktop entry point:
In the Desktop entry point, a fake implementation of this abstraction layer is used, like this:

```dart
// lib/main_desktop.dart
Expand All @@ -154,7 +198,7 @@ class FakeBookmarkService implements BookmarkService {
Launch this entry point in desktop with
`flutter run -t lib/main_desktop.dart -d macos|windows|linux`

Create the real entry point:
And the real entry point (the one used in the actual compiled extension) looks like:

```dart
// lib/main.dart
Expand Down
33 changes: 28 additions & 5 deletions README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,29 @@ The JS interop is build on top of `dart:js_interop` (static interop) which make

### Example

#### `chrome.tabs`
```dart
import 'example/example.dart';
import 'example/chrome_tabs.dart';
```

#### `chrome.alarms`
```dart
import 'example/chrome_alarms.dart';
```

#### `chrome.power`
```dart
import 'example/chrome_power.dart';
```

#### `chrome.runtime`
```dart
import 'example/chrome_runtime.dart';
```

#### `chrome.storage`
```dart
import 'example/chrome_storage.dart';
```

### Available APIs
Expand All @@ -28,14 +49,16 @@ import 'example/example.dart';

## Tips to build Chrome extensions with Flutter

Here are some personal tips to build Chrome extension using the Flutter UI framework.

#### Develop the app using Flutter Desktop

In order to develop in a comfortable environment with hot-reload,
most of the app can be developed using Flutter desktop.
most of the app (the UI part) should be developed using Flutter desktop.

This will require an abstraction layer between the UI and the `chrome_extension` APIs.
This requires an abstraction layer between the UI and the `chrome_extension` APIs.

A fake implementation of this abstraction layer is used in the Desktop entry point:
In the Desktop entry point, a fake implementation of this abstraction layer is used, like this:

```dart
import 'example/desktop_entry_point.dart#example';
Expand All @@ -44,7 +67,7 @@ import 'example/desktop_entry_point.dart#example';
Launch this entry point in desktop with
`flutter run -t lib/main_desktop.dart -d macos|windows|linux`

Create the real entry point:
And the real entry point (the one used in the actual compiled extension) looks like:

```dart
import 'example/real_entry_point.dart#example';
Expand Down
8 changes: 8 additions & 0 deletions example/chrome_alarms.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:chrome_extension/alarms.dart';

void main() async {
await chrome.alarms.create('MyAlarm', AlarmCreateInfo(delayInMinutes: 2));

var alarm = await chrome.alarms.get('MyAlarm');
print(alarm!.name);
}
5 changes: 5 additions & 0 deletions example/chrome_power.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:chrome_extension/power.dart';

void main() {
chrome.power.requestKeepAwake(Level.display);
}
12 changes: 12 additions & 0 deletions example/chrome_runtime.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'dart:js_interop';
import 'package:chrome_extension/runtime.dart';

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

chrome.runtime.onMessage.listen((e) {
e.sendResponse.callAsFunction(null, {'the_response': 1}.jsify());
});
}
7 changes: 7 additions & 0 deletions example/chrome_storage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:chrome_extension/storage.dart';

void main() async {
await chrome.storage.local.set({'mykey': 'value'});
var values = await chrome.storage.local.get(null /* all */);
print(values['mykey']);
}
9 changes: 9 additions & 0 deletions example/chrome_tabs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:chrome_extension/tabs.dart';

void main() async {
var tabs = await chrome.tabs.query(QueryInfo(
active: true,
currentWindow: true,
));
print(tabs.first.title);
}
32 changes: 28 additions & 4 deletions extension_examples/dart/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ packages:
path: "../.."
relative: true
source: path
version: "0.1.2"
version: "0.3.0"
collection:
dependency: transitive
description:
Expand Down Expand Up @@ -92,10 +92,10 @@ packages:
dependency: transitive
description:
name: file
sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c"
sha256: "1b92bec4fc2a72f59a8e15af5f52cd441e4a7860b49499d69dfa817af20e925d"
url: "https://pub.dev"
source: hosted
version: "7.0.0"
version: "6.1.4"
frontend_server_client:
dependency: transitive
description:
Expand Down Expand Up @@ -208,6 +208,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.3"
platform:
dependency: transitive
description:
name: platform
sha256: ae68c7bfcd7383af3629daafb32fb4e8681c7154428da4febcff06200585f102
url: "https://pub.dev"
source: hosted
version: "3.1.2"
pool:
dependency: transitive
description:
Expand All @@ -216,6 +224,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.5.1"
process:
dependency: transitive
description:
name: process
sha256: "53fd8db9cec1d37b0574e12f07520d582019cb6c44abf5479a01505099a34a09"
url: "https://pub.dev"
source: hosted
version: "4.2.4"
process_runner:
dependency: "direct dev"
description:
name: process_runner
sha256: a3511a69d42216522166583f19cce6bf44efe109f3ca52fc7994f64bbdbe01e6
url: "https://pub.dev"
source: hosted
version: "4.1.2"
pub_semver:
dependency: transitive
description:
Expand Down Expand Up @@ -393,4 +417,4 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.1.0 <4.0.0"
dart: ">=3.2.0-0 <4.0.0"
1 change: 1 addition & 0 deletions extension_examples/dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ dependencies:

dev_dependencies:
lints:
process_runner:
test:
15 changes: 15 additions & 0 deletions extension_examples/dart/tool/compile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'dart:io';
import 'package:process_runner/process_runner.dart';

final _process = ProcessRunner(printOutputDefault: true);

void main() async {
await _process.runProcess([
Platform.resolvedExecutable,
'compile',
'js',
'web/main.dart',
'-o',
'web/_main.dart.js',
]);
}
5 changes: 4 additions & 1 deletion extension_examples/dart/web/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'dart:js_interop';
import 'package:chrome_extension/tabs.dart';
import 'package:web/web.dart';

void main() {
chrome.tabs.query(QueryInfo());

var button = document.querySelector('#startButton')! as HTMLElement;
button.addEventListener(
'click',
(PointerEvent e) async {
(PointerEvent e) {
// Demonstrate apis
}
.toJS);
Expand Down
Loading