77 lines
2.3 KiB
Java
77 lines
2.3 KiB
Java
package com.yutou.qqbot.utlis;
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import org.openqa.selenium.Cookie;
|
|
import org.openqa.selenium.WebDriver;
|
|
import org.openqa.selenium.chrome.ChromeDriver;
|
|
import org.openqa.selenium.chrome.ChromeOptions;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
public class WebClient {
|
|
private static WebClient client;
|
|
|
|
|
|
public static WebClient getInstance() {
|
|
if (client == null) {
|
|
client = new WebClient();
|
|
}
|
|
return client;
|
|
}
|
|
|
|
public WebDriver getWebDriver() {
|
|
return new ChromeDriver(getOptions());
|
|
}
|
|
|
|
private WebClient() {
|
|
System.setProperty("webdriver.chrome.driver",
|
|
ConfigTools.load(ConfigTools.CONFIG, "chrome", String.class));
|
|
// java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF);
|
|
}
|
|
|
|
|
|
public static List<Cookie> loadCookie(JSONArray array) {
|
|
List<Cookie> list = new ArrayList<>();
|
|
|
|
for (Object o : array) {
|
|
JSONObject json = (JSONObject) o;
|
|
boolean containsDate = json.containsKey("expirationDate");
|
|
long t = 0;
|
|
if (containsDate) {
|
|
t = Long.parseLong(json.getString("expirationDate").replace(".", "")) / 1000;
|
|
}
|
|
|
|
Cookie cookie = new Cookie(json.getString("name"),
|
|
json.getString("value"),
|
|
json.getString("domain"),
|
|
json.getString("path"),
|
|
containsDate ? new Date(t) : new Date(),
|
|
json.getBoolean("secure"),
|
|
json.getBoolean("httpOnly")
|
|
|
|
);
|
|
list.add(cookie);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public static ChromeOptions getOptions() {
|
|
ChromeOptions options = new ChromeOptions();
|
|
// options.addArguments("--disable-gpu");
|
|
// options.addArguments("blink-settings=imagesEnabled=false");
|
|
// options.addArguments("--headless");
|
|
// options.addArguments("--no-sandbox");
|
|
// options.addArguments("--incognito");
|
|
options.addArguments("--disable-plugins");
|
|
options.addArguments("--lang=zh-CN");
|
|
|
|
return options;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
}
|
|
}
|