Can somebody tell me what is wrong with my syntax? While running this code in trifle JS it tells me that
. is unexpected at (2,4).
var page = require('webpage').create(),
page.open("http://www.phantomjs.org", function(status) {
if ( status === "success" ) {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
console.log(page.evaluate(function() {
return $("#intro").text();
}));
phantom.exit();
});
}
});
The problem is the ; at the end of the first line. This assumes that you define a variable in the next line which you don't do anymore. Exchange it with a semicolon:
var page = require('webpage').create();
page.open("http://www.phantomjs.org", function(status) {
//...
});
Related
I was using the below google app script to delete emails automatically from my trash folder. for some reason, this is no longer deleting the emails however the trigger dashboard shows the script executed successfully. can someone help to modify the script ? thanks in advance.
function removeMyTest2() {
var mymail = "me";
var mylabel = "del";
var permanentlyRemoveMyLabel = true;
var pageToken;
do {
var threadList = Gmail.Users.Threads.list('me', {
q: 'in:trash' + mylabel,
pageToken: pageToken
});
if (threadList.threads && threadList.threads.length > 0) {
threadList.threads.forEach(function(thread) {
Logger.log('id: %s snippet: %s', thread.id, thread.snippet);
if (permanentlyRemoveMyLabel) {
Gmail.Users.Threads.remove(mymail, thread.id);
Logger.log('id: %s snippet: %s REMOVED', thread.id, thread.snippet);
}
});
}
pageToken = threadList.nextPageToken;
} while (pageToken);
}
Try this:
I believe you were missing the label key.
function removeMyTest2() {
var pageToken=null;
do {
var threadList=Gmail.Users.Threads.list('me', {q:'in:trash label:del',pageToken:pageToken});
if (threadList.threads && threadList.threads.length>0) {
threadList.threads.forEach(function(thread) {
Gmail.Users.Threads.remove("me", thread.id);
});
}
pageToken=threadList.nextPageToken;
} while (pageToken);
}
I tested this with another label and it works fine
I needed this scope: https://mail.google.com/
My goal is to inject:
javascript document.getElementById("pickupZip").value = "90049";
document.getElementById("refreshStoresForZipPop").click();
into a website with PhantomJS. This is all of the code on my inject.js file, and I get this in the console: SyntaxError: Expected an identifier but found "document" instead, so I know it is the wrong syntax, though I can't find a place that shows what the correct is. Here is my code for the PhantomJS file:
var webPage = require('webpage');
var page = webPage.create();
page.open('http://shop.advanceautoparts.com/p/purolator-classic-air-filter-a24278/5792304-P?navigationPath=L1*14934/', function(status) {
var success = phantom.injectJs('inject.js');
console.log(success);
if (status === "success") {
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {
page.render('advanceautoparts.png');
phantom.exit();
});
}
});
Use evaluate.
var webPage = require('webpage');
var page = webPage.create();
page.open('http://shop.advanceautoparts.com/p/purolator-classic-air-filter-a24278/5792304-P?navigationPath=L1*14934/', function(status) {
var success = phantom.injectJs('inject.js');
console.log(success);
if (status === "success") {
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {
page.evaluate(function(s) {
document.getElementById("pickupZip").value = "90049";
document.getElementById("refreshStoresForZipPop").click();
});
page.render('advanceautoparts.png');
phantom.exit();
});
}
});
Reference: http://phantomjs.org/api/webpage/method/evaluate.html
In the following example I am able to pass JSON representation of the country list and display it but I cannot display the message "Could not find any countries". Could you please check the code below:
Controller:
if ($this->model_abc->did_get_country_list($user_id)) {
$country["results"]= $this->model_abc->did_get_country_list($user_id);
echo json_encode($country);
}
else {
$country = array('message' => "Could not find any countries" );
echo json_encode($country);
}
JS file:
$.post('cont/get_country_list', function (country) {
$.each(country.results, function(i, res) {
var item = $('<div>'),
title = $('<h3>');
title.text(res);
item.append(title);
item.appendTo($("#country_list_is_got"));
});
},
"json").error(function() {
$( "#country_list_is_not_got").text(country.message);
});
Update:
I changed my code as following and now I get thise error from console: Uncaught TypeError: Cannot read property 'length' of undefined.
JS file:
$.post('cont/get_country_list', function (country) {
if (country.message !== undefined) {
$( "#country_list_is_not_got").text(country.message);
} else {
$.each(country.results, function(i, res) {
var item = $('<div>'),
title = $('<h3>');
title.text(res);
item.append(title);
item.appendTo($("#country_list_is_got"));
});
}
});
The error-function is only called when the server responses with a 500-HTTP code (or 404, 403 or a timeout etc.). You are returning just valid JSON with a normal HTTP status-code, so the success-function is called. Within that success-function you should check if there was a message or not:
$.post('cont/get_country_list', function (data) {
if (data.message !== undefined) {
$( "#country_list_is_not_got").text(data.message);
} else {
$.each(country.results, function(i, res) {
var item = $('<div>'),
title = $('<h3>');
title.text(res);
item.append(title);
item.appendTo($("#country_list_is_got"));
});
}
});
You're not setting an error reply code in the case where no countries are found, so it's not treated as an error by jQuery.
else {
header("HTTP/1.1 500 Error");
$country = array('message' => "Could not find any countries" );
echo json_encode($country);
}
Also, your error function needs to get the data from its parameter:
.error(function(xhr) {
country = $.parseJSON(xhr.responseText);
$( "#country_list_is_not_got").text(country.message);
});
This webpage (test.html):
<p id="test">bla</p>
And this js code:
var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
console.log("Message: "+msg);
};
page.open("http://localhost/test.html", function(status) {
if ( status === "success" ) {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
console.log($("#test").text());
});
phantom.exit();
});
}
});
Result:
Message:
Does anybody know Why it doesn't display the text content of p when I run it with phantomjs?
try to use this one:
console.log($("#test").val());
In my extension's content script, I request data from background.js like so:
fireOnNewTopic (); // Initial run on cold start or full reload.
window.addEventListener ("hashchange", fireOnNewTopic, false);
function fireOnNewTopic () {
/*-- For the pages we want, location.hash will contain values
like: "#!newtopic/{group title}"
*/
if (location.hash) {
var locHashParts = location.hash.split ('/');
if (locHashParts.length > 1 && locHashParts[0] == '#!newtopic') {
var subjectStr = '';
var bodyStr = '';
switch (locHashParts[1]) {
case 'opencomments-site-discussions':
chrome.extension.sendMessage({name:"domain"},
function(response)
{
subjectStr = response.domain;
});
chrome.extension.sendMessage({name:"url"},
function(response)
{
bodyStr = "URL of last page visited: " + response.url;
});
break;
default:
break;
}
if (subjectStr && bodyStr) {
runPayloadCode (subjectStr, bodyStr);
}
}
}
}
Unfortunately, since sendMessage() runs asynchronously with the callback, at the time the code reaches runPayloadCode(), subjectStr and bodyStr are still null, since the code in background.js hasn't completed. What's the best way to synchronize the code so that subjectStr and bodyStr are filled in by the time runPayloadCode() is called?
To elaborate on what Sudarshan said about combining the two requests into one (sorry, need code so couldn't just comment) here's what you could do...
send
chrome.extension.sendMessage({url: true, domain:true}, function(response) {
console.debug('The url is "'+response.url+'" and the domain is"'+response.domain+'"');
if (repsone.url && response.domain) {
runPayloadCode (subjectStr, "URL of last page visited: " + response.domain);
}
});
or, if you wanted it more like your case way for some reason, maybe this is the sorta thing you'd like...
if(location.hash) {
var locHashParts = location.hash.split('/');
if(locHashParts.length > 1 && locHashParts[0] == '#!newtopic') {
var subjectStr = '';
var bodyStr = '';
var request = {};
switch(locHashParts[1]) {
case 'opencomments-site-discussions':
request = {
url: true,
domain: true
}
break;
default:
break;
}
chrome.extension.sendMessage(request, function(response) {
console.debug('The url is "' + response.url + '" and the domain is"' + response.domain + '"');
if(repsone.url && response.domain) {
runPayloadCode(subjectStr, "URL of last page visited: " + response.domain);
}
});
}
}
}
listen
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
var response = {};
if(request.url) {
response.url = "someURL";
}
if(request.domain) {
response.domain = "someDomain";
}
sendResponse(response);
});
You can try the following code:
case 'opencomments-site-discussions':
chrome.extension.sendMessage({
name: "domain"
},
function (response) {
subjectStr = response.domain;
if(subjectStr){
chrome.extension.sendMessage({
name: "url"
},
function (response) {
bodyStr = "URL of last page visited: " + response.url;
if (bodyStr) {
runPayloadCode(subjectStr, bodyStr);
}
});
}
});
However, can't you merge both message's chrome.extension.sendMessage({name: "url"}) and chrome.extension.sendMessage({name: "domain"}) into single message(Because i see them independent) and eliminate multiple nesting?