Skip to content

Commit

Permalink
Adds a setter for the TargetInfo type (#202)
Browse files Browse the repository at this point in the history
Resolves #201

Adds a `type` setter on `Target` which sets the `TargetInfo` type.
  • Loading branch information
elliette authored Dec 8, 2022
1 parent 649f4b3 commit a0d4419
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
6 changes: 5 additions & 1 deletion lib/src/page/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ class Page {
await page._initialize();

if (viewport != null) {
await page.setViewport(viewport);
try {
await page.setViewport(viewport);
} catch (e) {
_logger.warning('Unable to set viewport for ${target.url}: $e');
}
}

for (var plugin in page.browser.plugins) {
Expand Down
29 changes: 21 additions & 8 deletions lib/src/target.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,23 @@ class Target {
/// Identifies what kind of target this is.
/// Can be `"page"`, [`"background_page"`](https://developer.chrome.com/extensions/background_pages),
/// `"service_worker"`, `"shared_worker"`, `"browser"` or `"other"`.
String? get type {
String get type {
var type = _info!.type;
if (const [
'page',
'background_page',
'service_worker',
'shared_worker',
'browser'
].contains(type)) return type;
if (_possibleTargetTypes.contains(type)) return type;
return 'other';
}

/// Sets the kind of target this is.
/// Must be `"page"`, [`"background_page"`](https://developer.chrome.com/extensions/background_pages),
/// `"service_worker"`, `"shared_worker"`, or `"browser"`.
set type(String targetType) {
if (_possibleTargetTypes.contains(targetType) && _info != null) {
final json = _info!.toJson();
json['type'] = targetType;
_info = TargetInfo.fromJson(json);
}
}

/// Get the target that opened this target. Top-level targets return `null`.
Target? get opener {
return _info!.openerId != null ? browser.targetById(_info!.openerId) : null;
Expand Down Expand Up @@ -112,3 +117,11 @@ class Target {
_closedCompleter.complete();
}
}

const _possibleTargetTypes = [
'page',
'background_page',
'service_worker',
'shared_worker',
'browser',
];

0 comments on commit a0d4419

Please sign in to comment.