From d5e9ec46d4dadc5f5491e08435382c6ea599d9cc Mon Sep 17 00:00:00 2001 From: rnet Date: Mon, 15 Apr 2024 23:58:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=201.=20=E5=8E=BB=E9=99=A4=E5=8D=95?= =?UTF-8?q?=E4=BE=8B=E5=88=9B=E5=BB=BA=E5=AE=9E=E4=BE=8B=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=EF=BC=9B2.=20=E5=A2=9E=E5=8A=A0Event=E3=80=81XPathException?= =?UTF-8?q?=E3=80=81prompt=E7=9A=84=E6=A3=80=E6=B5=8B=E5=A4=84=E7=90=86?= =?UTF-8?q?=EF=BC=9B3.=20=E5=85=B6=E5=AE=83=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- browser/chrome/RTCPeerConnection.js | 31 ++++---- browser/chrome/chrome.js | 70 ++++++++--------- browser/chrome/ctorRegistry.js | 19 ++--- browser/chrome/document-element.js | 24 +++--- browser/chrome/document.js | 7 +- browser/chrome/index.js | 28 +++---- browser/chrome/indexedDB.js | 25 +++--- browser/chrome/location.js | 19 ++--- browser/chrome/navigation.js | 39 +++++----- browser/chrome/navigator.js | 93 ++++++++++++----------- browser/chrome/styleMedia.js | 11 +-- browser/chrome/visualViewport.js | 27 +++---- browser/chrome/webkitRequestFileSystem.js | 19 ++--- browser/chrome/window.js | 35 +++++---- browser/index.js | 10 +-- example/use-local/index.js | 2 +- example/use-proxy/index.js | 2 +- example/use-remote/index.js | 20 ++++- package.json | 9 ++- utils/jsdom.js | 4 +- 20 files changed, 265 insertions(+), 229 deletions(-) diff --git a/browser/chrome/RTCPeerConnection.js b/browser/chrome/RTCPeerConnection.js index efe3d58..75a5c82 100644 --- a/browser/chrome/RTCPeerConnection.js +++ b/browser/chrome/RTCPeerConnection.js @@ -1,19 +1,20 @@ -const sdenv = require('sdenv-extend').sdenv(); -const window = sdenv.memory.sdWindow; +module.exports = (sdenv) => { + const window = sdenv.memory.sdWindow; -function RTCPeerConnection() { - if (!(this instanceof RTCPeerConnection)) { - throw new TypeError("Uncaught TypeError: Failed to construct 'RTCPeerConnection': Please use the 'new' operator, this DOM object constructor cannot be called as a function."); + function RTCPeerConnection() { + if (!(this instanceof RTCPeerConnection)) { + throw new TypeError("Uncaught TypeError: Failed to construct 'RTCPeerConnection': Please use the 'new' operator, this DOM object constructor cannot be called as a function."); + } + this.createDataChannel = function(...params) { + // window.console.log(`【RTCPeerConnection RTCPeerConnection】调用,参数:${params}`) + } + this.createOffer = function(...params) { + // window.console.log(`【RTCPeerConnection createOffer】调用,参数:${params}`) + } } - this.createDataChannel = function(...params) { - // window.console.log(`【RTCPeerConnection RTCPeerConnection】调用,参数:${params}`) - } - this.createOffer = function(...params) { - // window.console.log(`【RTCPeerConnection createOffer】调用,参数:${params}`) - } -} -sdenv.tools.setNativeFuncName(RTCPeerConnection, 'RTCPeerConnection'); -sdenv.tools.setNativeObjName(RTCPeerConnection.prototype, 'RTCPeerConnection'); + sdenv.tools.setNativeFuncName(RTCPeerConnection, 'RTCPeerConnection'); + sdenv.tools.setNativeObjName(RTCPeerConnection.prototype, 'RTCPeerConnection'); -window.RTCPeerConnection = RTCPeerConnection; + window.RTCPeerConnection = RTCPeerConnection; +} diff --git a/browser/chrome/chrome.js b/browser/chrome/chrome.js index 95ec3a6..41d5cb9 100644 --- a/browser/chrome/chrome.js +++ b/browser/chrome/chrome.js @@ -1,39 +1,39 @@ -const sdenv = require('sdenv-extend').sdenv(); - -sdenv.memory.sdWindow.chrome = { - app: { - isInstalled: false, - InstallState: { - DISABLED: "disabled", - INSTALLED: "installed", - NOT_INSTALLED: "not_installed", +module.exports = (sdenv) => { + sdenv.memory.sdWindow.chrome = { + app: { + isInstalled: false, + InstallState: { + DISABLED: "disabled", + INSTALLED: "installed", + NOT_INSTALLED: "not_installed", + }, + RunningState: { + CANNOT_RUN: "cannot_run", + READY_TO_RUN: "ready_to_run", + RUNNING: "running", + }, + getDetails: function () {}, + getIsInstalled: function() {}, + installState: function() {}, + runningState: function() {}, }, - RunningState: { - CANNOT_RUN: "cannot_run", - READY_TO_RUN: "ready_to_run", - RUNNING: "running", - }, - getDetails: function () {}, - getIsInstalled: function() {}, - installState: function() {}, - runningState: function() {}, - }, - csi: function() {}, - loadTimes: function() { - return { - "requestTime": 1700779741.985, - "startLoadTime": 1700779741.985, - "commitLoadTime": 1700779742.021, - "finishDocumentLoadTime": 0, - "finishLoadTime": 0, - "firstPaintTime": 0, - "firstPaintAfterLoadTime": 0, - "navigationType": "Reload", - "wasFetchedViaSpdy": false, - "wasNpnNegotiated": true, - "npnNegotiatedProtocol": "http/1.1", - "wasAlternateProtocolAvailable": false, - "connectionInfo": "http/1.1" + csi: function() {}, + loadTimes: function() { + return { + "requestTime": 1700779741.985, + "startLoadTime": 1700779741.985, + "commitLoadTime": 1700779742.021, + "finishDocumentLoadTime": 0, + "finishLoadTime": 0, + "firstPaintTime": 0, + "firstPaintAfterLoadTime": 0, + "navigationType": "Reload", + "wasFetchedViaSpdy": false, + "wasNpnNegotiated": true, + "npnNegotiatedProtocol": "http/1.1", + "wasAlternateProtocolAvailable": false, + "connectionInfo": "http/1.1" + } } } } diff --git a/browser/chrome/ctorRegistry.js b/browser/chrome/ctorRegistry.js index 3da437a..db7fdce 100644 --- a/browser/chrome/ctorRegistry.js +++ b/browser/chrome/ctorRegistry.js @@ -1,12 +1,13 @@ const logger = require('@utils/logger'); const utils = require('sdenv-jsdom/lib/jsdom/living/generated/utils.js'); -const sdenv = require('sdenv-extend').sdenv(); -const window = sdenv.memory.sdWindow; -const ctorRegistry = window[utils.ctorRegistrySymbol] -window[utils.ctorRegistrySymbol] = new window.Proxy(ctorRegistry, { - get(target, propKey, receiver) { - logger.trace('proxy ctorRegistry get', propKey); - return window.Reflect.get(target, propKey, receiver); - } -}) +module.exports = (sdenv) => { + const window = sdenv.memory.sdWindow; + const ctorRegistry = window[utils.ctorRegistrySymbol] + window[utils.ctorRegistrySymbol] = new window.Proxy(ctorRegistry, { + get(target, propKey, receiver) { + logger.trace('proxy ctorRegistry get', propKey); + return window.Reflect.get(target, propKey, receiver); + } + }) +} diff --git a/browser/chrome/document-element.js b/browser/chrome/document-element.js index aa63fdb..a82a1f3 100644 --- a/browser/chrome/document-element.js +++ b/browser/chrome/document-element.js @@ -1,16 +1,16 @@ const utils = require('sdenv-jsdom/lib/jsdom/living/generated/utils.js'); -const sdenv = require('sdenv-extend').sdenv(); -const document = sdenv.memory.sdWindow.document; - -const getElementsByTagNameOri = document.getElementsByTagName; -document.getElementsByTagName = (...params) => { - var findArr = getElementsByTagNameOri.apply(document, params); - if (params[0] === 'script' && document.readyState === 'loading' && document[utils.implSymbol]._currentScript) { - // dom动态解析的临时解决方案,看后续是否会产生其它问题 - const findIdx = [...findArr].indexOf(document[utils.implSymbol]._currentScript[utils.wrapperSymbol]); - if (findIdx > -1) { - return [...findArr].slice(0, findIdx + 1); +module.exports = (sdenv) => { + const document = sdenv.memory.sdWindow.document; + const getElementsByTagNameOri = document.getElementsByTagName; + document.getElementsByTagName = (...params) => { + var findArr = getElementsByTagNameOri.apply(document, params); + if (params[0] === 'script' && document.readyState === 'loading' && document[utils.implSymbol]._currentScript) { + // dom动态解析的临时解决方案,看后续是否会产生其它问题 + const findIdx = [...findArr].indexOf(document[utils.implSymbol]._currentScript[utils.wrapperSymbol]); + if (findIdx > -1) { + return [...findArr].slice(0, findIdx + 1); + } } + return findArr; } - return findArr; } diff --git a/browser/chrome/document.js b/browser/chrome/document.js index 1461adf..d48e311 100644 --- a/browser/chrome/document.js +++ b/browser/chrome/document.js @@ -1,5 +1,6 @@ const getDocumentAll = require('@/build/Release/documentAll').getDocumentAll; -const sdenv = require('sdenv-extend').sdenv(); -const window = sdenv.memory.sdWindow; +module.exports = (sdenv) => { + const window = sdenv.memory.sdWindow; -window.document.all = getDocumentAll({ length: 3 }); + window.document.all = getDocumentAll({ length: 3 }); +} diff --git a/browser/chrome/index.js b/browser/chrome/index.js index feaf740..e737e74 100644 --- a/browser/chrome/index.js +++ b/browser/chrome/index.js @@ -1,13 +1,15 @@ -require('./window'); -require('./document'); -require('./navigation'); -require('./navigator'); -require('./chrome'); -require('./visualViewport'); -require('./styleMedia'); -// require('./webkitRequestFileSystem'); -require('./ctorRegistry'); -require('./location'); -require('./indexedDB'); -require('./RTCPeerConnection'); -require('./document-element'); +module.exports = (sdenv) => { + require('./window')(sdenv); + require('./document')(sdenv); + require('./navigation')(sdenv); + require('./navigator')(sdenv); + require('./chrome')(sdenv); + require('./visualViewport')(sdenv); + require('./styleMedia')(sdenv); + require('./webkitRequestFileSystem')(sdenv); + require('./ctorRegistry')(sdenv); + require('./location')(sdenv); + require('./indexedDB')(sdenv); + require('./RTCPeerConnection')(sdenv); + require('./document-element')(sdenv); +} diff --git a/browser/chrome/indexedDB.js b/browser/chrome/indexedDB.js index 81c3630..af5df36 100644 --- a/browser/chrome/indexedDB.js +++ b/browser/chrome/indexedDB.js @@ -1,16 +1,17 @@ -const sdenv = require('sdenv-extend').sdenv(); -const window = sdenv.memory.sdWindow; +module.exports = (sdenv) => { + const window = sdenv.memory.sdWindow; -const IDBFactory = function IDBFactory() { - throw new TypeError("Illegal constructor"); -} + const IDBFactory = function IDBFactory() { + throw new TypeError("Illegal constructor"); + } -const indexedDB = { - __proto__: IDBFactory.prototype -}; + const indexedDB = { + __proto__: IDBFactory.prototype + }; -sdenv.tools.setNativeFuncName(IDBFactory, 'IDBFactory'); -sdenv.tools.setNativeObjName(indexedDB, 'IDBFactory'); + sdenv.tools.setNativeFuncName(IDBFactory, 'IDBFactory'); + sdenv.tools.setNativeObjName(indexedDB, 'IDBFactory'); -window.IDBFactory = IDBFactory; -window.indexedDB = indexedDB; + window.IDBFactory = IDBFactory; + window.indexedDB = indexedDB; +} diff --git a/browser/chrome/location.js b/browser/chrome/location.js index 3f2bfc3..f178d17 100644 --- a/browser/chrome/location.js +++ b/browser/chrome/location.js @@ -1,10 +1,11 @@ -const sdenv = require('sdenv-extend').sdenv(); -const window = sdenv.memory.sdWindow; +module.exports = (sdenv) => { + const window = sdenv.memory.sdWindow; -Object.defineProperty(window.location, 'replace', { - ...Object.getOwnPropertyDescriptor(window.location, 'replace'), - writable: false, - value: function(url) { - sdenv.tools.exit({ url }); - } -}); + Object.defineProperty(window.location, 'replace', { + ...Object.getOwnPropertyDescriptor(window.location, 'replace'), + writable: false, + value: function(url) { + sdenv.tools.exit({ url }); + } + }); +} diff --git a/browser/chrome/navigation.js b/browser/chrome/navigation.js index f74d09f..b9e6d6a 100644 --- a/browser/chrome/navigation.js +++ b/browser/chrome/navigation.js @@ -1,20 +1,21 @@ -const sdenv = require('sdenv-extend').sdenv(); -const window = sdenv.memory.sdWindow; +module.exports = (sdenv) => { + const window = sdenv.memory.sdWindow; -[window.Navigation, window.navigation] = sdenv.tools.getNativeProto('Navigation', 'navigation', { - canGoBack: false, - canGoForward: false, - oncurrententrychange: null, - onnavigate: null, - onnavigateerror: null, - onnavigatesuccess: null, - transition: null, - currentEntry: { - id: 'c72e7c89-2c22-47b6-86b8-e83db973ad22', - index: 1, - key: 'd6cc1590-0028-48e9-b6e7-b489d28d8481', - ondispose: null, - sameDocument: true, - url: 'http://example.com', - } -}); + [window.Navigation, window.navigation] = sdenv.tools.getNativeProto('Navigation', 'navigation', { + canGoBack: false, + canGoForward: false, + oncurrententrychange: null, + onnavigate: null, + onnavigateerror: null, + onnavigatesuccess: null, + transition: null, + currentEntry: { + id: 'c72e7c89-2c22-47b6-86b8-e83db973ad22', + index: 1, + key: 'd6cc1590-0028-48e9-b6e7-b489d28d8481', + ondispose: null, + sameDocument: true, + url: 'http://example.com', + } + }); +} diff --git a/browser/chrome/navigator.js b/browser/chrome/navigator.js index ba18f84..af51f14 100644 --- a/browser/chrome/navigator.js +++ b/browser/chrome/navigator.js @@ -1,48 +1,49 @@ -const sdenv = require('sdenv-extend').sdenv(); -const window = sdenv.memory.sdWindow; +module.exports = (sdenv) => { + const window = sdenv.memory.sdWindow; -const DeprecatedStorageQuota = function DeprecatedStorageQuota() { - throw new TypeError("Illegal constructor"); -}; -DeprecatedStorageQuota.prototype = { - queryUsageAndQuota() { - }, - requestQuota() { - }, -}; -sdenv.tools.setObjName(DeprecatedStorageQuota.prototype, "DeprecatedStorageQuota"); -const NetworkInformation = function NetworkInformation() { - throw new TypeError("Illegal constructor"); -} -sdenv.tools.setObjName(NetworkInformation.prototype, "NetworkInformation"); -class NavigatorCustomize { - get webkitPersistentStorage() { - return { __proto__: DeprecatedStorageQuota.prototype }; - } - get connection() { - return { - __proto__: NetworkInformation.prototype, - downlink: 3.85, - effectiveType: "4g", - onchange: null, - rtt: 100, - saveData: false, - }; - } - get userAgent() { - return 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36'; - } - get appVersion() { - return '5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36' + const DeprecatedStorageQuota = function DeprecatedStorageQuota() { + throw new TypeError("Illegal constructor"); + }; + DeprecatedStorageQuota.prototype = { + queryUsageAndQuota() { + }, + requestQuota() { + }, + }; + sdenv.tools.setObjName(DeprecatedStorageQuota.prototype, "DeprecatedStorageQuota"); + const NetworkInformation = function NetworkInformation() { + throw new TypeError("Illegal constructor"); } - get platform() { - return 'MacIntel'; - } - get vendor() { - return "Google Inc."; - } -}; -sdenv.tools.mixin(window.navigator, NavigatorCustomize.prototype, ['userAgent', 'platform', 'appVersion', 'vendor']); -Object.keys(window.navigator.__proto__).forEach(name => { - sdenv.tools.setFuncNative(Object.getOwnPropertyDescriptor(window.navigator.__proto__, name)?.get, 'get'); -}) + sdenv.tools.setObjName(NetworkInformation.prototype, "NetworkInformation"); + class NavigatorCustomize { + get webkitPersistentStorage() { + return { __proto__: DeprecatedStorageQuota.prototype }; + } + get connection() { + return { + __proto__: NetworkInformation.prototype, + downlink: 3.85, + effectiveType: "4g", + onchange: null, + rtt: 100, + saveData: false, + }; + } + get userAgent() { + return 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36'; + } + get appVersion() { + return '5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36' + } + get platform() { + return 'MacIntel'; + } + get vendor() { + return "Google Inc."; + } + }; + sdenv.tools.mixin(window.navigator, NavigatorCustomize.prototype, ['userAgent', 'platform', 'appVersion', 'vendor']); + Object.keys(window.navigator.__proto__).forEach(name => { + sdenv.tools.setFuncNative(Object.getOwnPropertyDescriptor(window.navigator.__proto__, name)?.get, 'get'); + }) +} diff --git a/browser/chrome/styleMedia.js b/browser/chrome/styleMedia.js index 716e8eb..4c48be3 100644 --- a/browser/chrome/styleMedia.js +++ b/browser/chrome/styleMedia.js @@ -1,6 +1,7 @@ -const sdenv = require('sdenv-extend').sdenv(); -const window = sdenv.memory.sdWindow; +module.exports = (sdenv) => { + const window = sdenv.memory.sdWindow; -window.styleMedia = sdenv.tools.getNativeProto('StyleMedia', 'styleMedia', { - type: 'screen' -})[1]; + window.styleMedia = sdenv.tools.getNativeProto('StyleMedia', 'styleMedia', { + type: 'screen' + })[1]; +} diff --git a/browser/chrome/visualViewport.js b/browser/chrome/visualViewport.js index 4e5b703..d674f0d 100644 --- a/browser/chrome/visualViewport.js +++ b/browser/chrome/visualViewport.js @@ -1,14 +1,15 @@ -const sdenv = require('sdenv-extend').sdenv(); -const window = sdenv.memory.sdWindow; +module.exports = (sdenv) => { + const window = sdenv.memory.sdWindow; -[window.VisualViewport, window.visualViewport] = sdenv.tools.getNativeProto('VisualViewport', 'visualViewport', { - height: 904, - offsetLeft: 0, - offsetTop: 0, - onresize: null, - onscroll: null, - pageLeft: 0, - pageTop: 0, - scale: 1, - width: 1066, -}); + [window.VisualViewport, window.visualViewport] = sdenv.tools.getNativeProto('VisualViewport', 'visualViewport', { + height: 904, + offsetLeft: 0, + offsetTop: 0, + onresize: null, + onscroll: null, + pageLeft: 0, + pageTop: 0, + scale: 1, + width: 1066, + }); +} diff --git a/browser/chrome/webkitRequestFileSystem.js b/browser/chrome/webkitRequestFileSystem.js index 326a611..c65cac6 100644 --- a/browser/chrome/webkitRequestFileSystem.js +++ b/browser/chrome/webkitRequestFileSystem.js @@ -1,10 +1,11 @@ -const sdenv = require('sdenv-extend').sdenv(); -const window = sdenv.memory.sdWindow; +module.exports = (sdenv) => { + const window = sdenv.memory.sdWindow; -const webkitRequestFileSystem = function webkitRequestFileSystem(type, size, successCallback, errorCallback) { - if (typeof successCallback === 'function') { - window.setTimeout(successCallback, 0); - } -}; -sdenv.tools.setNativeFuncName(webkitRequestFileSystem, 'webkitRequestFileSystem') -window.webkitRequestFileSystem = webkitRequestFileSystem; + const webkitRequestFileSystem = function webkitRequestFileSystem(type, size, successCallback, errorCallback) { + if (typeof successCallback === 'function') { + window.setTimeout(successCallback, 0); + } + }; + sdenv.tools.setNativeFuncName(webkitRequestFileSystem, 'webkitRequestFileSystem') + window.webkitRequestFileSystem = webkitRequestFileSystem; +} diff --git a/browser/chrome/window.js b/browser/chrome/window.js index ad7aad1..35acdd6 100644 --- a/browser/chrome/window.js +++ b/browser/chrome/window.js @@ -1,16 +1,25 @@ const logger = require('@utils/logger'); -const sdenv = require('sdenv-extend').sdenv(); -const window = sdenv.memory.sdWindow; +module.exports = (sdenv) => { + const window = sdenv.memory.sdWindow; -window.fetch = function fetch() {}; -sdenv.tools.setFuncNative(window.fetch); -window.Request = function Request() {}; -sdenv.tools.setFuncNative(window.Request); -window.closed = false; -window.opener = null; -window.clientInformation = window.navigator; -window.isSecureContext = false; -window.open = function(url) { - sdenv.tools.exit({ url }); + window.fetch = function fetch() {}; + sdenv.tools.setFuncNative(window.fetch); + window.Request = function Request() {}; + sdenv.tools.setFuncNative(window.Request); + window.closed = false; + window.opener = null; + window.clientInformation = window.navigator; + window.isSecureContext = false; + window.open = function(url) { + sdenv.tools.exit({ url }); + } + sdenv.tools.setFuncNative(window.Event, 'Event'); + delete window.XPathException; + sdenv.tools.setFuncNative(window.prompt, 'prompt'); + // Object.assign(window, { + // innerWidth: 326, + // innerHeight: 992, + // outerWidth: 1728, + // outerHeight: 1079, + // }); } -// window.console = logger; diff --git a/browser/index.js b/browser/index.js index cc8c42f..a0ed2e7 100644 --- a/browser/index.js +++ b/browser/index.js @@ -6,11 +6,7 @@ require('module-alias')(path.dirname(paths.package)); const SdenvExtend = require('sdenv-extend'); module.exports = (win, type = 'chrome') => { - new SdenvExtend({ - memory: { - SdenvExtend, - } - }, win); - require(`@/browser/${type}`); - return new SdenvExtend(); + win.sdenv = new SdenvExtend({ }, win); + require(`@/browser/${type}`)(win.sdenv); + return win.sdenv; } diff --git a/example/use-local/index.js b/example/use-local/index.js index 2129daf..c71e4af 100644 --- a/example/use-local/index.js +++ b/example/use-local/index.js @@ -25,7 +25,7 @@ function initBrowser(window, cookieJar) { window.onbeforeunload = async (url) => { const cookies = cookieJar.getCookieStringSync(baseUrl); logger.debug('生成cookie:', cookies); - process.exit(); + window.close(); } browser(window, 'chrome'); } diff --git a/example/use-proxy/index.js b/example/use-proxy/index.js index 834c059..b6ec7c3 100644 --- a/example/use-proxy/index.js +++ b/example/use-proxy/index.js @@ -17,7 +17,7 @@ async function loadPages() { if (cookies) { logger.debug('cookieJar:', cookies); } - process.exit(); + dom.window.close(); } } loadPages() diff --git a/example/use-remote/index.js b/example/use-remote/index.js index de9f63e..3fa8b84 100644 --- a/example/use-remote/index.js +++ b/example/use-remote/index.js @@ -5,7 +5,20 @@ const { jsdomFromUrl } = require('../../utils/jsdom'); const baseUrl = "https://wcjs.sbj.cnipa.gov.cn" -async function loadPages() { +async function loadPagesSecond(cookieJar) { + const [jsdomer, ..._] = jsdomFromUrl({ + userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36', + }, cookieJar); + const dom = await jsdomer(`${baseUrl}/sgtmi`); + if (dom.window.document.title === '商标网上检索') { + logger.info(`cookie验证通过,存在document.title,且值为:${dom.window.document.title}`); + } else { + logger.error('cookie验证不通过!'); + } + dom.window.close(); +} + +async function loadPagesFirst() { const [jsdomer, cookieJar] = jsdomFromUrl({ userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36', }); @@ -14,9 +27,10 @@ async function loadPages() { window.onbeforeunload = async (url) => { const cookies = cookieJar.getCookieStringSync(baseUrl); logger.debug('生成cookie:', cookies); - process.exit(); + await loadPagesSecond(cookieJar) + window.close(); } browser(window, 'chrome'); } -loadPages() +loadPagesFirst() diff --git a/package.json b/package.json index 08afe27..7e64b09 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,6 @@ "name": "sdenv", "version": "0.1.8", "description": "补环境框架", - "main": "main.js", "directories": { "test": "test" }, @@ -13,6 +12,12 @@ "release": "release-it" }, "logLevel": "debug", + "repository": { + "type": "git", + "url": "https://github.com/pysunday/sdenv" + }, + "homepage": "https://github.com/pysunday/sdenv#readme", + "bugs": "https://github.com/pysunday/sdenv/issues", "author": "pysunday", "license": "ISC", "dependencies": { @@ -23,7 +28,7 @@ "log4js": "^6.9.1", "module-alias": "^2.2.3", "node-addon-api": "^7.0.0", - "sdenv-extend": "^1.2.0", + "sdenv-extend": "^1.3.0", "sdenv-jsdom": "^1.1.0" }, "devDependencies": { diff --git a/utils/jsdom.js b/utils/jsdom.js index e055684..424cbc5 100644 --- a/utils/jsdom.js +++ b/utils/jsdom.js @@ -2,7 +2,7 @@ const jsdom = require('sdenv-jsdom'); const logger = require('./logger'); const { JSDOM, CookieJar } = jsdom; -exports.jsdomFromUrl = (config, ua) => { +exports.jsdomFromUrl = (config, cookieJar) => { const resourceLoader = new jsdom.ResourceLoader({ strictSSL: false, ...config, @@ -13,7 +13,7 @@ exports.jsdomFromUrl = (config, ua) => { warn: logger.warn.bind(logger), error: logger.error.bind(logger), }); - const cookieJar = new CookieJar() + if (!cookieJar) cookieJar = new CookieJar(); const options = { pretendToBeVisual: true, runScripts: "dangerously",