Phantomjs check if javascript exists and is working - javascript

I am quite new to Phantomjs and am starting to getting to know how to use it. However all of the semi advanced tutorials does not cover the thing i want to use Phantomjs for.
Now my question is how would i check if a Javascript is active on the site and if it is working correcly (i.e not throwing erros in the console).
I hope someone is able to point me in the right direction or know how to do this.

you can interact with the open page using the webpage.evaluate method:
var page = require('webpage').create();
page.open('http://m.bing.com', function(status) {
var title = page.evaluate(function(s) {
//what you do here is done in the context of the page
//this console.log will appear in the virtual page console
console.log("test")
//here they are just returning the page title (tag passed as argument)
return document.querySelector(s).innerText;
//you are not required to return anything
}, 'title');
console.log(title);
phantom.exit(); //closes phantom, free memory!
});
in order to see the virtual page console, you have to add a onConsoleMessage callback:
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
EDIT: by default javascript is executed.

Related

WebView2 / CoreWebView2 - Detecting if a javascript error happens

I have written a web-crawler to test a pre-release website for errors and issues (i.e. missing content type, timeouts, exceptions, redirects).
This morning a colleague asked me to include a check if there was any javascript error on the page...
A pure javascript solution is unlikely -- i.e. detect all JS errors, using JS
What I appear to want to be able to do is capture the javascript console text, ideally via CoreWebView2
If the page logs each exception into an array,
Errors=[]
window.onerror = function (msg, url, line) {
Errors.push("Caught[via window.onerror]: '" + msg + "' from " + url + ":" + line);
return true;
};
Then you can just query the Errors and see both the count and the nature of the errors.
You can also push errors from try catch

Get ID from an after edited copy of a Google Doc with Google APP Script [duplicate]

This question already has an answer here:
Google app script only updates document's changes after finishing running. Can I force it to refresh before?
(1 answer)
Closed 2 years ago.
I have created a script that is triggered after button click on android app.
The script I've written gets a Google Docs Template and creates a copy of it to edit it afterwards.
After being edited i want to send an email to the email that will be inserted in the app.
The email should include the body, the subject and the file attached as pdf.
I've achieved to send the email with the pdf in it but it sends the copy of the Template without being edited.
Any suggestions to solve this problem?
Here's my code:
//Make a copy of the template file
var templateID = DriveApp.getFileById('My Template ID').makeCopy().getId();
//Rename The copied file
DriveApp.getFileById(templateID).setName(codigOperari + " - " + numero + " - " + operari + " - " + cliente + " - " + data);
//Get the document body as a variable
var doc = DocumentApp.openById(templateID);
var body = doc.getBody();
//Edit the copy of the template
body.replaceText('<<C>>', codigOperari);
body.replaceText('<<NUMPARTE>>', numero);
body.replaceText('<<FECHA>>', data);
body.replaceText('<<CLIENTE>>', cliente);
body.replaceText('<<CORREO>>', e.parameter.ETEmail);
body.replaceText('<<ORDENTRABAJO>>', e.parameter.ETMotiu);
body.replaceText('<<OPERARIO>>', operari);
//A lot of data more
//Message to be sent
var message = {
to: e.parameter.ETEmail,
subject: "Subject",
body: "This is a Test",
name: "Name",
attachments: [DocumentApp.getActiveDocument().getAs(MimeType.PDF)
.setName(codigOperari + " - " + numero + " - " + operari + " - " + cliente + " - " + data)]
}
//Send Email
MailApp.sendEmail(message);
I wrote you a comment as well, but I believe your issue is that you're sending the ActiveDocument and not the document that was edited.
DocumentApp.getActiveDocument() will only ever return the document the script is "anchored" to. If you open up a Google Doc or Spreadsheet, go to the Tools menu, and click Script Editor, you will be given an Apps Script file that is bound to your Google Doc. You can access the document directly in this Apps Script ONLY with DocumentApp.getActiveDocument(). Trying to edit this Google Doc from anywhere else will require the DocumentApp.openById() method.
My guess is that once you ran var body = DocumentApp.openById(templateID) you thought that you actually opened the document and activated it, which lead you to think that all you had to do was attach the "active document". This is wrong though, as you will only ever be attaching the source document. I'm assuming this is the template.
You already have the solution though. The document you're trying to send is stored in the doc variable. var doc = DocumentApp.openById() gives you a variable that points to that spreadsheet indefinitely. You can access this document through this variable at any point (you do so right afterwards with the var body = doc.getBody() statement.
Let me know if this fixed it!

