I want to fill the form and take capture of the generated page. So I wrote a CasperJS (This is my first time to use CasperJS) to archive to goal. I read the CasperJS API document but still not sure how to fix the problem.
Below is my code:
var casper = require('casper').create();
var filename = casper.cli.get(0);
var myear = casper.cli.get(1);
var mmon = casper.cli.get(2);
var stk_no = casper.cli.get(3);
casper.start('http://www.twse.com.tw/en/trading/exchange/STOCK_DAY/STOCK_DAYMAIN.php', function() {
this.fill("form[name='date_form']", {
'myear' : myear,
'mmon' : mmon,
'STK_NO': stk_no
},true);
});
casper.then(function() {
this.capture(filename);
this.echo("Saved screenshot of " + (this.getCurrentUrl()) + " to " + filename);
});
casper.run();
My command is like
casperjs test.js picture.png 2013 12 8271
Sometimes no picture file will be generated and it turns out to be failed:
Saved screenshot of http://www.twse.com.tw/en/trading/exchange/STOCK_DAY/STOCK_DAY.php to picture.png
Sometimes a picture file will be generated and it turns out to be successful:
Saved screenshot of http://www.twse.com.tw/en/trading/exchange/STOCK_DAY/genpage/Report201312/201312_F3_1_8_8271.php?STK_NO=8271&myear=2013&mmon=12 to picture.png
Why does sometime it work and give the capture but sometimes fail? Why the return value of this.getCurrentUrl() could differ?
When the current URL is http://www.twse.com.tw/en/trading/exchange/STOCK_DAY/STOCK_DAY.php, means you are blocked in the start page and waiting for the redirect, Change your
casper.then(function() {
this.capture(filename);
this.echo("Saved screenshot of " + (this.getCurrentUrl()) + " to " + filename);
});
to:
casper.waitForUrl(/Report/, function() {
this.capture(filename);
this.echo("Saved screenshot of " + (this.getCurrentUrl()) + " to " + filename);
});
/Report/ means "Report" in your current URL, so your are at the page data queried.
Related
I am trying to automate a bugzillametrics query. From a browser, If I do
http://10.x.x.x:8080/BugzillaMetrics1_3/#calculate_query,1.1.1, the query works fine and i can get the relevant graph from bugzillametrics. In order to automate this progress, I am using phantomjs. My phantomjs script is as follows:
var webPage = require('webpage');
var page = webPage.create();
var postBody1 = '';
page.open('http://10.x.x.x:8080/BugzillaMetrics1_3/#calculate_query,1.1.1 ',function(status) {
console.log('Status: ' + status);
var content = page.content;
console.log('Content: ' + content);
page.onError = function (msg, trace) {
console.log(msg);
trace.forEach(function(item) {
console.log(' ', item.file, ':', item.line);
});
};
phantom.exit();
});
If I run the script using "./phantomjs post1.js", the bugzillametrics does not create the necessary output. In fact, all i get is status = success. However, If I open the remote debugger and execute the same script using "__run()", I get correct results. What should I do different from the shell ?
I'm trying to write a JS Windows app. I have a button on the first page, and the click event handler code is as follows:
function pickSingleAudioFile(args) {
document.getElementById("output").innerText += "\n" + this.id + ": ";
// Create the picker object and set options
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.musicLibrary;
openPicker.fileTypeFilter.replaceAll([".mp3"]);
// Open the picker for the user to pick a file
openPicker.pickSingleFileAsync().then(function (file) {
if (file) {
// Application now has read/write access to the picked file
WinJS.log && WinJS.log("Picked file: " + file.name, "sample", "status");
document.getElementById("output").innerText += "You picked " + file.name + " from " + file.path;
// Save the file as an audio tag and load it
var audtag = document.createElement('audio');
audtag.setAttribute("id", "audtag");
audtag.setAttribute("controls", "true");
audtag.setAttribute("msAudioCategory", "backgroundcapablemedia");
audtag.setAttribute("src", "\"" + file.path + "\"");
document.getElementById("output").appendChild(audtag);
audtag.load();
} else {
// The picker was dismissed with no selected file
WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
}
});
}
The path is something like "D:\Songs\song1.mp3" or "\network-share\My Music\song name.mp3" I get the "Invalid Source" error when trying to load the file.
At first glance, this:
audtag.setAttribute("src", "\"" + file.path + "\"");
should instead be this:
audtag.setAttribute("src", file.path);
It's not clear why you are adding the backslashes. However, depending on what you are doing and based on samples I've seen, you'd be better off doing something like this:
var fileLocation = window.URL.createObjectURL(file, { oneTimeOnly: true });
audtag.setAttribute("src", fileLocation);
You might check out the "Playback Manager msAudioCategory Sample" from the Windows Dev Center for more ideas.
I'm trying to get all facebook comments with profile pictures of commentators through fb.api() call. Currently all comments gets outputed i just can't get profile pictures to append to appropriate comment.
In for loop which loops through all comments is another FB.api call which gets the profile pic of user who commented on video and than append them into single comment block. With this code i get profile pictures of all users who commented in every single comment.
What am i doing wrong?? Thank you in advance!
var komentarji_length = response.comments.data.length;
for (var i = 0; i < komentarji_length; i++) {
var user_id = response.comments.data[i].from.id;
FB.api("/" + user_id + "/picture", {
access_token: access_token
}, function (response) {
var profile_pic_link = response.data.url;
$(".comments_profile_pic" + i).append("<img src=" + comments_profile_pic_link + ">");
});
var ime = response.comments.data[i].from.name;
var message = response.comments.data[i].message;
$(".fb_comments").append('<div class="single_comment"><div class="comments_profile_pic' + i + '"></div><div class="name">' + name + '  says:</div><div class="single_message">' + message + '</div></div>');
}
Issue 1: comments_profile_pic_link is not defined, the variable is: profile_pic_link.
But then also the problem will not be solved. When you call "/" + user_id + "/picture" it is redirected to the image url automatically. So you can directly use the image url as: https://graph.facebook.com/{user-id}/picture. No need to make the API call unnecessarily.
Example
Still if you wish to get the proper image url (not required though), use redirect=0 with your call-
FB.api("/" + user_id + "/picture?redirect=0", {
access_token: access_token
}, function (response) {
var profile_pic_link = response.data.url;
$(".comments_profile_pic" + i).append("<img src=" + comments_profile_pic_link + ">");
});
I am developing a browser extension using crossrider.
I have added a context menu (background.js)
var ContextData;
appAPI.contextMenu.add("key1", "Send Data To Server", function (data) {
var ContextData = 'pageUrl: ' + data.pageUrl + '\r\n' +
'linkUrl: ' + data.linkUrl + '\r\n' +
'selectedText:' + data.selectedText + '\r\n' +
'srcUrl:' + data.srcUrl;
}, ["all"]);
On user click I want to send ContextData to extension.js. At extension.js some function will receive the data and send it to my server (A Rest API which will accept the data).
To send data to the server I have tested this and it works fine (code sample in extension.js)
appAPI.ready(function($) {
var dataToSend =="test data";
appAPI.request.post({
url: 'REST API URL',
postData: dataToSend,
onSuccess: function(response, additionalInfo) {
var details = {};
details.response = response;
},
onFailure: function(httpCode) {
// alert('POST:: Request failed. HTTP Code: ' + httpCode);
}
});
});
How can I write a function to accept ContextData from background.js and assign it to dataToSend in extension.js?
#Neel If I understand your requirements correctly, #Rob is essentially correct though a little clarification may help
By design/architecture, the extension.js code runs on each HTML page i.e. a separate extension.js instance is run for each URL that loads. In contrast, the context menu runs at the browser level (not HTML page) and is hence correctly coded in background.js file. However, the background.js code does not have direct access to the extension.js instance code running on the HTML page in the active tab and must therefore communicate the data via messaging. (For more information about scopes, see Scopes Overview)
Obviously, a user clicks the context menu item on the active tab (i.e. the page showing the HTML page being viewed); hence, once the ContextData string is created, you can use appAPI.message.toActiveTab to send the string to the extension.js instance running on the page/tab where the the context menu item was clicked.
This being the case, using your code example you can achieve this goal as follows:
background.js:
appAPI.ready(function($) {
var ContextData;
appAPI.contextMenu.add("key1", "Send Data To Server", function (data) {
var ContextData = 'pageUrl: ' + data.pageUrl + '\r\n' +
'linkUrl: ' + data.linkUrl + '\r\n' +
'selectedText:' + data.selectedText + '\r\n' +
'srcUrl:' + data.srcUrl;
appAPI.message.toActiveTab({type:'dataToSend', data: ContextData});
}, ["all"]);
});
extension.js:
appAPI.ready(function($) {
var dataToSend =="test data";
appAPI.message.addListener(function(msg) {
if (msg.type === 'dataToSend') {
appAPI.request.post({
url: 'REST API URL',
postData: dataToSend,
onSuccess: function(response, additionalInfo) {
var details = {};
details.response = response;
},
onFailure: function(httpCode) {
// alert('POST:: Request failed. HTTP Code: ' + httpCode);
}
});
}
});
});
[Disclaimer: I am a Crossrider employee]
I am not sure why but when I do the following
<img id="buttons" src="https://qikout.com/apppics/MerchantPromo.png" style="margin:1% auto; width:99% !important;" onclick="openextlink('https://qikout.com');"/>
function openextlink(url)
{
var ref = window.open(url, '_blank', 'location=no');
//window.plugins.childBrowser.showWebPage(url, { showLocationBar: true });
}
it works fine. But when I want to run the openextlink from within another function it does not open the window, instead it loads the page but just does not show it to the user.
for e.g.
function scan(){
window.plugins.barcodeScanner.scan( function(result) {
if(result.format != "QR_CODE")
{
alert("That is not a valid google Tag");
}
else
{
n = result.text.split(".com/");
loadpet();
//alert("We got a barcode\n" +
// "Result: " + result.text + "\n" +
// "Format: " + result.format + "\n" +
// "Cancelled: " + result.cancelled);
}
}, function(error) {
alert("Scanning failed: " + error);
}
);
}
function loadpet(){
$("#buttons").trigger("click");
var link = "http://www.google.com/" + n[1];
openextlink(link);
}
it works via
<div onclick="scan();" class="menu-right"></div>
Basically it scans fine, it gets the result it moves the result to the loadpet function and even sends it to the openextlink function however does not popup the viewer.
Your window.open code looks right (based on how InAppBrowser works as of Cordova 3.0).
Can you confirm all of the following?
The InAppBrowser plugin has an entry in your /#myapp#/platforms/#platform#/www/cordova_plugins.js file?
The InAppBrowser plugin folder is in /#myapp#/platforms/#platform#/plugins/
The related SRC files for InAppBrowser are in the platform folder as well (for example IOS plugin src files are: /#myapp#/platforms/#platform#/#appname#/Plugins/)
Also if all the above are confirmed but still no luck you might try appending a console.log('inappbrowser.js file has loaded!') line to the end of your inappbrowser.js file to confirm it is indeed loading (assuming you have setup the debug console and can view the output).