18

I am writing a custom .pac script for use with Firefox. Following numerous examples I've seen, I intersperse alert()s in order to debug it, but no alerts popup, even though the script is clearly being invoked. (I am clicking "Reload" in the "Connection settings" after each change to my script. I have even tried restarting Firefox.)

Are alerts supposed to work from PAC scripts? Maybe this is an IE-only feature?

Chris Noe
  • 36,411
  • 22
  • 71
  • 92

4 Answers4

17
  1. Use alert function in your .pac file.

    • In Firefox Browser:

      Tools -> Web Developer -> Browser Console (Ctrl+Shift+J) [This is not Web Console!!] -> Filter output: PAC-alert

    • In Chrome Browser:

      Go to chrome://net-internals/#events -> Search for a record with description: PAC_JAVASCRIPT_ALERT

      (About Chrome, thank this answer)


Sample .pac file:

function FindProxyForURL(url, host) {
    alert("url = " + url + " *** host = " + host + " *** Resolved IP = " + dnsResolve(host));

    return "DIRECT";
}
Mir-Ismaili
  • 13,974
  • 8
  • 82
  • 100
  • @claya The link is correct. But if you click on it, your browser tries to load an edited link, probably. Simply **copy it and paste** into your address bar. – Mir-Ismaili Aug 31 '17 at 18:55
  • 1
    chrome://net-internals/#events now displays a message to use chrome://net-export/ which is a file save then view process via a json file. Not great for real time diagnostics. – dBags Dec 08 '20 at 22:55
  • The only thing that worked for me (after trying Chrome, IE, Edge) was Firefox and the mentioned **Browser Console (Ctrl+Shift+J)** (August 2021) – Eugen Aug 05 '21 at 08:18
12

http://mxr.mozilla.org/mozilla-central/source/netwerk/base/src/nsProxyAutoConfig.js

The alert function is added to the sandbox:

80         // add predefined functions to pac
81         this._sandBox.importFunction(myIpAddress);
82         this._sandBox.importFunction(dnsResolve);
83         this._sandBox.importFunction(proxyAlert, "alert");

And the mapped function calls dump, which goes to the Error Console:

108 function proxyAlert(msg) {
109     msg = XPCSafeJSObjectWrapper(msg);
110     try {
111         // It would appear that the console service is threadsafe.
112         var cns = Components.classes["@mozilla.org/consoleservice;1"]
113                             .getService(Components.interfaces.nsIConsoleService);
114         cns.logStringMessage("PAC-alert: "+msg);
115     } catch (e) {
116         dump("PAC: proxyAlert ERROR: "+e+"\n");
117     }
benc
  • 1,381
  • 5
  • 31
  • 39
  • Interesting. I wonder why they unilaterally convert alerts to log dumps. I suppose popping alerts during such a low-level operation is considered unacceptable. – Chris Noe Jun 26 '09 at 16:19
  • Yeah. Its not in the spec, it's not in any PAC bug I ever found (I tested Proxy and PAC for Netscape/AOL/Mozilla). We could go digging in the changelog... – benc Jul 25 '09 at 06:03
  • 2
    It occurs to me that alert() probably requires a Window object, and being a network-level operation, there certainly is no Window object yet. There's not even a TCP connection yet, since the PAC script is helping decide how we're going to connect... – Chris Noe Aug 11 '09 at 20:23
  • Chris, you are probably right. The low-level implementation of PAC involves running javascript in a sandbox, worrying about how it relates to blocking activity like DNS lookups. Putting up alerts in a dialog box was probably just too much work. I would imagine IE has similar concerns. – benc Aug 12 '09 at 15:57
3

Ah Ha! The alert messages are getting logged to the console. I actually prefer that to alert popups anyway.

Chris Noe
  • 36,411
  • 22
  • 71
  • 92
0

You might need to disable "EnableAutoproxyResultCache" in the Windows registry . . .

  • 2
    According to http://support.microsoft.com/kb/271361, that's an IE feature. That key does not appear anywhere in my registry, and I doubt that it would affect Firefox. – Chris Noe Jun 24 '09 at 15:09