Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/justjavac/proxy-www
学会 Proxy 就可以为所欲为吗?对,学会 Proxy 就可以为所欲为!
https://github.com/justjavac/proxy-www
es6 javascript proxy typescript
Last synced: about 1 month ago
JSON representation
学会 Proxy 就可以为所欲为吗?对,学会 Proxy 就可以为所欲为!
- Host: GitHub
- URL: https://github.com/justjavac/proxy-www
- Owner: justjavac
- License: unlicense
- Created: 2021-02-22T15:01:58.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-05-01T09:27:56.000Z (over 3 years ago)
- Last Synced: 2024-10-30T05:58:13.570Z (about 1 month ago)
- Topics: es6, javascript, proxy, typescript
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 882
- Watchers: 9
- Forks: 28
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- my-awesome - justjavac/proxy-www - 05 star:0.9k fork:0.0k 学会 Proxy 就可以为所欲为吗?对,学会 Proxy 就可以为所欲为! (JavaScript)
- awesome-list - proxy-www
README
# proxy-www
> 学会 Proxy 就可以为所欲为吗?
>
> 对,学会 Proxy 就可以为所欲为!原始来源:https://twitter.com/RReverser/status/1138788910975397888 #14
```js
const www = new Proxy(new URL('https://www'), {
get: function get(target, prop) {
let o = Reflect.get(target, prop);
if (typeof o === 'function') {
return o.bind(target)
}
if (typeof prop !== 'string') {
return o;
}
if (prop === 'then') {
return Promise.prototype.then.bind(fetch(target));
}
target = new URL(target);
target.hostname += `.${prop}`;
return new Proxy(target, { get });
}
});
```访问百度
```js
www.baidu.com.then(response => {
console.log(response.status);
// ==> 200
})
```使用 `async`/`await` 语法:
```js
const response = await www.baidu.comconsole.log(response.ok)
// ==> trueconsole.log(response.status);
// ==> 200
```