Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS errors : calling native console.log() failed #482

Closed
gmetais opened this issue Jan 28, 2015 · 6 comments
Closed

JS errors : calling native console.log() failed #482

gmetais opened this issue Jan 28, 2015 · 6 comments
Labels
Milestone

Comments

@gmetais
Copy link
Contributor

gmetais commented Jan 28, 2015

Hi,

Phantomas generates many errors when running on this page: http://www.lafermeduweb.net
The errors seems to be internal, but i've been struggling for 2 hours looking for the reason.

Here is a part of the log in verbose mode:

15:49:52.925 JS error: Error: phantomas: calling native console.log() failed ("TypeError: '["DOMqueriesByTagName","%s (in %s)","head","#document"]' is not a valid argument for 'Function.prototype.apply' (evaluating 'this.addOffender.apply')")!
15:49:52.925 Event jserror emitted
15:49:52.925 Error: phantomas: calling native console.log() failed ("TypeError: '["DOMqueriesByTagName","%s (in %s)","head","#document"]' is not a valid argument for 'Function.prototype.apply' (evaluating 'this.addOffender.apply')")!
15:49:52.926 Backtrace: sendMsg(): /Users/gaelmetais/Documents/GitHub/phantomas/core/scope.js @ 127 / addOffender(): /Users/gaelmetais/Documents/GitHub/phantomas/core/scope.js @ 157 / selectorTagNameSpy(): phantomjs://webpage.evaluate() @ 35 / unknown fn: /Users/gaelmetais/Documents/GitHub/phantomas/core/scope.js @ 210 / unknown fn: http://static.lafermeduweb.net/js/fdw-min.js @ 1 / unknown fn: http://static.lafermeduweb.net/js/fdw-min.js @ 1

Any idea why it is failing?

@gmetais
Copy link
Contributor Author

gmetais commented May 26, 2015

I managed to understand the problem. It's when a website (or a JS library) overrides some JSON related functions.

  • Mootools 1.2 overrides the window.JSON object, JSON.stringify and JSON.parse don't exist anymore. It's fine in scope.js because the stringify method is saved. But it's not in some other modules (example).
  • Prototype 1.6 (and Mootools 1.2 too) creates an Array.prototype.toJSON function which get used by JSON.stringify (stackoverflow).

These problems got fixed in later releases.

Some example websites:
www.lafermeduweb.net uses Mootools 1.2.0
www.ratp.fr uses Prototype 1.6.1

Not an easy bug to fix. Maybe the cleanest way would be to add some Phantomas.JSONstringify and Phantomas.JSONparse functions which do not rely on Array/Object/Hash/String.prototype.toJSON.

@macbre
Copy link
Owner

macbre commented May 26, 2015

@gmetais, thanks for investigating.

phantomas already copies the native console.log before site JS can modify it. The simple fix here is to do the same for JSON related functions.

@macbre macbre modified the milestones: v1.11, Roadmap May 26, 2015
@gmetais
Copy link
Contributor Author

gmetais commented May 26, 2015

Be careful, there will still be a problem with:

Array.prototype.toJSON
Object.prototype.toJSON
Hash.prototype.toJSON
String.prototype.toJSON

By default, they don't exist in the browser. But the JSON.stringify function will look for them and use them if they get defined.

That's the touchy part of this fix.

macbre added a commit that referenced this issue May 30, 2015
@macbre
Copy link
Owner

macbre commented May 30, 2015

Error: phantomas: calling native console.log() failed fixed in a9b5a9d.

However, there's still TypeError: 'undefined' is not a function (evaluating 'JSON.parse("{\"svgResources\":[]}")') caused by JSON.parse being removed - patched in 84e8af9

@gmetais
Copy link
Contributor Author

gmetais commented Jun 15, 2015

Awesome, thank you!

@gmetais
Copy link
Contributor Author

gmetais commented Dec 22, 2015

For your information, this bug is still occurring on https://www.washingtonpost.com

For the moment I can't find the reason :(

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

No branches or pull requests

2 participants