web_synchronization_tool/lib/JavaScriptString.dart

118 lines
2.9 KiB
Dart
Raw Normal View History

2024-03-30 10:58:39 +08:00
class JavaScriptString {
/// 点击监听
2024-03-31 20:15:43 +08:00
static String clickEventJSString =
2024-03-30 10:58:39 +08:00
'''document.addEventListener('click', function(event) {
var x = event.clientX;
var y = event.clientY;
console.log('点击坐标x=' + x + ', y=' + y);
2024-03-31 20:15:43 +08:00
window.flutter_inappwebview.callHandler('click', x, y);
2024-03-30 10:58:39 +08:00
});''';
2024-03-31 20:15:43 +08:00
/// 触摸监听
static String touchendEventJSString =
'''document.addEventListener('touchend', function(event) {
var x = event.changedTouches[0].clientX;
var y = event.changedTouches[0].clientY;
// 获取目标元素
var target = event.target;
// 获取目标元素的class和id
var targetClass = target.className;
var targetId = target.id;
console.log('Class: ' + targetClass);
console.log('Id: ' + targetId);
console.log('触摸坐标x=' + x + ', y=' + y);
window.flutter_inappwebview.callHandler('touchend', x, y);
});''';
/// 退出登录
static String loginOutJsString = '''
\$.ajax({
type: "GET",
url: "/api/logout.do",
success: function(t) {
"index.html" != location.pathname ? window.location.href = "index.html" : location.reload()
},
error: function(t) {
var e = \$.parseJSON(t.responseText + "");
alert(e.msg, 2)
}
})
''';
2024-03-30 10:58:39 +08:00
/// 模拟点击
static String clickJSString(int x, int y) {
return 'document.elementFromPoint($x, $y).click();';
}
2024-03-31 20:15:43 +08:00
/// 模拟触摸
static String touchendJsString(int x, int y) {
return '''
''';
}
/// 模拟触摸
static String getClassTouchendJsString(int x, int y) {
return '''
try{
// 获取鼠标点击位置的坐标
var x = $x;
var y = $y;
// 创建一个touchstart事件
var touchstartEvent = new TouchEvent('touchstart', {
bubbles: true,
cancelable: true,
view: window,
changedTouches: [new Touch({ identifier: Date.now(), target: document.body, clientX: x, clientY: y })],
targetTouches: [new Touch({ identifier: Date.now(), target: document.body, clientX: x, clientY: y })]
});
// touchstartEvent
document.body.dispatchEvent(touchstartEvent);
// 创建一个touchend事件
var touchendEvent = new TouchEvent('touchend', {
bubbles: true,
cancelable: true,
view: window,
changedTouches: [new Touch({ identifier: Date.now(), target: document.body, clientX: x, clientY: y })],
targetTouches: [new Touch({ identifier: Date.now(), target: document.body, clientX: x, clientY: y })]
});
// 触发touchend事件
document.body.dispatchEvent(touchendEvent);
} catch (t) {
console.log('模拟触摸错误 -- ' + t);
}
''';
}
2024-03-30 10:58:39 +08:00
/// 输入
static String inputJsString(int value) {
return '''
var inputEvent = new Event('input', {
bubbles: true,
cancelable: true,
});
2024-03-31 20:15:43 +08:00
var inputElement = document.querySelector(".input");
2024-03-30 10:58:39 +08:00
inputElement.value = "$value";
inputElement.dispatchEvent(inputEvent);
''';
}
}