ProxymanApp / Proxyman

Modern. Native. Delightful Web Debugging Proxy for macOS, iOS, and Android ⚡️
https://proxyman.io
5.73k stars 189 forks source link

[Scripting] Possible to parse a URL? #885

Open jsorge opened 3 years ago

jsorge commented 3 years ago

Proxyman version? (Ex. Proxyman 1.4.3)

2.25.0

macOS Version? (Ex. mac 10.14)

11.3.1

In Scripting, is it possible to use the url module (I'm not sure what it's called in JS) to parse a URL? I've got this code in a CodeRunner window using Node and it works:

var URL = require('url').URL;
var myURL = new URL('http://www.example.com/foo?bar=1#main');
console.log(myURL.host); // prints 'www.example.com'

However when I try to do this in the Scripting window I get this error:

❌Error: Could not import 3rd scripts at url. Only support @users, @addons and @libs
❌Error: TypeError: undefined is not an object (evaluating 'require('url').URL') 

I'm hoping there's a way to do what I'm after. Thanks for a great app; Proxyman is fantastic!

NghiaTranUIT commented 3 years ago

Hi @jsorge Unfortunately, the scripting could not use NodeJS modules because the scripting is built on top of JavascriptCore, which is a pure JS framework.

JavascriptCore doesn't support this URL too

Just wondering: How often do you use URL for the scripting? If it's an important class, I might find a solution to support it 👍

jsorge commented 3 years ago

Ah yeah that makes sense @NghiaTranUIT, thanks for the explanation!

It's not something I'm doing a bunch (today was the first time) but I do have to find an alternate solution. I really only need the value of a path parameter so maybe it's time to turn to a regex.

NghiaTranUIT commented 3 years ago

You can easily get a path of the URL by using

const path = request.path;

Ref: https://docs.proxyman.io/scripting/script#onrequest-object-format

jsorge commented 3 years ago

Ah yeah this is a bit different. We route to new places in our app through protocol URIs that come in the payload as strings. I need to parse one of those 😊

NghiaTranUIT commented 3 years ago

Thanks. I will find a way to support native URL if it's possible 👍

Zirro commented 3 years ago

https://github.com/zloirock/core-js has well-designed implementations of URL and URLSearchParams (among many others) which are designed to work in buggy or unusual environments that don't support them natively. You could probably inject them as globals into JavaScriptCore and have them work as if they were a native implementation.

NghiaTranUIT commented 3 years ago

Thanks for your suggestion @Zirro Let me play around and try to integrate it 👍