搜尋套件、儲存庫與 APP 分類

套件詳情v2.3.0

universal_html

一個可在所有平台(包括 Flutter 和伺服器端)上運作的 'dart:html',可簡化跨平台開發與 HTML/XML 處理。

AndroidiOSmacOSWindowsLinux
近 30 天下載
66.2萬
喜歡數
516
Pub 分數
150

安裝套件

flutter pub add universal_html

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

universal_html 提供了一個統一的 'dart:html' API,可在所有平台運作,包括移動設備、桌面及伺服器端環境中的 Flutter。它無論在何種執行時環境下都能實現一致的 HTML 與 XML 處理,簡化跨平台開發。此套件透過提供相容的 DOM 操作、HTTP 請求以及結構化內容解析介面,彌補了 Web 與非 Web 環境之間的差距。非常適合在 Web 與後端服務之間共用邏輯,減少程式碼重複並優化開發流程。

適用情境

  • 跨平台的 Web 和伺服器邏輯
  • 共享 HTML/XML 解析
  • 伺服器端渲染工具
  • Flutter 應用中的 Web 組件重用
  • 建構通用 Dart 應用程式

主要特色

  • 跨平台的 dart:html API
  • 支援在所有平台上的 DOM 操作
  • 內建的 XML/HTML 解析
  • 與 Flutter 和伺服器無縫配合
  • 輕量且依賴極少

適合對象

  • 針對多平台的開發者
  • 需要HTML處理的後端服務
  • 需要類似 Web 功能的 Flutter 應用
  • Web 與伺服器之間的程式碼共用
  • 避免使用平台特定條件判斷的專案

使用建議

  • 非 Web 平台上的瀏覽器特定功能有限
  • 不支援網頁以外的某些 DOM 事件
  • 伺服器端執行時效能可能會有差異
  • 不適用於完整的瀏覽器模擬
  • 需要謹慎處理非同步操作

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Pub Package package publisher Github Actions CI

Introduction

A cross-platform replacement for dart:html (or package:web):

  • Eases cross-platform development
    • You can use this package in browsers, mobile, desktop, and server-side VM, and Node.JS.
    • Just replace dart:html / package:web imports with package:universal_html/universal_html.dart. Normal dart:html will continue to be used when application run in browsers.
  • Extensive support for processing HTML and XML documents
  • EventSource streaming support
    • Cross-platform dart:html EventSource ("application/event-stream").
    • If you want to customize EventSource HTTP headers outside browsers, see EventSourceOutsideBrowser.

The project is licensed under the Apache License 2.0. Some of the source code was adopted from the original dart:html in Dart SDK, which is documented in the relevant files.

Documentation

Similar projects

Getting started

1. Add dependency

In pubspec.yaml:

dependencies:
  universal_html: ^2.3.0

2. Use

import "package:universal_html/universal_html.dart";

void main() {
  // Create a DOM tree
  final div = DivElement();
  div.append(Element.tag("h1")
    ..classes.add("greeting")
    ..appendText("Hello world!"));

  // Print outer HTML
  print(div.outerHtml);
  // --> <div><h1>Hello world</h1></div>

  // Do a CSS query
  print(div.querySelector("div > .greeting").text);
  // --> Hello world
}

Examples

Parsing HTML

Use parseHtmlDocument:

import 'package:universal_html/parsing.dart';

void main() {
  final htmlDocument = parseHtmlDocument('<html>...</html>');
}

Parsing XML

Use parseXmlDocument:

import 'package:universal_html/parsing.dart';

void main() {
  final xmlDocument = parseXmlDocument('<xml>...</xml>');
}

Scraping a website

Load a Window with WindowController:

import 'dart:io' show Cookie;
import 'package:universal_html/controller.dart';

Future main() async {
  // Load a document.
  final controller = WindowController();
  controller.defaultHttpClient.userAgent = 'My Hacker News client';
  await controller.openHttp(
    method: 'GET',
    uri: Uri.parse("https://news.ycombinator.com/"),
    onRequest: (HttpClientRequest request) {
      // Add custom headers
      request.headers.set('Authorization', 'headerValue');
      request.cookies.add(Cookie('cookieName', 'cookieValue'));
    },
    onResponse: (HttpClientResponse response) {
      print('Status code: ${response.statusCode}');
    },
  );

  // Select the top story using a CSS query
  final titleElements = controller.document.querySelectorAll(".athing > .title");
  final topStoryTitle = titleElements.first.text;

  // Print result
  print("Top Hacker News story is: $topStoryTitle");
}

EventSource

EventSource (see mozilla.org) is a browser API for reading "application/event-stream" streams. It has been supported by browsers for a long time.

import 'package:universal_html/universal_html.dart';

Future<void> main() async {
  final eventSource = EventSource('http://example.com/events');
  await for (var message in event.onMessage) {
    print('Event type: ${message.type}');
    print('Event data: ${message.data}');
  }
}

EventSource requests from real browsers are typically authenticated using cookies. If you want to add cookies or customize other HTTP headers, you need to use EventSourceOutsideBrowser:

import 'package:universal_html/universal_html.dart';
import 'dart:io' show Cookie;

Future<void> main() async {
  final eventSource = EventSource('http://example.com/events');
  
  // The following block will NOT be executed in browsers.
  // Because compiler can infer instances of EventSourceOutsideBrowser are never constructed,
  // it will not appear in Javascript either.
  if (eventSource is EventSourceOutsideBrowser) {
    eventSource.onHttpClientRequest = (eventSource, request) {
      request.headers.set('Authorization', 'example');
      request.cookies.add(Cookie('name', 'value'));
    };
    eventSource.onHttpClientResponse = (eventSource, request, response) {
      // ...
    };
  }
  
  await for (var message in eventSource.onMessage) {
    print('Event:');
    print('  type: ${message.type}');
    print('  data: ${message.data}');
  }
}

Testing

import 'package:universal_html/controller.dart';
import 'package:test/test.dart';

void main() {
  setUp(() {
    WindowController.instance = WindowController();
  });
  
  test('test #1', () {
    // ...
  });
  
  test('test #2', () {
    // ...
  });
}

所屬分類

依賴

依賴清單 9 項

原始碼儲存庫

使用此套件的專案