aardio网页组件:webPageOperation

2023-12-14 00:10:42

webPageOperation是webview的初步封装,用来网页填表、操作网页。可操作web.form、web.view、web.view2等浏览器组件。
1

使用方法

首先把webPageOperation.aardio(源码在后面)放到~\lib\godking目录下,然后新建窗口项目:

import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=759;bottom=469)
winform.add(
button={cls="button";text="打开网页";left=220;top=400;right=340;bottom=450;z=2};
button2={cls="button";text="搜索";left=410;top=400;right=530;bottom=450;z=3};
custom={cls="custom";text="自定义控件";left=9;top=13;right=744;bottom=386;ah=1;aw=1;z=1}
)
/*}}*/

import web.view
import console
var wb = web.view(winform.custom)
wb.go("https://www.sogou.com/")
 
winform.button.oncommand = function(id,event){
	import godking.webPageOperation;
	var wpo = godking.webPageOperation(wb);
	
	// 点击"图片"连接
	var s = "#weixinch";
	wpo.bySelector( s ).click();
}
 
winform.button2.oncommand = function(id,event){
	import godking.webPageOperation;
	var wpo = godking.webPageOperation(wb);
	
	// 填写搜索框内容
	var s = "#query";
	wpo.bySelector( s ).setValue("光庆aardio");
		
	// 表单提交
	var tagname = "form"
	wpo.byTagNameS( tagname,1 ).submit();	
}
 
winform.show();
win.loopMessage();

组件源码

//webPageOperation 网页操作
//光庆封装 http://chengxu.online
//V1.1.1
//2023-06-05
//参考文档: https://developer.mozilla.org/zh-CN/docs/Web/API
/*
更新日志
	1.1.1 改进S结尾的操作集合,增加 byIndex() 函数,改进 byChildS() 和 byParent() 函数命名。
	1.1.0 支持 parentNode() 父元素操作集 和 childNodes() 子元素操作集。内置find函数,可寻找指定元素并返回操作集,但速度很慢。
	1.0.0 初步封装
Selector(CSS选择器)参考手册: 
	https://www.runoob.com/cssref/css-selectors.html
	https://m.w3cschool.cn/cssref/css-selectors.html
bySelector(querySelector)使用文档:
	https://www.runoob.com/jsref/met-document-queryselector.html	
	https://m.w3cschool.cn/jsref/met-element-queryselector.html
*/
namespace godking
	// 对象操作类
