How to debug headless Electron preload script failure? - debugging

I have an Electron app with code like
let win = new BrowserWindow({ show: false, frame: false, webPreferences: {preload: "/path/to/preload.js", ...}, ...});
and while developing new feature, I missed one closing parenthesis and one closing brace. Of course, I didn't know that and I instead got output to console while trying to launch the app
[18330:1204/142153.915989:INFO:CONSOLE(91)] "Unable to load preload script: /path/to/preload.js", source: electron/js2c/renderer_init.js (91)
[18330:1204/142153.916049:INFO:CONSOLE(91)] "SyntaxError: missing ) after argument list", source: electron/js2c/renderer_init.js (91)
For some reason, electron doesn't want to share the exception it actually receives while trying to load the preload script. For example, line number of the original parse error would be great! So I end up with no clue about the real cause barely on the log alone. Note that this issue is about problems with preload script contents - I know that some people are having problems with the preload script not being found by electron.
Also note that the second line in the log claims that error is on file electron/js2c/renderer_init.js on line 91 which is obviously only incorrect reporting from electron.
Is there a way to get the actual parse error to log? I'm trying to create headless app to be run with xvfb-run so I need to have output in the log.
I'm currently working with electron v10.0.1 but I'm willing to take hints for any version. Currently the only way forward is to use external linters or randomly comment out newly modified code blocks and try to figure out the problem manually. There must be a better way.
I'm already passing following command line flags to electron:
--enable-logging --v=2 --debug-print --noerrdialogs --headless

Related

SwiftUI previews error: Connecting to launched interactive agent

I received the above error when using the SwiftUI previews feature and can't figure out why this happens. The error always looks something like this:
GenericHumanReadableError: unexpected error occurred
messageRepliedWithError("Connecting to launched interactive agent 1894", Optional(Error Domain=com.apple.dt.xcodepreviews.service Code=17 "connectToPreviewHost: Failed to connect to 1894: (null)" UserInfo={NSLocalizedDescription=connectToPreviewHost: Failed to connect to 1894: (null)}))
I managed to figure out a good way to debug this, please see below
If you head into /Users/USERNAME/Library/Logs/DiagnosticReports you will see the latest crash reports. Open the most recent one (should start with your app name), and it should tell you the reason the app crashed. It'll look something like this:
Application Specific Information:
Fatal error: This request requires an authenticated account: file /Users/USERNAME/Work/AppName/Models/CloudKitAlbumManager.swift, line 101
In my case, it was a fatalError i threw in development for debugging. The previews loads your app and thus call your whole stack and will crash if you like me throws fatalErrors for debugging.
I hope this helps

Chrome DevTools Protocol addScriptToEvaluateOnNewDocument using ruby chrome_remote

So I am trying to inject a script to run on any page using addScriptToEvaluateOnNewDocument on chrome 79, but it doesn't seem to be working.
I am using the ruby gem chrome_remote, which gives a pretty basic access to the CDP.
Here is an example ruby:
scpt =<<EOF
window.THIS_WAS_SET = 1
EOF
ChromeRemote.client.send_cmd 'Page.addScriptToEvaluateOnNewDocument',{source: scpt}
ChromeRemote.client.send_cmd "Page.navigate", url: "http://localhost:4567/test"
I then start chrome with --remote-debugging-port=9222
The Page.addScriptToEvaluateOnNewDocument will always return {"identifier"=>"1"} (even if I call it multiple times, say with different scripts).
And when I open console on the opened tab in Chrome (which works, so I know CDP in general is working), and check the value of window.THIS_WAS_SET, it is undefined.
Is there any way to verify the command was sent to the browser, such as a log in the browser it was received? Any way to see what scripts were injected? Why does each call always return a ScriptIdentifier of 1, that seems problematic?
Anyone have a similar example working?
you should call "page.enable" first.

How Can I Fix Firefox Breaking My Site With Content-Security-Policy Warnings?