Script Stopped working after PHP5 > PHP7.2?

I came across this problem recently that I have no idea what caused it. This script makes my wp custom taxonomy list act like a tree accordion menu, but this script broke - I had to find out through a member of the site.
I am too far ahead into updating the site to be able to turn back time. However 1 major thing I did was upgrade from PHP5 to PHP7.2 on the server so I am not sure if that broke it. Here is the script in question:
<script>
function addExpandCollapse(id, expandSym, collapseSym, accordion) {jQuery('#' + id + ' .expand').live('click', function() {
if (accordion==1) {
var theDiv = jQuery(this).parent().parent().find('span.collapse').parent().find('div');
jQuery(theDiv).hide('normal');
jQuery(this).parent().parent().find('span.collapse').removeClass('collapse').addClass('expand');
createCookie(theDiv.attr('id'), 0, 7);
}
jQuery('#' + id + ' .expand .sym').html(expandSym);
expandCat(this, expandSym, collapseSym);
return false;
});
jQuery('#' + id + ' .collapse').live('click', function() {
collapseCat(this, expandSym, collapseSym);
return false;
});
}
</script>
The error on Safari displays
TypeError: undefined is not a function (near '...jQuery('#' + id + ' .expand').live...')
But this error never occurred back then so I am not sure if the code is not PHP7.2 compliant. I am really new to PHP, and fairly a student to Javascript. Any advice?
Try change .live('click') to .on('click')

See all console messages in PhantomJS [duplicate]

This question already has an answer here:
PhantomJS not returning results
(1 answer)
Closed 6 years ago.
I have running
localhost === http://localhost:8000/app/templates/
and I would like to open it with PhantomJS so all procedures looks exactly like in real browser -> and in result i can see all the information that i normally see in browser console.
I couldn't find any information how to do it properly.
Untill now i have tried this pattern
var webPage = require('webpage');
var page = webPage.create();
page.open('http://localhost:8000/app/templates/
', function(status) {
});
but still I cannot see any console.logs which are used in js files which should be loaded together with get call to my localhost
To see the console log, register the onConsoleMessage callback:
var webPage = require('webpage');
var page = webPage.create();
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
http://phantomjs.org/api/webpage/handler/on-console-message.html

HTML chunk appears in DOM only when Firebug is running

I've been banging my head against this for the past few hours.
I'm building an HTML chunk with Javascript and appending it to the page's DOM. Problem is this works only if Firebug is running and disabling Firebug doesn't help. When Firebug isn't running the code gets generated but not appended to the DOM (it's not that it's invisible or something).
Safari and Chrome are immune to the problem.
What should I look into?
Following is the incriminated code: if it looks weird it's because it is, and i'm just now refactoring it from the its original german black magic style without comments to self-explanatory jquery
function create_button() {
var textblock = $('div#textblock2');
var token;
token = $(textblock).text();
token = token.split('=')[1];
//delete the text that we parsed to build the btn
textblock.text('');
var form = write_form();
var btn = write_submit_btn(token);
console.log('form: ' + form);
console.log('btn: ' + btn);
textblock.append(form);
textblock.append(btn);
console.log('textblock2 contents:' + textblock.html());
}
function write_form() {
return "<form name='Formular'></form>";
}
function write_submit_btn(token) {
var btn;
if ( token.match(/weiter/) != null )
btn = "<input type='button' name='naechsteFrage' value='weiter' onClick='load_next_question();' />";
else
btn = "<input type='button' name='naechsteFrage' value='zur" + String.fromCharCode(252) + "ck' onClick='load_prev_question()' />";
return btn;
}
create_button();
console.log('textblock2 contents:' + textblock.html()); needs to be removed ... as does
console.log('form: ' + form);
console.log('btn: ' + btn);
When Firebug is not up, Firefox does not have a console and the function terminates.
All Webkit-based browsers have a built-in console, and are thus immune to this problem. (IE and Opera, on the other hand, would also have this problem. [And Links too, but I don't think you are worried about that. ;-) ])
Any calls to "console" methods will fail if firebug is closed. That's the issue.
Although you should always remove console functions in a live environment, the following code will ensure that any console function being run does not give a javascript error when there is no console:
if(!window.console||!console.firebug){var names=["log","debug","info","warn","error","assert","dir","dirxml","group","groupEnd","time","timeEnd","count","trace","profile","profileEnd"];window.console={};for(var i=0;i<names.length;++i)window.console[names[i]]=function(){}}

Categories