class_func = class {
    	ctor( webview,funcName,Selector="",index,childNodes="",childIndex){
    	    if index this.index = "[" ++(index-1)++ "]" else this.index = "";
    	    if childIndex this.childIndex = "[" ++(childIndex-1)++ "]" else this.childIndex = "";
    	    var childNodesIndex = childNodes ++ this.childIndex;
    	};
		getProp = function(Prop){
			Prop:="value";
			return webview.eval("document." ++ funcName ++ "('" ++Selector++ "')" ++ this.index ++ childNodesIndex ++ "." ++Prop);
		}
		setProp = function(Prop,v=""){
			Prop:="value";
			if type(v)==type.string v="'" ++v++ "'";
			return webview.doScript("document." ++ funcName ++ "('" ++Selector++ "')" ++ this.index ++ childNodesIndex ++ "." ++Prop++ "=" ++v);
		}		
    	count = function(){
    	    var t;
    		if childNodes {
	    	    t = webview.eval("document." ++ funcName ++ "('" ++Selector++ "')" ++ this.index ++ childNodes);
	    	}else{
	    		t = webview.eval("document." ++ funcName ++ "('" ++Selector++ "')");
	    	}
    	    if type(t) == type.table return ..table.count(t);
    	    else return 0;
    	}
		click = function(){
			return webview.doScript("document." ++ funcName ++ "('" ++Selector++ "')" ++ this.index ++ childNodesIndex ++ ".click()");
		}
		submit = function(){
			return webview.doScript("document." ++ funcName ++ "('" ++Selector++ "')" ++ this.index ++ childNodesIndex ++ ".submit()");
		}
		
		getValue = function() { return this.getProp("value") }; 
		setValue = function(v) { return this.setProp("value",v) }; 

		getText = function() { var t = this.getProp("value"); if t!==null return tostring(t); }; 
		setText = function(v) { return this.setProp("value",tostring(v)) }; 
		
		getChecked = function() { return this.getProp("checked") }; 
		setChecked = function(v=1) { return this.setProp("checked",v?1:0) }; 
		
		getInnerText = function() { return this.getProp("innerText") }; 
		setInnerText = function(v="") { return this.setProp("innerText",v) }; 
		
		getOuterText = function() { return this.getProp("outerText") }; 
		setOuterText = function(v="") { return this.setProp("outerText",v) }; 
		
		getOuterHTML = function() { return this.getProp("outerHTML") }; 
		setOuterHTML = function(v="") { return this.setProp("outerHTML",v) }; 
		
		getInnerHTML = function() { return this.getProp("innerHTML") }; 
		setInnerHTML = function(v="") { return this.setProp("innerHTML",v) }; 
		
		getSelectedItemIndex = function(){
			var idx = this.getProp("selectedIndex"); if idx!==null return idx+1; 
		}
		setSelectedItemByIndex = function(v){
			v:=1; v-=1; return this.setProp("selectedIndex",v)
		}
		getSelectedItemText = function(){
			var idx = this.getProp("selectedIndex"); if idx===null return ; 
			return webview.eval("document." ++ funcName ++ "('" ++Selector++ "')" ++ this.index ++ childNodesIndex ++ ".options.item(" ++idx++ ").text");
		}
		setSelectedItemByText = function(v=""){
			for(i=1;this.count();1){
				if webview.eval("document." ++ funcName ++ "('" ++Selector++ "')" ++ this.index ++ childNodesIndex ++ ".options.item(" ++(i-1)++ ").text")==v {
					return webview.doScript("document." ++ funcName ++ "('"+Selector+"')" ++ this.index ++ childNodesIndex ++ ".selectedIndex=" ++(i-1));
				}
			}
		} 
		getItemText = function(v){
			v:=1; v-=1;
			return webview.eval("document." ++ funcName ++ "('" ++Selector++ "')" ++ this.index ++ childNodesIndex ++ ".options.item(" ++v++ ").text");
		} 
		getItemCount = function(){
			var t = webview.eval("document." ++ funcName ++ "('" ++Selector++ "')" ++ this.index ++ childNodesIndex ++ ".options");
			if type(t)==type.table return ..table.count(t); 
		} 				
		getName = function() { return this.getProp("name") }; 
		getID = function() { return this.getProp("id") }; 
		getHref = function() { return this.getProp("href") }; 
		getSrc = function() { return this.getProp("src") }; 
		getTag = function() { return this.getProp("tagName") }; 
		getType = function() { return this.getProp("type") }; 
		getClass = function() { return this.getProp("className") }; 
		getStyle = function() { return this.getProp("style") }; 

		setHref = function(v) { return this.setProp("href",v) }; 
		setSrc = function(v) { return this.setProp("src",v) }; 
		setTag = function(v) { return this.setProp("tagName",v) }; 
		setType = function(v) { return this.setProp("type",v) }; 
		setClass = function(v) { return this.setProp("className",v) }; 
		setStyle = function(v) { return this.setProp("style",v) }; 
		
		byParent = function(){
			return ..godking.class_func(webview,funcName,Selector,index,childNodes++ this.childIndex ++ ".parentNode",null); 
		}
		byChildS = function(v){
			return ..godking.class_func(webview,funcName,Selector,index,childNodes++ this.childIndex ++ ".childNodes",v:1); 
		}
		focus = function(){
			webview.doScript("document." ++ funcName ++ "('" ++Selector++ "')" ++ this.index ++ childNodesIndex ++ ".focus()");
		}
		byIndex = function(v){
			if childIndex return ..godking.class_func( webview,funcName,Selector,index,childNodes,v:1); 
			else return ..godking.class_func( webview,funcName,Selector,v:1,childNodes,childIndex); 
		}
    }

class webPageOperation {
	ctor(webview){}; 
	// 获取对象函数
    focus = function(){
    	webview.focus();
    }
	getProp = function(prop){
		return webview.eval("document."+prop);
	}
	setProp = function(prop,v){
		if type(v)==type.string v="'"+v+"'";
		return webview.doScript("document."+prop+"="+v);
	}
	hideScrollbar = function(){
		webview.doScript(`document.body.style.overflow='hidden';
		document.body.style.overflowX='hidden';
		document.body.style.overflowY='hidden';
		document.parentWindow.execScript("document.body.style.overflow='hidden';")
		`)
	}
	hideBoder = function(){
		webview.doScript("document.body.style.border='0px none #CCCCCC';")
	}
	addLimit = function(){
		webview.doScript(`document.οncοntextmenu=function(){return false;};
		document.onselectstart=function(){return false;};
		document.onsdragstart=function(){return false;};
		`)
	}
	getTitle = function(){
		return webview.eval("document.title");
	}
	getCookie = function(){
		return webview.eval("document.cookie");
	}	
	setDesignMode = function(v=true){
		webview.doScript("document.designMode='"+(v?"on":"off")+"'")
	}
	back = function(){
		webview.doScript("window.history.back();")
	}
	forward = function(){
		webview.doScript("window.history.forward();")
	}
	find = function(kv){
		var fd = function(clss,fd){
			for(i=1;clss.childNodes().count();1){
				// 检查当前子对象
				var curChd = clss.childNodes(i);
				var is = true;
				for(k,v in kv){
					if curChd.getProp(k)!==v {
						is=false;
						break;
					}
				}
				if is return curChd; 
				// 如果有子子对象,则深入分析当前子对象
				if curChd.childNodes().count() {
					var r = fd(curChd,fd);
					if r return r; 
				}
			}			
		}
		var cls = this.bySelector("html");
		if !cls.childNodes().count() cls = this.bySelector("body");
		if cls.childNodes().count() return fd(cls,fd); 
	}

    // 创建对象操作类实例函数
	byID = function(v){ return ..godking.class_func(webview,"getElementById",v) };
	bySelector = function(v){ return ..godking.class_func(webview,"querySelector",v) };
	byTagNameS = function(v,index){ return ..godking.class_func(webview,"getElementsByTagName",v,index:1) };
	byNameS = function(v,index){ return ..godking.class_func(webview,"getElementsByName",v,index:1) };
	byClassNameS = function(v,index){ return ..godking.class_func(webview,"getElementsByClassName",v,index:1) };
	bySelectorS = function(v,index){ return ..godking.class_func(webview,"querySelectorAll",v,index:1) };

}

/**intellisense()
godking.webPageOperation = 网页操作类
godking.webPageOperation( wb ) = 初始化网页操作类。参数:webform、webview、webview2 对象
godking.webPageOperation() = !godking_webPageOperation.
end intellisense**/

/**intellisense(!godking_webPageOperation.)
byID( "__" ) = 通过ID获取元素对象,创建对象操作集。
bySelector( "__" ) = 通过Selector路径获取元素对象,创建对象操作集。
byTagNameS( "__", 1 ) = 通过TagName获取元素对象集合,创建对象操作集。\n参数1:tagName;参数2:对象索引(从1开始)
byNameS( "__", 1 ) = 通过name获取元素对象集合,创建对象操作集。\n参数1:name;参数2:对象索引(从1开始)
byClassNameS( "__", 1 ) = 通过ClassName获取元素对象集合,创建对象操作集。\n参数1:ClassName;参数2:对象索引(从1开始)
bySelectorS( "__", 1 ) = 通过Selector路径获取元素对象集合,创建对象操作集。\n参数1:Selector路径;参数2:对象索引(从1开始)

byID() = !godking_webPageOperation_class.
bySelector() = !godking_webPageOperation_class.
byTagNameS() = !godking_webPageOperation_class.
byNameS() = !godking_webPageOperation_class.
byClassNameS() = !godking_webPageOperation_class.
bySelectorS() = !godking_webPageOperation_class.

getProp( "__" ) = 获取属性值。参数:属性名
setProp( "__", ) = 设置属性值。参数:属性名、属性值
hideScrollbar() = 隐藏滚动条
hideBoder() = 隐藏边框
addLimit() = 添加鼠标右键菜单、选择文本、拖拽的限制。
getTitle() = 获取标题
getCookie() = 获取cookie
setDesignMode( true ) = 设置设计模式(编辑模式)
back() = 后退
forward() = 前进
focus() = 网页组件获取输入焦点
end intellisense**/

/**intellisense(!godking_webPageOperation_class.)
getProp( "__" ) = 获取属性值。参数:属性名
setProp( "__", ) = 设置属性值。参数:属性名、属性值

count() = 元素总数量。by开头S结束的函数,可以通过该函数获取总元素数量。
click() = 点击元素
submit() = 提交表单
focus() = 网页元素获取输入焦点

getValue() = 取value值
setValue( "__" ) = 置value值

getText() = 取value值,自动tostring转文本。
setText( "__" ) = 置value值,自动tostring转文本。

getChecked() = 取选中状态
setChecked( true ) = 置选中状态

getInnerText() = 取innerText
setInnerText( "__" ) = 置innerText

getOuterText() = 取outerText
setOuterText( "__" ) = 置outerText

getOuterHTML() = 取outerHTML
setOuterHTML( "__" ) = 置outerHTML

getInnerHTML() = 取innerHTML
setInnerHTML( "__" ) = 置innerHTML

byChildS( 1 ) = 创建子元素对象操作集。参数:子元素索引。
byChildS() = !godking_webPageOperation_class.
byParent() = 创建父元素对象操作集。\n!godking_webPageOperation_class.
byIndex( 1 ) = 创建当前元素集(S结尾的)的指定索引的元素的操作集。参数:元素索引。
byIndex() = !godking_webPageOperation_class.

getSelectedItemIndex() = 取选择框当前选中项目索引
setSelectedItemByIndex( __ ) = 置选择框当前选中项目索引
getSelectedItemText() = 取选择框当前选中项目文本
setSelectedItemByText( "__" ) = 置选择框当前选中项目文本
getItemText( __ ) = 取指定项目文本。参数:项目索引(从1开始)
getItemCount() = 取项目数量
getName() = 取name
getID() = 取id
getHref() = 取href
getSrc() = 取src
getTag() = 取tagName
getType() = 取type
getClass() = 取class
getStyle() = 取style

setHref( "__" ) = 置href
setSrc( "__" ) = 置src
setTag( "__" ) = 置tagName
setType( "__" ) = 置type
setClass( "__" ) = 置class
setStyle( "__" ) = 置style

end intellisense**/

参考

https://blog.csdn.net/sdlgq/article/details/131011599

文章来源:https://blog.csdn.net/lilongsy/article/details/134962946
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。