@alicloud/console-os-browser-vm 1.4.33 → 1.4.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/Context.js +147 -0
- package/es/Document.js +79 -0
- package/es/Elements.js +61 -0
- package/es/History.js +40 -0
- package/es/Location.js +47 -0
- package/es/Window.js +92 -0
- package/es/events.js +28 -0
- package/es/index.js +82 -0
- package/es/utils/HTMLScriptElement.js +43 -0
- package/es/utils/common.js +9 -0
- package/es/utils/credentials.js +28 -0
- package/es/utils/injectScriptCallBack.js +30 -0
- package/es/utils/isSSR.js +3 -0
- package/es/utils/not-implemented.js +3 -0
- package/lib/Context.js +155 -0
- package/lib/Document.js +86 -0
- package/lib/Elements.js +67 -0
- package/lib/History.js +48 -0
- package/lib/Location.js +55 -0
- package/lib/Window.js +101 -0
- package/lib/events.js +38 -0
- package/lib/index.js +90 -0
- package/lib/utils/HTMLScriptElement.js +53 -0
- package/lib/utils/common.js +16 -0
- package/lib/utils/credentials.js +35 -0
- package/lib/utils/injectScriptCallBack.js +38 -0
- package/lib/utils/isSSR.js +10 -0
- package/lib/utils/not-implemented.js +10 -0
- package/package.json +1 -1
package/es/Context.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
2
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
3
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
4
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
5
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
6
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
7
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
8
|
+
import Window from './Window';
|
|
9
|
+
import Document from './Document';
|
|
10
|
+
import Location from './Location';
|
|
11
|
+
import History from './History';
|
|
12
|
+
import { getFetchCredentials } from './utils/credentials';
|
|
13
|
+
var Context = /*#__PURE__*/function () {
|
|
14
|
+
function Context(conf, frame) {
|
|
15
|
+
_classCallCheck(this, Context);
|
|
16
|
+
this.location = new Location(frame.contentWindow.location);
|
|
17
|
+
this.history = new History(conf.id, frame.contentWindow);
|
|
18
|
+
this.window = new Window(conf, this, frame, location);
|
|
19
|
+
this.allowResources = conf.allowResources || [];
|
|
20
|
+
var body = conf.body;
|
|
21
|
+
var disableBody = conf.disableBody;
|
|
22
|
+
if (disableBody) {
|
|
23
|
+
body = document.body;
|
|
24
|
+
} else if (!body) {
|
|
25
|
+
body = document.createElement('body');
|
|
26
|
+
document.documentElement.appendChild(body);
|
|
27
|
+
}
|
|
28
|
+
this.body = body;
|
|
29
|
+
this.document = new Document(_objectSpread({
|
|
30
|
+
enableScriptEscape: true
|
|
31
|
+
}, conf), this, frame, location);
|
|
32
|
+
this.baseFrame = frame;
|
|
33
|
+
this._listenerMap = new Map();
|
|
34
|
+
this.window.__IS_CONSOLE_OS_CONTEXT__ = true;
|
|
35
|
+
}
|
|
36
|
+
_createClass(Context, [{
|
|
37
|
+
key: "loadScripts",
|
|
38
|
+
value: function () {
|
|
39
|
+
var _loadScripts = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(url) {
|
|
40
|
+
var resp, code;
|
|
41
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
42
|
+
while (1) switch (_context.prev = _context.next) {
|
|
43
|
+
case 0:
|
|
44
|
+
_context.next = 2;
|
|
45
|
+
return fetch(url, {
|
|
46
|
+
credentials: getFetchCredentials(url)
|
|
47
|
+
});
|
|
48
|
+
case 2:
|
|
49
|
+
resp = _context.sent;
|
|
50
|
+
_context.next = 5;
|
|
51
|
+
return resp.text();
|
|
52
|
+
case 5:
|
|
53
|
+
code = _context.sent;
|
|
54
|
+
this.evalScript(code, url);
|
|
55
|
+
case 7:
|
|
56
|
+
case "end":
|
|
57
|
+
return _context.stop();
|
|
58
|
+
}
|
|
59
|
+
}, _callee, this);
|
|
60
|
+
}));
|
|
61
|
+
function loadScripts(_x) {
|
|
62
|
+
return _loadScripts.apply(this, arguments);
|
|
63
|
+
}
|
|
64
|
+
return loadScripts;
|
|
65
|
+
}()
|
|
66
|
+
}, {
|
|
67
|
+
key: "evalScript",
|
|
68
|
+
value: function evalScript(code) {
|
|
69
|
+
var url = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
70
|
+
var currentScript = this.document.createElement('script');
|
|
71
|
+
currentScript.src = url;
|
|
72
|
+
|
|
73
|
+
// eslint-disable-next-line no-new-func
|
|
74
|
+
var resolver = new Function("\n return function ({window, location, history, document, __os_current_script__}){\n var document = new Proxy(document, {\n get: function get(target, prop) {\n if (prop === \"currentScript\" && __os_current_script__) {\n return __os_current_script__;\n }\n return target[prop];\n }\n });\n var window = new Proxy(window, {\n get: function get(target, prop) {\n if (prop === \"document\") return document;\n return target[prop];\n }\n })\n var globalThis = window;\n with(window.__CONSOLE_OS_GLOBAL_VARS_) {\n try {\n ".concat(code, "\n } catch(e) {\n console.log(e)\n }\n }\n }//# sourceMappingURL=").concat(url));
|
|
75
|
+
resolver().call(this.window, _objectSpread(_objectSpread({}, this), {}, {
|
|
76
|
+
__os_current_script__: currentScript
|
|
77
|
+
}));
|
|
78
|
+
}
|
|
79
|
+
}, {
|
|
80
|
+
key: "updateBody",
|
|
81
|
+
value: function updateBody(dom) {
|
|
82
|
+
if (!dom && this.body) return;
|
|
83
|
+
this.body = dom;
|
|
84
|
+
}
|
|
85
|
+
}, {
|
|
86
|
+
key: "remove",
|
|
87
|
+
value: function remove() {
|
|
88
|
+
this.document.removeEventListeners();
|
|
89
|
+
if (this.baseFrame) {
|
|
90
|
+
if (this.baseFrame.parentNode) {
|
|
91
|
+
this.baseFrame.parentNode.removeChild(this.baseFrame);
|
|
92
|
+
} else {
|
|
93
|
+
this.baseFrame.setAttribute('src', 'about:blank');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}], [{
|
|
98
|
+
key: "create",
|
|
99
|
+
value: function create(conf) {
|
|
100
|
+
var _this = this;
|
|
101
|
+
return new Promise(function (resolve) {
|
|
102
|
+
var _document$documentEle;
|
|
103
|
+
var iframe = document.createElement('iframe');
|
|
104
|
+
|
|
105
|
+
// TODO: change src to a reasonable value.
|
|
106
|
+
iframe.setAttribute('src', conf.url ? conf.url : '/api.json');
|
|
107
|
+
iframe.style.cssText = 'position: absolute; top: -20000px; width: 1px; height: 1px;';
|
|
108
|
+
if (conf.id) iframe.setAttribute('data-id', conf.id);
|
|
109
|
+
|
|
110
|
+
// body will be hijacked in sandbox, so get body from html
|
|
111
|
+
var topBody = ((_document$documentEle = document.documentElement) === null || _document$documentEle === void 0 ? void 0 : _document$documentEle.getElementsByTagName('body')[0]) || document.body;
|
|
112
|
+
topBody.appendChild(iframe);
|
|
113
|
+
|
|
114
|
+
// the onload will no trigger when src is about:blank
|
|
115
|
+
if (conf.url === 'about:blank') {
|
|
116
|
+
return resolve(new _this(conf, iframe));
|
|
117
|
+
}
|
|
118
|
+
iframe.onload = function () {
|
|
119
|
+
resolve(new _this(conf, iframe));
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}, {
|
|
124
|
+
key: "remove",
|
|
125
|
+
value: function () {
|
|
126
|
+
var _remove = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(context) {
|
|
127
|
+
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
128
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
129
|
+
case 0:
|
|
130
|
+
if (context.remove) {
|
|
131
|
+
context.remove();
|
|
132
|
+
}
|
|
133
|
+
case 1:
|
|
134
|
+
case "end":
|
|
135
|
+
return _context2.stop();
|
|
136
|
+
}
|
|
137
|
+
}, _callee2);
|
|
138
|
+
}));
|
|
139
|
+
function remove(_x2) {
|
|
140
|
+
return _remove.apply(this, arguments);
|
|
141
|
+
}
|
|
142
|
+
return remove;
|
|
143
|
+
}()
|
|
144
|
+
}]);
|
|
145
|
+
return Context;
|
|
146
|
+
}();
|
|
147
|
+
export default Context;
|
package/es/Document.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
3
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
4
|
+
/**
|
|
5
|
+
* Document.js
|
|
6
|
+
* @lastModified 2019085
|
|
7
|
+
* @forwardCompatibleTo 2019085
|
|
8
|
+
* @createAt 2019085
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { injectHTMLScriptElement } from './utils/HTMLScriptElement';
|
|
12
|
+
var Document = /*#__PURE__*/_createClass(function Document() {
|
|
13
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
14
|
+
var context = arguments.length > 1 ? arguments[1] : undefined;
|
|
15
|
+
var frame = arguments.length > 2 ? arguments[2] : undefined;
|
|
16
|
+
_classCallCheck(this, Document);
|
|
17
|
+
var eventListeners = [];
|
|
18
|
+
return new Proxy(document, {
|
|
19
|
+
set: function set(target, name, value) {
|
|
20
|
+
switch (name) {
|
|
21
|
+
case 'cookie':
|
|
22
|
+
document.cookie = value;
|
|
23
|
+
break;
|
|
24
|
+
default:
|
|
25
|
+
target[name] = value;
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
},
|
|
29
|
+
get: function get(target, name) {
|
|
30
|
+
switch (name) {
|
|
31
|
+
case 'body':
|
|
32
|
+
return context.body;
|
|
33
|
+
case 'location':
|
|
34
|
+
return context.location;
|
|
35
|
+
case 'defaultView':
|
|
36
|
+
return context.window;
|
|
37
|
+
case 'write':
|
|
38
|
+
case 'writeln':
|
|
39
|
+
return function () {};
|
|
40
|
+
case 'createElement':
|
|
41
|
+
return function () {
|
|
42
|
+
var _document;
|
|
43
|
+
var el = (_document = document).createElement.apply(_document, arguments);
|
|
44
|
+
el.ownerContext = context;
|
|
45
|
+
el.appId = options.id;
|
|
46
|
+
// 对于 script 标签如果开启防逃逸,就直接 hack script 标签
|
|
47
|
+
if (el.tagName === 'SCRIPT' && !options.enableScriptEscape) {
|
|
48
|
+
injectHTMLScriptElement(el);
|
|
49
|
+
}
|
|
50
|
+
return el;
|
|
51
|
+
};
|
|
52
|
+
case 'addEventListener':
|
|
53
|
+
return function () {
|
|
54
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
55
|
+
args[_key] = arguments[_key];
|
|
56
|
+
}
|
|
57
|
+
eventListeners.push(args);
|
|
58
|
+
return target.addEventListener.apply(target, args);
|
|
59
|
+
};
|
|
60
|
+
case 'removeEventListeners':
|
|
61
|
+
return function () {
|
|
62
|
+
for (var _i = 0, _eventListeners = eventListeners; _i < _eventListeners.length; _i++) {
|
|
63
|
+
var args = _eventListeners[_i];
|
|
64
|
+
target.removeEventListener.apply(target, _toConsumableArray(args));
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
default:
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// TODO: hijack document.getElementsByTagName('body')
|
|
71
|
+
if (typeof target[name] === 'function') {
|
|
72
|
+
return target[name].bind && target[name].bind(target);
|
|
73
|
+
} else {
|
|
74
|
+
return target[name];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
export default Document;
|
package/es/Elements.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
|
+
import injectScriptCallBack, { getJsonCallback } from './utils/injectScriptCallBack';
|
|
3
|
+
import { isSSR } from './utils/isSSR';
|
|
4
|
+
var makeElInjector = function makeElInjector(originMethod, methodName) {
|
|
5
|
+
return function (el) {
|
|
6
|
+
var notAllowed = el && el.ownerContext && el.ownerContext.allowResources.indexOf(el.src) === -1 && el.ownerContext.allowResources !== '*';
|
|
7
|
+
|
|
8
|
+
// 如果不在 BrowserVM 的白名单内,尝试通过 context 的 load script 通过 xhr 获取脚本内容,并在沙箱中执行
|
|
9
|
+
if (el && el._evalScriptInSandbox && el.ownerContext && el.nodeName === 'SCRIPT' && el.src && notAllowed && !getJsonCallback(el.src)) {
|
|
10
|
+
return el.ownerContext.loadScripts(el.src).then(function () {
|
|
11
|
+
var fns = el._listenerMap.get('load');
|
|
12
|
+
if (fns) {
|
|
13
|
+
fns.forEach(function (fn) {
|
|
14
|
+
fn();
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
var e = new Event('load');
|
|
18
|
+
el.onload && el.onload(e);
|
|
19
|
+
}).catch(function (e) {
|
|
20
|
+
console.error(e, el.src);
|
|
21
|
+
var fns = el._listenerMap.get('error');
|
|
22
|
+
if (fns) {
|
|
23
|
+
fns.forEach(function (fn) {
|
|
24
|
+
fn(e);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
el.onerror && el.onerror(e);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (el && el.nodeName === 'SCRIPT' && el.ownerContext) {
|
|
31
|
+
injectScriptCallBack(el);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 如果有 scriptText, 证明在 append 之前被设置了 text
|
|
35
|
+
// 所以直接执行这个 script
|
|
36
|
+
if (el && el.scriptText) {
|
|
37
|
+
el.ownerContext.evalScript(el.scriptText);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// fix: babel 会把 fn.call 转义成 fn.call.apply 造成 chrome 50 的几个版本报错
|
|
41
|
+
// 这里 尝试 try catch 如果报错 直接调用 fn.apply
|
|
42
|
+
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
43
|
+
args[_key - 1] = arguments[_key];
|
|
44
|
+
}
|
|
45
|
+
var methodArgs = methodName === 'append' && el === undefined // 修复 append 的过程中 append() 为一个没参数的状态。应为 append 的函数签名为 append(param1, param2, /* ... ,*/ paramN)
|
|
46
|
+
? [].concat(args) : [el].concat(args);
|
|
47
|
+
try {
|
|
48
|
+
return originMethod.call.apply(originMethod, [this].concat(_toConsumableArray(methodArgs)));
|
|
49
|
+
} catch (_unused) {
|
|
50
|
+
return originMethod.apply(this, methodArgs);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
if (!isSSR() && typeof window.Element === 'function') {
|
|
55
|
+
var mountElementMethods = ['appendChild', 'insertBefore', 'append'];
|
|
56
|
+
for (var _i = 0, _mountElementMethods = mountElementMethods; _i < _mountElementMethods.length; _i++) {
|
|
57
|
+
var method = _mountElementMethods[_i];
|
|
58
|
+
var originMethod = window.Element.prototype[method];
|
|
59
|
+
window.Element.prototype[method] = makeElInjector(originMethod, method);
|
|
60
|
+
}
|
|
61
|
+
}
|
package/es/History.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
2
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
3
|
+
var History = /*#__PURE__*/_createClass(function History(id, frame) {
|
|
4
|
+
_classCallCheck(this, History);
|
|
5
|
+
if (!id) {
|
|
6
|
+
return frame.history;
|
|
7
|
+
}
|
|
8
|
+
var postMessage = function postMessage() {
|
|
9
|
+
frame.postMessage({
|
|
10
|
+
type: "".concat(id, ":history-change"),
|
|
11
|
+
data: JSON.parse(JSON.stringify(frame.location))
|
|
12
|
+
}, '*');
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// throw error when iframe src is about:blank
|
|
16
|
+
try {
|
|
17
|
+
var originalPushStatus = frame.history.pushState;
|
|
18
|
+
var originalReplaceStatus = frame.history.replaceState;
|
|
19
|
+
frame.history.pushState = function () {
|
|
20
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
21
|
+
args[_key] = arguments[_key];
|
|
22
|
+
}
|
|
23
|
+
var returnValue = originalPushStatus.apply(frame.history, [].concat(args));
|
|
24
|
+
postMessage();
|
|
25
|
+
return returnValue;
|
|
26
|
+
};
|
|
27
|
+
frame.history.replaceState = function () {
|
|
28
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
29
|
+
args[_key2] = arguments[_key2];
|
|
30
|
+
}
|
|
31
|
+
var returnValue = originalReplaceStatus.apply(frame.history, [].concat(args));
|
|
32
|
+
postMessage();
|
|
33
|
+
return returnValue;
|
|
34
|
+
};
|
|
35
|
+
return frame.history;
|
|
36
|
+
} catch (e) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
export default History;
|
package/es/Location.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
2
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
3
|
+
function isAboutBlank(loc) {
|
|
4
|
+
return loc && loc.href === 'about:blank';
|
|
5
|
+
}
|
|
6
|
+
var Location = /*#__PURE__*/_createClass(function Location(location) {
|
|
7
|
+
_classCallCheck(this, Location);
|
|
8
|
+
return new Proxy({}, {
|
|
9
|
+
set: function set(target, name, value) {
|
|
10
|
+
switch (name) {
|
|
11
|
+
case 'href':
|
|
12
|
+
break;
|
|
13
|
+
default:
|
|
14
|
+
location[name] = value;
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
},
|
|
18
|
+
get: function get(target, name) {
|
|
19
|
+
switch (name) {
|
|
20
|
+
case 'reload':
|
|
21
|
+
return function () {
|
|
22
|
+
return window.location.reload();
|
|
23
|
+
};
|
|
24
|
+
case 'replace':
|
|
25
|
+
return function () {};
|
|
26
|
+
case 'toString':
|
|
27
|
+
return function () {
|
|
28
|
+
try {
|
|
29
|
+
return isAboutBlank(location) ? window.location.toString() : location.toString();
|
|
30
|
+
} catch (e) {
|
|
31
|
+
return isAboutBlank(location) ? window.location.href : location.href;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
default:
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
if (typeof location[name] === 'function') {
|
|
38
|
+
return location[name].bind && location[name].bind(target);
|
|
39
|
+
} else if (isAboutBlank(location)) {
|
|
40
|
+
return window.location[name];
|
|
41
|
+
} else {
|
|
42
|
+
return location[name];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
export default Location;
|
package/es/Window.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
3
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
4
|
+
/**
|
|
5
|
+
* Window.js
|
|
6
|
+
*/
|
|
7
|
+
import { addEventListener, removeEventListener } from './events';
|
|
8
|
+
import { isConstructable, isBoundedFunction } from './utils/common';
|
|
9
|
+
var globalFnName = ['setTimeout', 'setInterval', 'clearInterval', 'clearTimeout'];
|
|
10
|
+
var globalVars = ['location', 'history'];
|
|
11
|
+
var defaultExternals = ['requestAnimationFrame', 'webkitRequestAnimationFrame', 'mozRequestAnimationFrame', 'oRequestAnimationFrame', 'msRequestAnimationFrame', 'cancelAnimationFrame', 'webkitCancelAnimationFrame', 'mozCancelAnimationFrame', 'oCancelAnimationFrame', 'msCancelAnimationFrame', 'matchMedia', 'getSelection', 'open'];
|
|
12
|
+
function getContextWindow(context) {
|
|
13
|
+
var descriptor = Object.getOwnPropertyDescriptor(context, 'window');
|
|
14
|
+
if (descriptor) {
|
|
15
|
+
descriptor.configurable = true;
|
|
16
|
+
var hasGetter = Object.prototype.hasOwnProperty.call(descriptor, 'get');
|
|
17
|
+
if (!hasGetter) {
|
|
18
|
+
descriptor.writable = true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return context.window;
|
|
22
|
+
}
|
|
23
|
+
var Window = /*#__PURE__*/_createClass(function Window() {
|
|
24
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
25
|
+
var context = arguments.length > 1 ? arguments[1] : undefined;
|
|
26
|
+
var frame = arguments.length > 2 ? arguments[2] : undefined;
|
|
27
|
+
_classCallCheck(this, Window);
|
|
28
|
+
var externals = [].concat(defaultExternals, _toConsumableArray(options.externals || []));
|
|
29
|
+
var __CONSOLE_OS_GLOBAL_VARS_ = {};
|
|
30
|
+
|
|
31
|
+
// 拦截全局方法
|
|
32
|
+
globalFnName.forEach(function (name) {
|
|
33
|
+
if (externals.includes(name)) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
__CONSOLE_OS_GLOBAL_VARS_[name] = frame.contentWindow[name].bind(frame.contentWindow);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// 通过拦截外置的全局变量,指向外部变量
|
|
40
|
+
globalVars.forEach(function (name) {
|
|
41
|
+
if (externals.includes(name)) {
|
|
42
|
+
__CONSOLE_OS_GLOBAL_VARS_[name] = window[name];
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return new Proxy(frame.contentWindow, {
|
|
46
|
+
set: function set(target, name, value) {
|
|
47
|
+
target[name] = value;
|
|
48
|
+
__CONSOLE_OS_GLOBAL_VARS_[name] = value;
|
|
49
|
+
return true;
|
|
50
|
+
},
|
|
51
|
+
get: function get(target, name) {
|
|
52
|
+
if (externals.includes(name)) {
|
|
53
|
+
var windowValue = window[name];
|
|
54
|
+
if (typeof windowValue === 'function' && !isBoundedFunction(windowValue) && !isConstructable(windowValue)) {
|
|
55
|
+
var bindFn = windowValue.bind(window);
|
|
56
|
+
for (var key in windowValue) {
|
|
57
|
+
bindFn[key] = windowValue[key];
|
|
58
|
+
}
|
|
59
|
+
return bindFn;
|
|
60
|
+
} else {
|
|
61
|
+
return windowValue;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
switch (name) {
|
|
65
|
+
case 'window':
|
|
66
|
+
return getContextWindow(context);
|
|
67
|
+
case 'document':
|
|
68
|
+
return context.document;
|
|
69
|
+
case 'location':
|
|
70
|
+
return context.location;
|
|
71
|
+
case 'history':
|
|
72
|
+
return context.history;
|
|
73
|
+
case '__CONSOLE_OS_GLOBAL_VARS_':
|
|
74
|
+
return __CONSOLE_OS_GLOBAL_VARS_;
|
|
75
|
+
case 'addEventListener':
|
|
76
|
+
return addEventListener(context);
|
|
77
|
+
case 'removeEventListener':
|
|
78
|
+
return removeEventListener(context);
|
|
79
|
+
}
|
|
80
|
+
if (__CONSOLE_OS_GLOBAL_VARS_[name]) {
|
|
81
|
+
return __CONSOLE_OS_GLOBAL_VARS_[name];
|
|
82
|
+
}
|
|
83
|
+
var value = target[name];
|
|
84
|
+
if (typeof value === 'function' && !isBoundedFunction(value) && !isConstructable(value)) {
|
|
85
|
+
return value.bind && value.bind(target);
|
|
86
|
+
} else {
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
export default Window;
|
package/es/events.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
|
+
import { isSSR } from './utils/isSSR';
|
|
3
|
+
var rawAddEventListener = !isSSR() ? window.addEventListener : function () {};
|
|
4
|
+
var rawRemoveEventListener = !isSSR() ? window.removeEventListener : function () {};
|
|
5
|
+
;
|
|
6
|
+
export var domEventsListeningTo = ['webkitmouseforcedown', 'webkitmouseforcewillbegin', 'visibilitychange', 'error', 'mousemove', 'mousedown', 'mouseup', 'touchcancel', 'touchend', 'touchstart', 'auxclick', 'dblclick', 'pointercancel', 'pointerdown', 'pointerup', 'dragend', 'dragstart', 'drop', 'compositionend', 'compositionstart', 'keydown', 'keypress', 'keyup', 'input', 'textInput', 'close', 'cancel', 'copy', 'cut', 'paste', 'click', 'change', 'contextmenu', 'reset', 'submit', 'resize', 'scroll', 'foucs', 'blur'];
|
|
7
|
+
export var addEventListener = function addEventListener(context) {
|
|
8
|
+
return function (type, listener, options) {
|
|
9
|
+
var listeners = context._listenerMap.get(type) || [];
|
|
10
|
+
context._listenerMap.set(type, [].concat(_toConsumableArray(listeners), [listener]));
|
|
11
|
+
if (domEventsListeningTo.indexOf(type) === -1) {
|
|
12
|
+
return context.baseFrame.addEventListener.apply(context.baseFrame.contentWindow, [type, listener, options]);
|
|
13
|
+
}
|
|
14
|
+
return rawAddEventListener.apply(window, [type, listener, options]);
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export var removeEventListener = function removeEventListener(context) {
|
|
18
|
+
return function (type, listener, options) {
|
|
19
|
+
var storedTypeListeners = context._listenerMap.get(type);
|
|
20
|
+
if (storedTypeListeners && storedTypeListeners.length && storedTypeListeners.indexOf(listener) !== -1) {
|
|
21
|
+
storedTypeListeners.splice(storedTypeListeners.indexOf(listener), 1);
|
|
22
|
+
}
|
|
23
|
+
if (domEventsListeningTo.indexOf(type) === -1) {
|
|
24
|
+
return context.baseFrame.removeEventListener.apply(context.baseFrame.contentWindow, [type, listener, options]);
|
|
25
|
+
}
|
|
26
|
+
return rawRemoveEventListener.apply(window, [type, listener, options]);
|
|
27
|
+
};
|
|
28
|
+
};
|
package/es/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
3
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
4
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
5
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
6
|
+
/**
|
|
7
|
+
* index.js
|
|
8
|
+
* @lastModified 2019085
|
|
9
|
+
* @forwardCompatibleTo 2019085
|
|
10
|
+
* @createAt 2019085
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import './Elements';
|
|
14
|
+
import Context from './Context';
|
|
15
|
+
export var createContext = /*#__PURE__*/function () {
|
|
16
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
|
17
|
+
var conf,
|
|
18
|
+
_args = arguments;
|
|
19
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
20
|
+
while (1) switch (_context.prev = _context.next) {
|
|
21
|
+
case 0:
|
|
22
|
+
conf = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
|
|
23
|
+
_context.next = 3;
|
|
24
|
+
return Context.create(conf);
|
|
25
|
+
case 3:
|
|
26
|
+
return _context.abrupt("return", _context.sent);
|
|
27
|
+
case 4:
|
|
28
|
+
case "end":
|
|
29
|
+
return _context.stop();
|
|
30
|
+
}
|
|
31
|
+
}, _callee);
|
|
32
|
+
}));
|
|
33
|
+
return function createContext() {
|
|
34
|
+
return _ref.apply(this, arguments);
|
|
35
|
+
};
|
|
36
|
+
}();
|
|
37
|
+
export var removeContext = /*#__PURE__*/function () {
|
|
38
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(context) {
|
|
39
|
+
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
40
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
41
|
+
case 0:
|
|
42
|
+
_context2.next = 2;
|
|
43
|
+
return Context.remove(context);
|
|
44
|
+
case 2:
|
|
45
|
+
return _context2.abrupt("return", _context2.sent);
|
|
46
|
+
case 3:
|
|
47
|
+
case "end":
|
|
48
|
+
return _context2.stop();
|
|
49
|
+
}
|
|
50
|
+
}, _callee2);
|
|
51
|
+
}));
|
|
52
|
+
return function removeContext(_x) {
|
|
53
|
+
return _ref2.apply(this, arguments);
|
|
54
|
+
};
|
|
55
|
+
}();
|
|
56
|
+
export var evalScripts = /*#__PURE__*/function () {
|
|
57
|
+
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(code) {
|
|
58
|
+
var conf,
|
|
59
|
+
ctx,
|
|
60
|
+
resolver,
|
|
61
|
+
_args3 = arguments;
|
|
62
|
+
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
|
|
63
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
64
|
+
case 0:
|
|
65
|
+
conf = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : {};
|
|
66
|
+
_context3.next = 3;
|
|
67
|
+
return Context.create(conf);
|
|
68
|
+
case 3:
|
|
69
|
+
ctx = _context3.sent;
|
|
70
|
+
// eslint-disable-next-line no-new-func
|
|
71
|
+
resolver = new Function("\n return function({window, location, history, document}){ \n with(window.__CONSOLE_OS_GLOBAL_VARS_) { \n ".concat(code, "\n }\n }//@sourceURL=").concat(conf.name));
|
|
72
|
+
return _context3.abrupt("return", resolver(_objectSpread({}, ctx)));
|
|
73
|
+
case 6:
|
|
74
|
+
case "end":
|
|
75
|
+
return _context3.stop();
|
|
76
|
+
}
|
|
77
|
+
}, _callee3);
|
|
78
|
+
}));
|
|
79
|
+
return function evalScripts(_x2) {
|
|
80
|
+
return _ref3.apply(this, arguments);
|
|
81
|
+
};
|
|
82
|
+
}();
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
|
+
export var addEventListener = function addEventListener(el, originEvent) {
|
|
3
|
+
return function (type, listener, options) {
|
|
4
|
+
var listeners = el._listenerMap.get(type) || [];
|
|
5
|
+
el._listenerMap.set(type, [].concat(_toConsumableArray(listeners), [listener]));
|
|
6
|
+
return originEvent.apply(el, [type, listener, options]);
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export var removeEventListener = function removeEventListener(el, originEvent) {
|
|
10
|
+
return function (type, listener, options) {
|
|
11
|
+
var storedTypeListeners = el._listenerMap.get(type);
|
|
12
|
+
if (storedTypeListeners && storedTypeListeners.length && storedTypeListeners.indexOf(listener) !== -1) {
|
|
13
|
+
storedTypeListeners.splice(storedTypeListeners.indexOf(listener), 1);
|
|
14
|
+
}
|
|
15
|
+
return originEvent.apply(el, [type, listener, options]);
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
var elProperties = ['innerHTML', 'text', 'innerText'];
|
|
19
|
+
export var injectHTMLScriptElement = function injectHTMLScriptElement(el) {
|
|
20
|
+
el._listenerMap = new Map();
|
|
21
|
+
el.addEventListener = addEventListener(el, el.addEventListener);
|
|
22
|
+
el.removeEventListener = removeEventListener(el, el.removeEventListener);
|
|
23
|
+
|
|
24
|
+
// do not redefine property in sandbox
|
|
25
|
+
if (!el._evalScriptInSandbox) {
|
|
26
|
+
elProperties.forEach(function (property) {
|
|
27
|
+
Object.defineProperty(el, property, {
|
|
28
|
+
get: function get() {
|
|
29
|
+
return this.scriptText || '';
|
|
30
|
+
},
|
|
31
|
+
set: function set(value) {
|
|
32
|
+
this.scriptText = value;
|
|
33
|
+
// 如果是已经插入到 dom 树里面,则直接执行
|
|
34
|
+
if (el.parentNode) {
|
|
35
|
+
this.ownerContext.evalScript && el.ownerContext.evalScript(value.toString());
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
enumerable: false
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
el._evalScriptInSandbox = true;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function isConstructable(fn) {
|
|
2
|
+
var classRegex = /^class\b/;
|
|
3
|
+
var constructable = fn.prototype && fn.prototype.constructor === fn && Object.getOwnPropertyNames(fn.prototype).length > 1 || classRegex.test(fn.toString());
|
|
4
|
+
return constructable;
|
|
5
|
+
}
|
|
6
|
+
export function isBoundedFunction(fn) {
|
|
7
|
+
var bounded = fn.name.indexOf('bound ') === 0 && !fn.hasOwnProperty('prototype');
|
|
8
|
+
return bounded;
|
|
9
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
function isSameOriginUrl(str, origin) {
|
|
2
|
+
try {
|
|
3
|
+
// str 的格式可以被解析成 url
|
|
4
|
+
var url = new URL(str);
|
|
5
|
+
return url.origin === origin;
|
|
6
|
+
} catch (e) {/**/}
|
|
7
|
+
try {
|
|
8
|
+
// str 是相对路径或绝对路径,无法被解析成 url
|
|
9
|
+
var _url = new URL(str, origin);
|
|
10
|
+
return _url.origin === origin;
|
|
11
|
+
} catch (e) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// 某些情况下,请求静态资源需要带 cookie
|
|
17
|
+
export var getFetchCredentials = function getFetchCredentials(url) {
|
|
18
|
+
// 兼容历史逻辑
|
|
19
|
+
if (url.includes('console.aliyun.com') || url.includes('console.alibabacloud.com')) {
|
|
20
|
+
return 'include';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 同域请求
|
|
24
|
+
if (isSameOriginUrl(url, location && location.origin)) {
|
|
25
|
+
return 'include';
|
|
26
|
+
}
|
|
27
|
+
return 'omit';
|
|
28
|
+
};
|