feat: 1. 去除单例创建实例方式;2. 增加Event、XPathException、prompt的检测处理;3. 其它代码优化

This commit is contained in:
rnet 2024-04-15 23:58:17 +08:00
parent 49a4a2bb45
commit d5e9ec46d4
20 changed files with 268 additions and 232 deletions

View File

@ -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.");
}
this.createDataChannel = function(...params) {
// window.console.log(`【RTCPeerConnection RTCPeerConnection】调用参数${params}`)
}
this.createOffer = function(...params) {
// window.console.log(`【RTCPeerConnection createOffer】调用参数${params}`)
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}`)
}
}
sdenv.tools.setNativeFuncName(RTCPeerConnection, 'RTCPeerConnection');
sdenv.tools.setNativeObjName(RTCPeerConnection.prototype, 'RTCPeerConnection');
window.RTCPeerConnection = RTCPeerConnection;
}
sdenv.tools.setNativeFuncName(RTCPeerConnection, 'RTCPeerConnection');
sdenv.tools.setNativeObjName(RTCPeerConnection.prototype, 'RTCPeerConnection');
window.RTCPeerConnection = RTCPeerConnection;

View File

@ -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"
}
}
}
}

View File

@ -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);
}
})
}

View File

@ -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;
}

View File

@ -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 });
}

View File

@ -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);
}

View File

@ -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
};
sdenv.tools.setNativeFuncName(IDBFactory, 'IDBFactory');
sdenv.tools.setNativeObjName(indexedDB, 'IDBFactory');
window.IDBFactory = IDBFactory;
window.indexedDB = indexedDB;
}
const indexedDB = {
__proto__: IDBFactory.prototype
};
sdenv.tools.setNativeFuncName(IDBFactory, 'IDBFactory');
sdenv.tools.setNativeObjName(indexedDB, 'IDBFactory');
window.IDBFactory = IDBFactory;
window.indexedDB = indexedDB;

View File

@ -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 });
}
});
}

View File

@ -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',
}
});
}

View File

@ -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");
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'
}
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');
})

View File

@ -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];
}

View File

@ -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,
});
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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');
}

View File

@ -17,7 +17,7 @@ async function loadPages() {
if (cookies) {
logger.debug('cookieJar', cookies);
}
process.exit();
dom.window.close();
}
}
loadPages()

View File

@ -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()

View File

@ -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": {

View File

@ -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",