@alicloud/console-os-browser-vm 1.4.21 → 1.4.24
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/LICENSE +21 -0
- package/es/Context.js +148 -0
- package/es/Document.js +79 -0
- package/es/Elements.js +61 -0
- package/es/History.js +40 -0
- package/es/Location.js +42 -0
- package/es/Window.js +69 -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 +3 -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 +154 -0
- package/lib/Document.js +86 -0
- package/lib/Elements.js +67 -0
- package/lib/History.js +48 -0
- package/lib/Location.js +50 -0
- package/lib/Window.js +78 -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 +10 -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 +11 -12
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Alibaba Cloud
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/es/Context.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
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
|
+
/**
|
|
9
|
+
* Context
|
|
10
|
+
* @lastModified 2019086
|
|
11
|
+
* @forwardCompatibleTo 2019086
|
|
12
|
+
* @createAt 2019086
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import Window from './Window';
|
|
16
|
+
import Document from './Document';
|
|
17
|
+
import Location from './Location';
|
|
18
|
+
import History from './History';
|
|
19
|
+
import { getFetchCredentials } from './utils/credentials';
|
|
20
|
+
var Context = /*#__PURE__*/function () {
|
|
21
|
+
function Context(conf, frame) {
|
|
22
|
+
_classCallCheck(this, Context);
|
|
23
|
+
this.location = new Location(frame.contentWindow.location);
|
|
24
|
+
this.history = new History(conf.id, frame.contentWindow);
|
|
25
|
+
this.window = new Window(conf, this, frame, location);
|
|
26
|
+
this.allowResources = conf.allowResources || [];
|
|
27
|
+
var body = conf.body;
|
|
28
|
+
if (conf.disableBody) {
|
|
29
|
+
body = document.body;
|
|
30
|
+
} else if (!conf.body) {
|
|
31
|
+
body = document.createElement('body');
|
|
32
|
+
document.documentElement.appendChild(body);
|
|
33
|
+
}
|
|
34
|
+
this.body = body;
|
|
35
|
+
this.document = new Document(_objectSpread({
|
|
36
|
+
enableScriptEscape: true
|
|
37
|
+
}, conf), this, frame, location);
|
|
38
|
+
this.baseFrame = frame;
|
|
39
|
+
this._listenerMap = new Map();
|
|
40
|
+
this.window.__IS_CONSOLE_OS_CONTEXT__ = true;
|
|
41
|
+
}
|
|
42
|
+
_createClass(Context, [{
|
|
43
|
+
key: "loadScripts",
|
|
44
|
+
value: function () {
|
|
45
|
+
var _loadScripts = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(url) {
|
|
46
|
+
var resp, code;
|
|
47
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
48
|
+
while (1) switch (_context.prev = _context.next) {
|
|
49
|
+
case 0:
|
|
50
|
+
_context.next = 2;
|
|
51
|
+
return fetch(url, {
|
|
52
|
+
credentials: getFetchCredentials(url)
|
|
53
|
+
});
|
|
54
|
+
case 2:
|
|
55
|
+
resp = _context.sent;
|
|
56
|
+
_context.next = 5;
|
|
57
|
+
return resp.text();
|
|
58
|
+
case 5:
|
|
59
|
+
code = _context.sent;
|
|
60
|
+
this.evalScript(code, url);
|
|
61
|
+
case 7:
|
|
62
|
+
case "end":
|
|
63
|
+
return _context.stop();
|
|
64
|
+
}
|
|
65
|
+
}, _callee, this);
|
|
66
|
+
}));
|
|
67
|
+
function loadScripts(_x) {
|
|
68
|
+
return _loadScripts.apply(this, arguments);
|
|
69
|
+
}
|
|
70
|
+
return loadScripts;
|
|
71
|
+
}()
|
|
72
|
+
}, {
|
|
73
|
+
key: "evalScript",
|
|
74
|
+
value: function evalScript(code) {
|
|
75
|
+
var url = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
76
|
+
// eslint-disable-next-line no-new-func
|
|
77
|
+
var resolver = new Function("\n return function ({window, location, history, document}){ \n with(window.__CONSOLE_OS_GLOBAL_VARS_) {\n try {\n ".concat(code, "\n }\n catch(e) {\n console.log(e)\n }\n }\n }//@ sourceMappingURL=").concat(url));
|
|
78
|
+
resolver().call(this.window, _objectSpread({}, this));
|
|
79
|
+
}
|
|
80
|
+
}, {
|
|
81
|
+
key: "updateBody",
|
|
82
|
+
value: function updateBody(dom) {
|
|
83
|
+
if (!dom && this.body) return;
|
|
84
|
+
this.body = dom;
|
|
85
|
+
}
|
|
86
|
+
}, {
|
|
87
|
+
key: "remove",
|
|
88
|
+
value: function remove() {
|
|
89
|
+
this.document.removeEventListeners();
|
|
90
|
+
if (this.baseFrame) {
|
|
91
|
+
if (this.baseFrame.parentNode) {
|
|
92
|
+
this.baseFrame.parentNode.removeChild(this.baseFrame);
|
|
93
|
+
} else {
|
|
94
|
+
this.baseFrame.setAttribute('src', 'about:blank');
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}], [{
|
|
99
|
+
key: "create",
|
|
100
|
+
value: function create(conf) {
|
|
101
|
+
var _this = this;
|
|
102
|
+
return new Promise(function (resolve) {
|
|
103
|
+
var _document$documentEle;
|
|
104
|
+
var iframe = document.createElement('iframe');
|
|
105
|
+
|
|
106
|
+
// TODO: change src to a reasonable value.
|
|
107
|
+
iframe.setAttribute('src', conf.url ? conf.url : '/api.json');
|
|
108
|
+
iframe.style.cssText = 'position: absolute; top: -20000px; width: 1px; height: 1px;';
|
|
109
|
+
if (conf.id) iframe.setAttribute('data-id', conf.id);
|
|
110
|
+
|
|
111
|
+
// body will be hijacked in sandbox, so get body from html
|
|
112
|
+
var topBody = ((_document$documentEle = document.documentElement) === null || _document$documentEle === void 0 ? void 0 : _document$documentEle.getElementsByTagName('body')[0]) || document.body;
|
|
113
|
+
topBody.appendChild(iframe);
|
|
114
|
+
|
|
115
|
+
// the onload will no trigger when src is about:blank
|
|
116
|
+
if (conf.url === 'about:blank') {
|
|
117
|
+
return resolve(new _this(conf, iframe));
|
|
118
|
+
}
|
|
119
|
+
iframe.onload = function () {
|
|
120
|
+
resolve(new _this(conf, iframe));
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}, {
|
|
125
|
+
key: "remove",
|
|
126
|
+
value: function () {
|
|
127
|
+
var _remove = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(context) {
|
|
128
|
+
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
129
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
130
|
+
case 0:
|
|
131
|
+
if (context.remove) {
|
|
132
|
+
context.remove();
|
|
133
|
+
}
|
|
134
|
+
case 1:
|
|
135
|
+
case "end":
|
|
136
|
+
return _context2.stop();
|
|
137
|
+
}
|
|
138
|
+
}, _callee2);
|
|
139
|
+
}));
|
|
140
|
+
function remove(_x2) {
|
|
141
|
+
return _remove.apply(this, arguments);
|
|
142
|
+
}
|
|
143
|
+
return remove;
|
|
144
|
+
}()
|
|
145
|
+
}]);
|
|
146
|
+
return Context;
|
|
147
|
+
}();
|
|
148
|
+
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);
|
|
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,42 @@
|
|
|
1
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
2
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
3
|
+
var Location = /*#__PURE__*/_createClass(function Location(location) {
|
|
4
|
+
_classCallCheck(this, Location);
|
|
5
|
+
return new Proxy({}, {
|
|
6
|
+
set: function set(target, name, value) {
|
|
7
|
+
switch (name) {
|
|
8
|
+
case 'href':
|
|
9
|
+
break;
|
|
10
|
+
default:
|
|
11
|
+
location[name] = value;
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
},
|
|
15
|
+
get: function get(target, name) {
|
|
16
|
+
switch (name) {
|
|
17
|
+
case 'reload':
|
|
18
|
+
return function () {
|
|
19
|
+
return window.location.reload();
|
|
20
|
+
};
|
|
21
|
+
case 'replace':
|
|
22
|
+
return function () {};
|
|
23
|
+
case 'toString':
|
|
24
|
+
return function () {
|
|
25
|
+
try {
|
|
26
|
+
return location.toString();
|
|
27
|
+
} catch (e) {
|
|
28
|
+
return location.href;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
default:
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
if (typeof location[name] === 'function') {
|
|
35
|
+
return location[name].bind && location[name].bind(target);
|
|
36
|
+
} else {
|
|
37
|
+
return location[name];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
export default Location;
|
package/es/Window.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
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 defaultExternals = ['requestAnimationFrame', 'webkitRequestAnimationFrame', 'mozRequestAnimationFrame', 'oRequestAnimationFrame', 'msRequestAnimationFrame', 'cancelAnimationFrame', 'webkitCancelAnimationFrame', 'mozCancelAnimationFrame', 'oCancelAnimationFrame', 'msCancelAnimationFrame', 'matchMedia'];
|
|
11
|
+
var Window = /*#__PURE__*/_createClass(function Window() {
|
|
12
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
13
|
+
var context = arguments.length > 1 ? arguments[1] : undefined;
|
|
14
|
+
var frame = arguments.length > 2 ? arguments[2] : undefined;
|
|
15
|
+
_classCallCheck(this, Window);
|
|
16
|
+
var externals = [].concat(defaultExternals, _toConsumableArray(options.externals || []));
|
|
17
|
+
var __CONSOLE_OS_GLOBAL_VARS_ = {};
|
|
18
|
+
globalFnName.forEach(function (name) {
|
|
19
|
+
if (externals.includes(name)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
__CONSOLE_OS_GLOBAL_VARS_[name] = frame.contentWindow[name].bind(frame.contentWindow);
|
|
23
|
+
});
|
|
24
|
+
return new Proxy(frame.contentWindow, {
|
|
25
|
+
set: function set(target, name, value) {
|
|
26
|
+
target[name] = value;
|
|
27
|
+
__CONSOLE_OS_GLOBAL_VARS_[name] = value;
|
|
28
|
+
return true;
|
|
29
|
+
},
|
|
30
|
+
get: function get(target, name) {
|
|
31
|
+
if (externals.includes(name)) {
|
|
32
|
+
var windowValue = window[name];
|
|
33
|
+
if (typeof windowValue === 'function' && !isBoundedFunction(windowValue) && !isConstructable(windowValue)) {
|
|
34
|
+
var bindFn = windowValue.bind(window);
|
|
35
|
+
for (var key in windowValue) {
|
|
36
|
+
bindFn[key] = windowValue[key];
|
|
37
|
+
}
|
|
38
|
+
return bindFn;
|
|
39
|
+
} else {
|
|
40
|
+
return windowValue;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
switch (name) {
|
|
44
|
+
case 'document':
|
|
45
|
+
return context.document;
|
|
46
|
+
case 'location':
|
|
47
|
+
return context.location;
|
|
48
|
+
case 'history':
|
|
49
|
+
return context.history;
|
|
50
|
+
case '__CONSOLE_OS_GLOBAL_VARS_':
|
|
51
|
+
return __CONSOLE_OS_GLOBAL_VARS_;
|
|
52
|
+
case 'addEventListener':
|
|
53
|
+
return addEventListener(context);
|
|
54
|
+
case 'removeEventListener':
|
|
55
|
+
return removeEventListener(context);
|
|
56
|
+
}
|
|
57
|
+
if (__CONSOLE_OS_GLOBAL_VARS_[name]) {
|
|
58
|
+
return __CONSOLE_OS_GLOBAL_VARS_[name];
|
|
59
|
+
}
|
|
60
|
+
var value = target[name];
|
|
61
|
+
if (typeof value === 'function' && !isBoundedFunction(value) && !isConstructable(value)) {
|
|
62
|
+
return value.bind && value.bind(target);
|
|
63
|
+
} else {
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
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,30 @@
|
|
|
1
|
+
export var getJsonCallback = function getJsonCallback(src) {
|
|
2
|
+
var u;
|
|
3
|
+
try {
|
|
4
|
+
u = new URL(src);
|
|
5
|
+
} catch (error) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
var sp = u.searchParams;
|
|
9
|
+
if (!sp) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
return sp.get('callback') || sp.get('cb');
|
|
13
|
+
};
|
|
14
|
+
var injectScriptCallBack = function injectScriptCallBack(scriptEl) {
|
|
15
|
+
setTimeout(function () {
|
|
16
|
+
if (!scriptEl.src) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
var appWindow = scriptEl.ownerContext.window;
|
|
20
|
+
var callbackName = getJsonCallback(scriptEl.src);
|
|
21
|
+
if (callbackName && typeof appWindow[callbackName] === 'function') {
|
|
22
|
+
window[callbackName] = function () {
|
|
23
|
+
var result = appWindow[callbackName].apply(appWindow, arguments);
|
|
24
|
+
window[callbackName] = null;
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}, 0);
|
|
29
|
+
};
|
|
30
|
+
export default injectScriptCallBack;
|