The site works fine on Chrome/iOS/Safari/Android (you should be able to select and image and proceed to write a message on the next step). Firefox refuses to run my project's main script (you can't select an image or go forward), and gives the following error in the console:
> Content Security Policy: Directive ‘frame-src’ has been deprecated.
> Please use directive ‘child-src’ instead. (3) Unknown
It's very cryptic. I've tried the following:
1) Adding a meta tag for the CSP in the header.
Result: Creates more errors if restrictive, same amount of errors if
left to wildcards on all parameters.
2) Locally serving all scripts.
Result: I still get three unknown CSP errors. It also loads a lot
slower since the dependencies are not being loaded from a CDN.
3) Removing specific scripts.
Result: It reduces the errors by up to one, but it seems all scripts
are equally responsible. Very strange behavior.
Is this a bug in Firefox that is unsolvable? I'm tearing my hair out over this.
I needed to put listeners in $(document).ready as Firefox loads things differently as pointed out to me by Matt Gibson.
Content Security Policy did not cause the script to fail. However, it is a weird error message that gives you no information to where the error is originating, and can potentially break the site, but that is not what happened here.

How can I log javascript errors with Poltergeist/Capybara/Rspec?

I'm using Rspec/Capybara with Poltergeist as a driver to write tests for some large web applications.
My issue is that I would like to record the messages that appear on the console, but so far I've been unable to do so.
I am aware of the options js_errors and phantomjs_logger, but I have had some issues with them:
if I set js_errors: false, the file I specify in phantomjs_logger stays empty;
if I set js_errors: true, console.log messages are logged in the file specified in phantomjs_logger, but then almost all my specs fail because of
javascript errors that may not even be relevant to the navigation example I'm testing.
Any idea on how I can save the console messages while not breaking specs on every js error?
CLARIFICATION:
I have no control over the development, my task is to check the stability of the whole stack of the applications in the various environments, accessing from the front-end, so clearing out all the javascript errors is out of the question. The specs I'm writing are also supposed to ignore javascript errors if they don't impair the usage of the interface.
You can't. The PhantomJS client catches javascript error messages and adds them to an array. Then when a command completes, if js_errors == true, that array is checked and if not empty the javascript errors are returned and trigger an error in the test. There is no other API in poltergeist for accessing those errors. It sounds like you need to have a discussion with your manager about the wisdom of just ignoring JS errors if they apparently don't impair usage - it's a potentially dangerous development practice

Ported Chrome extension to Firefox not working (indexedDB error)

I'm trying to port the Twitter Notifier extension to FF.
I'm not too familiar with JavaScript so I just used extension factory to convert it to FF.
Unfortunately it is not working; I don't get any HTML5 desktop notifications.
I've looked at the code a bit and I think the problem is with twitter.js which should be found in the path 'resources\extension-data\js'.
http://pastebin.com/C0NqU4Ur
First I've changed
twitter.messagesDB = window.webkitIndexedDB.open("messagesDB");
to
twitter.messagesDB = window.mozIndexedDB.open("messagesDB");
but it still is not working.
The error console also doesn't show any error.
When I put a console.log command to the twitter.messagesDB.onsuccess function nothing is being outputted to the error console.
Also no output when I create an onerror function.
So I though to surround the functions with a try and catch clause but no error is being thrown.
So I tried to put twitter.messagesDB = twitter.messagesDB.result; out of the function and the following error shows up in the error console:
Error: uncaught exception: [Exception... "A mutation operation was attempted on a database that did not allow mutations." code: "6" nsresult: "0x80660006 (NS_ERROR_DOM_INDEXEDDB_NOT_ALLOWED_ERR)" location: "chrome-extension://gjlehiopfilnaggndcmbhphaglkbkekf/js/twitter.js Line: 28"]
What does that mean?
How can I make it work in Firefox?
Thanks in advance!
Edit: I'm using Firefox 12 on Windows
This is due to an incompatibility between the Firefox implementation of IndexedDB, which supports the latest IndedexDB protocol published December 2011, and the Chrome implementation, which supports the version previous to that.
The breaking change is how databases are created, where the older (Chrome) implementation uses the setVersion transaction whereas the newer version (FF) uses an onupgradeneeded event and callback.
To see this change, please star this issue.

Resources