Skip to content

Conversation

cyfung1031
Copy link
Contributor

@cyfung1031 cyfung1031 commented Aug 16, 2023

close #118

Remarks

Since most requests are async, it might not be suitable to do the bypassing in onRequest.
onConfig is synchronously triggered in xhr.open, so it can completely bypass the entire open+setRequestHeader(POST request)+send.

Usage

const { unProxy, originXhr } = proxy({
    onConfig: (config, xhrProxy) => {
        console.log(`url: ${config.url}`)
        if (config.url.indexOf('google.com') >= 0) return false;
    },
    onRequest: (config, handler) => {
        handler.next(config);
    },
    onError: (err, handler) => {
        handler.next(err)
    },
    onResponse: (response, handler) => {
        handler.next(response)
    }
})

cyfung1031 added a commit to cyfung1031/ajax-hook that referenced this pull request Aug 16, 2023
cyfung1031 added a commit to cyfung1031/ajax-hook that referenced this pull request Aug 16, 2023
cyfung1031 added a commit to cyfung1031/ajax-hook that referenced this pull request Aug 16, 2023
cyfung1031 added a commit to cyfung1031/ajax-hook that referenced this pull request Aug 16, 2023
cyfung1031 added a commit to cyfung1031/ajax-hook that referenced this pull request Aug 16, 2023
@cyfung1031
Copy link
Contributor Author

Combined Distribution Script ajaxhook.js for Testing (PR 119, 120, 121, 122)
https://cdn.jsdelivr.net/gh/cyfung1031/ajax-hook@1ebe48e08108449669290a226e52fc6fbf7ec9ef/dist/ajaxhook.js

if (onConfig) {
if (onConfig(config, this) === false) onRequest = null;
}
if (onRequest) return true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面这段代码直接改成这样就可以了:

if (onFilter && onFilter(config)) {
    return;
}

if (onRequest) return true;

可以看到 hookFunction 里面,当函数没有 ret 的时候,就会直接走原生方法:
image

}

interface Proxy {
onConfig?: (config: XhrRequestConfig, xhrProxy: Hooks) => boolean | void,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成 onFilter 吧,语义化更好,第二个参数没什么太大用处,可以不传,因为过滤 url 后就可以直接走原生方法了,不用手动调用,切莫过度设计

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改了

@DAHUIAAAAA
Copy link
Collaborator

新增 api 需要完善一下 markdown 文档

@cyfung1031
Copy link
Contributor Author

新增 api 需要完善一下 markdown 文档

新增了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

proxy 的 onRequest 有沒有方法像 hook 一樣整個bypass?
2 participants