Why is the shExpMatch function not working inside pacScript?

  • 0
    I am writing an extension for Google Chrome that on the fly would change the proxy in the browser depending on the address.

    Added this config to background.js:
    config = {
    	mode: "pac_script",
    	pacScript: {
    	data: `
    			function FindProxyForURL(url, host) {
    				if (shExpMatch(url, "*1")) {
    					return 'PROXY';
    	{value: config, scope: 'regular'},
    	function() {}

    That is, in theory, if the site address contains a unit at the end, then the specified proxy should have been used, but alas, this does not happen. And I don't even know how to look inside the pack script to understand what's going on under the soot (

    For tests, I went here:

    And yes, I don’t see any errors in the code, because if, for example, in the conditions, instead of the regular time, you simply set true, then changing the proxy works for itself and Yandex shows a different IP, which means that in principle the method of changing the proxy is correct. The only question is why the regular season does not work out ...
    JavaScript Sienna Roberson, Oct 28, 2019

  • 2 Answers
  • 0
    I found what the problem is. It turned out that the full address of the open page is passed to the url variable inside the pac script only if the site is opened using the http protocol. In the event that the site is opened using the https protocol, the browser cuts the path leaving only the domain:

    The URL being accessed. The path and query components of https: // URLs are stripped. In Chrome (versions 52 to 73), you can disable this by setting PacHttpsUrlStrippingEnabled to false in policy or by launching with the --unsafe-pac-url command-line flag (in Chrome 74, only the flag works, and from 75 onward , there is no way to disable path-stripping; as of Chrome 81, path-stripping does not apply to HTTP URLs, but there is interest in changing this behavior to match HTTPS); in Firefox, the preference is network.proxy.autoconfig_url.include_path.

  • 0
    You have a problem understanding reg. expressions and not with a function.

    That is, in theory, if the site address contains a unit at the end

    Your reg. the expression not only does not look for a unit at the end of the line, but is generally mistaken.

    In addition, you are looking in the url, in a non-IP address, you must pass the host through dnsResolve and the output will be the IP address of the server you are going to get to.
    Nathan Valentine

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!