mirror of
https://github.com/kodjodevf/mangayomi.git
synced 2026-04-21 16:01:58 +00:00
+
This commit is contained in:
parent
68ff513932
commit
6eeced8ca9
4 changed files with 5 additions and 172 deletions
|
|
@ -22,7 +22,7 @@ class $MElement implements MElement, $Instance {
|
||||||
'element': BridgeFieldDef(BridgeTypeAnnotation($Element.$type)),
|
'element': BridgeFieldDef(BridgeTypeAnnotation($Element.$type)),
|
||||||
},
|
},
|
||||||
getters: {
|
getters: {
|
||||||
'outerHTML': BridgeMethodDef(BridgeFunctionDef(
|
'outerHtml': BridgeMethodDef(BridgeFunctionDef(
|
||||||
returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string),
|
returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string),
|
||||||
nullable: true),
|
nullable: true),
|
||||||
)),
|
)),
|
||||||
|
|
@ -129,8 +129,8 @@ class $MElement implements MElement, $Instance {
|
||||||
@override
|
@override
|
||||||
$Value? $getProperty(Runtime runtime, String identifier) {
|
$Value? $getProperty(Runtime runtime, String identifier) {
|
||||||
switch (identifier) {
|
switch (identifier) {
|
||||||
case 'outerHTML':
|
case 'outerHtml':
|
||||||
final res = $value.outerHTML;
|
final res = $value.outerHtml;
|
||||||
return res == null ? const $null() : $String(res);
|
return res == null ? const $null() : $String(res);
|
||||||
case 'innerHtml':
|
case 'innerHtml':
|
||||||
final res = $value.innerHtml;
|
final res = $value.innerHtml;
|
||||||
|
|
@ -253,7 +253,7 @@ class $MElement implements MElement, $Instance {
|
||||||
MElement? get nextElementSibling => $value.nextElementSibling;
|
MElement? get nextElementSibling => $value.nextElementSibling;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String? get outerHTML => $value.outerHTML;
|
String? get outerHtml => $value.outerHtml;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
MElement? get parent => $value.parent;
|
MElement? get parent => $value.parent;
|
||||||
|
|
|
||||||
|
|
@ -513,51 +513,6 @@ class $MProvider extends MProvider with $Bridge<MProvider> {
|
||||||
false),
|
false),
|
||||||
]),
|
]),
|
||||||
),
|
),
|
||||||
'querySelector': BridgeMethodDef(
|
|
||||||
BridgeFunctionDef(
|
|
||||||
returns: BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)),
|
|
||||||
params: [
|
|
||||||
BridgeParameter(
|
|
||||||
'html',
|
|
||||||
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)),
|
|
||||||
false),
|
|
||||||
BridgeParameter(
|
|
||||||
'selector',
|
|
||||||
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)),
|
|
||||||
false),
|
|
||||||
BridgeParameter('typeElement',
|
|
||||||
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.int)), false),
|
|
||||||
BridgeParameter(
|
|
||||||
'attributes',
|
|
||||||
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)),
|
|
||||||
false),
|
|
||||||
]),
|
|
||||||
),
|
|
||||||
'querySelectorAll': BridgeMethodDef(
|
|
||||||
BridgeFunctionDef(
|
|
||||||
returns: BridgeTypeAnnotation(BridgeTypeRef(
|
|
||||||
CoreTypes.list, [BridgeTypeRef(CoreTypes.string)])),
|
|
||||||
params: [
|
|
||||||
BridgeParameter(
|
|
||||||
'html',
|
|
||||||
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)),
|
|
||||||
false),
|
|
||||||
],
|
|
||||||
namedParams: [
|
|
||||||
BridgeParameter(
|
|
||||||
'selector',
|
|
||||||
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)),
|
|
||||||
false),
|
|
||||||
BridgeParameter('typeElement',
|
|
||||||
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.int)), false),
|
|
||||||
BridgeParameter(
|
|
||||||
'attributes',
|
|
||||||
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.string)),
|
|
||||||
false),
|
|
||||||
BridgeParameter('typeRegExp',
|
|
||||||
BridgeTypeAnnotation(BridgeTypeRef(CoreTypes.int)), false),
|
|
||||||
]),
|
|
||||||
),
|
|
||||||
'xpath': BridgeMethodDef(
|
'xpath': BridgeMethodDef(
|
||||||
BridgeFunctionDef(
|
BridgeFunctionDef(
|
||||||
returns: BridgeTypeAnnotation(BridgeTypeRef(
|
returns: BridgeTypeAnnotation(BridgeTypeRef(
|
||||||
|
|
@ -956,8 +911,6 @@ class $MProvider extends MProvider with $Bridge<MProvider> {
|
||||||
.map((e) => $String(e))
|
.map((e) => $String(e))
|
||||||
.toList());
|
.toList());
|
||||||
}),
|
}),
|
||||||
"querySelector" => MBridge.querySelector,
|
|
||||||
"querySelectorAll" => MBridge.querySelectorAll,
|
|
||||||
"xpath" => MBridge.xpath,
|
"xpath" => MBridge.xpath,
|
||||||
_ => $Function((_, __, List<$Value?> args) {
|
_ => $Function((_, __, List<$Value?> args) {
|
||||||
throw UnimplementedError('Unknown property $identifier');
|
throw UnimplementedError('Unknown property $identifier');
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ class MElement {
|
||||||
|
|
||||||
final Element? _element;
|
final Element? _element;
|
||||||
|
|
||||||
String? get outerHTML => _element?.outerHtml;
|
String? get outerHtml => _element?.outerHtml;
|
||||||
|
|
||||||
String? get innerHtml => _element?.innerHtml;
|
String? get innerHtml => _element?.innerHtml;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,6 @@ import 'package:mangayomi/utils/extensions.dart';
|
||||||
import 'package:mangayomi/utils/reg_exp_matcher.dart';
|
import 'package:mangayomi/utils/reg_exp_matcher.dart';
|
||||||
import 'package:mangayomi/utils/xpath_selector.dart';
|
import 'package:mangayomi/utils/xpath_selector.dart';
|
||||||
import 'package:xpath_selector_html_parser/xpath_selector_html_parser.dart';
|
import 'package:xpath_selector_html_parser/xpath_selector_html_parser.dart';
|
||||||
import 'package:html/parser.dart' as parser;
|
|
||||||
import 'package:http/http.dart' as hp;
|
import 'package:http/http.dart' as hp;
|
||||||
import 'package:encrypt/encrypt.dart' as encrypt;
|
import 'package:encrypt/encrypt.dart' as encrypt;
|
||||||
|
|
||||||
|
|
@ -67,125 +66,6 @@ class WordSet {
|
||||||
}
|
}
|
||||||
|
|
||||||
class MBridge {
|
class MBridge {
|
||||||
///Seaches for the first descendant node matching the given selectors, using a preorder traversal.
|
|
||||||
static const $Function querySelector = $Function(_querySelector);
|
|
||||||
|
|
||||||
static $Value? _querySelector(_, __, List<$Value?> args) {
|
|
||||||
String html = args[0]!.$reified;
|
|
||||||
String selector = args[0]!.$reified;
|
|
||||||
int typeElement = args[0]!.$reified;
|
|
||||||
String attributes = args[0]!.$reified;
|
|
||||||
|
|
||||||
String res = "";
|
|
||||||
try {
|
|
||||||
var parse = parser.parse(html);
|
|
||||||
|
|
||||||
// return querySelector text
|
|
||||||
if (typeElement == 0) {
|
|
||||||
res = parse.querySelector(selector)!.text.trim().trimLeft().trimRight();
|
|
||||||
|
|
||||||
// return querySelector innerHtml
|
|
||||||
} else if (typeElement == 1) {
|
|
||||||
res = parse
|
|
||||||
.querySelector(selector)!
|
|
||||||
.innerHtml
|
|
||||||
.trim()
|
|
||||||
.trimLeft()
|
|
||||||
.trimRight();
|
|
||||||
|
|
||||||
// return querySelector outerHtml
|
|
||||||
} else if (typeElement == 2) {
|
|
||||||
res = parse
|
|
||||||
.querySelector(selector)!
|
|
||||||
.outerHtml
|
|
||||||
.trim()
|
|
||||||
.trimLeft()
|
|
||||||
.trimRight();
|
|
||||||
}
|
|
||||||
// return querySelector attributes
|
|
||||||
res = parse
|
|
||||||
.querySelector(selector)!
|
|
||||||
.attributes[attributes]!
|
|
||||||
.trim()
|
|
||||||
.trimLeft()
|
|
||||||
.trimRight();
|
|
||||||
return $String(res);
|
|
||||||
} catch (_) {
|
|
||||||
return $String("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///Returns all descendant nodes matching the given selectors, using a preorder traversal.
|
|
||||||
static const $Function querySelectorAll = $Function(_querySelectorAll);
|
|
||||||
|
|
||||||
static $Value? _querySelectorAll(_, __, List<$Value?> args) {
|
|
||||||
String html = args[0]!.$value;
|
|
||||||
String selector = args[1]!.$value;
|
|
||||||
int typeElement = args[2]!.$value;
|
|
||||||
String attributes = args[3]!.$value;
|
|
||||||
int typeRegExp = args[4]!.$value;
|
|
||||||
try {
|
|
||||||
var parse = parser.parse(html);
|
|
||||||
final a = parse.querySelectorAll(selector);
|
|
||||||
|
|
||||||
List<String> res = [];
|
|
||||||
for (var element in a) {
|
|
||||||
//text
|
|
||||||
if (typeElement == 0) {
|
|
||||||
res.add(element.text.trim().trimLeft().trimRight());
|
|
||||||
}
|
|
||||||
|
|
||||||
//innerHtml
|
|
||||||
else if (typeElement == 1) {
|
|
||||||
res.add(element.innerHtml.trim().trimLeft().trimRight());
|
|
||||||
}
|
|
||||||
|
|
||||||
//outerHtml
|
|
||||||
else if (typeElement == 2) {
|
|
||||||
res.add(element.outerHtml.trim().trimLeft().trimRight());
|
|
||||||
}
|
|
||||||
|
|
||||||
//attributes
|
|
||||||
else if (typeElement == 3) {
|
|
||||||
res.add(
|
|
||||||
element.attributes[attributes]!.trim().trimLeft().trimRight());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// if (typeRegExp == 0) is the default parameter
|
|
||||||
if (typeRegExp == 0) {
|
|
||||||
return $List.wrap(res.map((e) => $String(e)).toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> resRegExp = [];
|
|
||||||
for (var element in res) {
|
|
||||||
//get first element of href that match
|
|
||||||
if (typeRegExp == 1) {
|
|
||||||
resRegExp.add(regHrefMatcher(element.trim().trimLeft().trimRight()));
|
|
||||||
}
|
|
||||||
|
|
||||||
//get first element of src that match
|
|
||||||
else if (typeRegExp == 2) {
|
|
||||||
resRegExp.add(regSrcMatcher(element.trim().trimLeft().trimRight()));
|
|
||||||
}
|
|
||||||
|
|
||||||
//get first element of datasrc that match
|
|
||||||
else if (typeRegExp == 3) {
|
|
||||||
resRegExp
|
|
||||||
.add(regDataSrcMatcher(element.trim().trimLeft().trimRight()));
|
|
||||||
}
|
|
||||||
|
|
||||||
//get first element of img that match
|
|
||||||
else if (typeRegExp == 4) {
|
|
||||||
resRegExp.add(regImgMatcher(element.trim().trimLeft().trimRight()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $List.wrap(resRegExp.map((e) => $String(e)).toList());
|
|
||||||
} catch (_) {
|
|
||||||
// botToast(e.toString());
|
|
||||||
return $List.wrap([]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static MDocument parsHtml(String html) {
|
static MDocument parsHtml(String html) {
|
||||||
return MDocument(Document.html(html));
|
return MDocument(Document.html(html));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue