Skip to content

[en]trashscript_lib

devel01 edited this page Dec 7, 2024 · 5 revisions

Global

sleep

sleep(milliseconds);

The time interval for which execution is to be suspended, in milliseconds.


timestamp

timestamp()

The timestamp() function returns a datetime value based on a date or datetime value, identical to new Date().getTime()




DOM

DOM manipulation can only read content from the webpage and does not provide any functionality for making modifications.

css selectors

//query elements
elements = $(css_selector);

//query all div elements
elements = $("div")

//query all div elememts under body
elements = $("div","body")

Members:

length

elements = $("div")
elements.length; //the length of div elements

eq

elements = $("div")
elements.eq(0) //get an element from the elements by index

each

/*
 * the each method is an iterative method.
 * It calls a provided callbackFn function once for each element in an array in ascending-index order.  
*/
elements = $("div");
result   = elements.each(function(i,element){
    //the result is undefined
});

result   = elements.each(function(i,element){
    //break the loop, the result is true
    return true;
});

css

<span id="content" style="color:red"></span>
//Get the corresponding computed style from an element.
color = $("#content").css("color")

attr

<img src="img.png"/>
//Get the corresponding attribute from an element.
src = $("img").attr("src");

prop

get property from a DOM object

$("div").prop("dom_property")
$("div").prop(function(name,value){
    if(name === "key"){
        return value;
    }
})

data

get data from a DOM dataset object

$("div").data("dataset_property")
$("div").data(function(name,value){
    if(name === "key"){
        return value;
    }
})

click

Click all elements

$("button").click();

text

<span id="aa">text</span>
//Get the rendered text content of a element and its descendant.
src = $("#aa").text();

html

<span id="aa"><span>text</span></span>
//The html method returns a serialization of the nested child DOM elements within the element.
src = $("#aa").html();

parent

<body>
    <span></span>
</body>
//Get the parent element of the span.
element = $("span").parent()

children

<body>
    <span>1</span>
    <span>2</span>
    <span>3</span>
</body>
elements = $("body").children();
elements.each(function(i,element){
     console.log(element.text());
});

siblings

<body>
    <span id="a">1</span>
    <span id="b">2</span>
    <span id="c">3</span>
</body>
siblings = $("#a").siblings();
siblings.each(function(i,element){
     console.log(element.text());
});

next

<body>
    <span id="a">1</span>
    <span id="b">2</span>
    <span id="c">3</span>
</body>
$("#a").next(); //b

prev

<body>
    <span id="a">1</span>
    <span id="b">2</span>
    <span id="c">3</span>
</body>
$("#b").prev(); //a

val

Get the value of and element.

<input value="123" />
$("input").val(); //The value is "123"

HTTP

Execute HTTP requests.

http = HTTP(method,url,username = "",password = "")
http.options(opts = {})
http.request(post_data = null)

Options:

{
    //key: value, HTTP request headers
    headers: {
        referer:"http://xxxx"
    } 
}

HTTP.get

http = HTTP.get(url,options = {})

HTTP.post

http = HTTP.get(url,post_data,options = {})

HTTP_Intermedia

/*
 * The default XMLHttpRequest does not support cross-origin requests. 
 * However, you can override the HTTP_Intermedia function to forward the request to another program, such as background.js. 
 * Finally, you can execute the resolve_callback to submit the results. 
*/
TrashScript.perperties.HTTP_Intermedia = function(option,resolve_callback)
{
    //option.method;  request method: GET, POST....
    //option.url;     request url
    //option.data;    post data (string or object)
    //option.timeout, {timeout:5000}
    //option.headers, {referer:"xxxx"}
    
    resolve_callback({status:200,status_text:"text",headers:{},body:"response content"})
}

Members:

{
    method: String,
    url: String,
    request_headers: Object,
    options: function(options_object),
    request: function(post_data),
}

Result:

{
    status: Number,         //status code
    status_text: String,    //status text
    body: String,           //response body
    text: String,           //response text
    XML: String,            //formatted response xml
    JSON: String,           //formatted response JSON, or null

    header: function(name)  //response headers, if name is undefined, it returns all headers
}