How to replace the navigator object on the popup in CasperJS? - javascript

How can I replace the navigator object on the popup?
I can replace the navigator by using page.initialized callback, but it has no effect for popups.
In the PhantomJS, I can use something like this:
page.onPageCreated = function (newPage) {
newPage.onInitialized = function () {
newPage.evaluate(function() {
window.navigator = {/*some code*/}
});
}
};
How to do it on CasperJS?

CasperJS is built on top of PhantomJS. Provided it works in PhantomJS, then it will also work in CasperJS.
You can either directly access casper.page to run the same code as in PhantomJS or
You can use the popup.created event handler to do the same in CasperJS:
casper.on("popup.created", function (newPage) {
newPage.onInitialized = function () {
newPage.evaluate(function() {
window.navigator = {/*some code*/}
});
}
});

Related

change on the javascript function is not being reflected in the browser

I've difficult closing popup. window.close(); is working only with Chrome not IE. After doing some researches, I found this article which tells why I shouldn't use window.close() directly in the popup to close it.
//Global var to store a reference to the opened window
var openedWindow;
function openWindow() {
openedWindow = window.open('moreinfo.htm');
}
function closeOpenedWindow() {
openedWindow.close();
}
function myReload() {
location.reload();
}
The problem is that the first time I wrote the closeOpenWindow method, I forgot to put the () at the end, so it was openedWindow.close;
I've fixed the problem, but I'm still getting the same error on that line.
openedWindow.close is not a function
If I delete the the entire function closeOpenedWindow(), I get the error that closeOpenedWindow() is not a function. When I put it back, I get the previous error that openedWindow.close is not a function.
Thanks for helping
EDIT:
This is how I'm calling the function from the popupu:
function mycloseChild() {
window.opener.myReload();
window.opener.closeOpenedWindow();
}
var openedWindow = {
open: () => { window.open('moreinfo.htm') },
close: () => { window.close() }
}
Actually, this seems to work in Chrome Debugger. Maybe a timing issue?
//Global var to store a reference to the opened window
var openedWindow;
function openWindow() {
openedWindow = window.open('moreinfo.htm');
}
function closeOpenedWindow() {
openedWindow.close();
}
openWindow();
//openedWindow is new window
closeOpenedWindow();
//openedWindow closes
It looks like you can use window.opener from the openedWindow to get a reference to the original window object which has the method closeOpenedWindow. but in order for that to work, you need to add closeOpenedWindow to the original window like so
window['closeOpenedWindow'] = function() {
openedWindow.close();
}
and then call it from the opened window like so window.opener.closeOpenedWindow()
see also Can I pass a JavaScript variable to another browser window?

How to add back button event in Universal Windows App without using WinjS Library?

This is my main.js
(function () {
"use strict";
//No need of WinJS
var activation = Windows.ApplicationModel.Activation;
var roaming = Windows.Storage.ApplicationData.current.roamingSettings;
// For App Start Up
Windows.UI.WebUI.WebUIApplication.addEventListener("activated", function (args) {
if (args.detail[0].kind === activation.ActivationKind.launch) {
if (roaming.values["currentUri"]) {
if (roaming.values["UserName"])
{
localStorage.setItem("UserName", roaming.values["UserName"]);
window.location.href = roaming.values["currentUri"];
}
}
}
});
// For App Suspension
Windows.UI.WebUI.WebUIApplication.addEventListener("suspending", function (args) {
roaming.values["currentUri"] = window.location.href;
roaming.values["UserName"] = localStorage.getItem("UserName");
});
// For Resuming App
Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", function (args) {
var roam = Windows.Storage.ApplicationData.current.roamingSettings;
if (roam) {
if (roam.values["currentUri"]) {
localStorage.setItem("UserName", roam.values["UserName"]);
window.location.href = roam.values["currentUri"];
}
}
}, false);
// not working backpressed event
Windows.UI.WebUI.WebUIApplication.addEventListener("backpressed", function (args) {
// to do
}, false);})();
I need to add back key press event for windows phone without using winjs library?
Can anyone suggest me?
I am using ms-appx-web context in my app. I dont want to use winjs library.
I need to add back key press event for windows phone without using winjs library?
The backpressed event should be attached to Windows.Phone.UI.Input.HardwareButtons but not Windows.UI.WebUI.WebUIApplication.
If you refer to HardwareButtons.BackPressed and HardwareButtons, you will find the backpressed event is used like this:
var hardwareButtons = Windows.Phone.UI.Input.HardwareButtons;
function onBackPressed(eventArgs) { /* Your code */ }
// addEventListener syntax
hardwareButtons.addEventListener("backpressed", onBackPressed);
hardwareButtons.removeEventListener("backpressed", onBackPressed);
And since you are not making a Single Page Application. This event should be attached on every new page's JS codes.
Update: If you want to know your current device programmatically, you can use the following if-statement:
if (deviceInfo.operatingSystem.toLowerCase() == "windowsphone")
{
//do your windows phone logic
} else if (deviceInfo.operatingSystem.toLowerCase() == "windows")
{
//do your windows logic
}
I used this-
var flag = Windows.Foundation.Metadata.ApiInformation.isTypePresent("Windows.Phone.UI.Input.HardwareButtons");
if (flag) {
var hardwareButtons = Windows.Phone.UI.Input.HardwareButtons;
hardwareButtons.addEventListener("backpressed", onBackPressed);
}
It worked for me well!

How to inject html into iframe, and display inside jquery-ui dialog

Here I create a jqueryui dialog from an iframe and populate the iframe with some html.
In firefox this displays nothing until I add the alert. Is there a better way to convince firefox to draw the iframe?
http://jsfiddle.net/jtmx00f4/6/
function fancyDialog(htm) {
$('<iframe></iframe>').dialog({
open: function () {
//alert('presto!');
var doc = this.contentDocument || this.contentWindow.document;
doc.body.innerHTML = htm;
}
});
}
fancyDialog('<html><body><p>Hello</p></body></html>');
While #dandavis fiddle does work in firefox, I also found a more complete solution in this article which does not require setTimeout.
http://jsfiddle.net/jtmx00f4/9/
function fancyDialog(htm) {
$('<iframe></iframe>').dialog({
open: function () {
this.contentWindow.contents = htm;
this.src = 'javascript:window["contents"]';
}
});
}
fancyDialog('<html><body><p>Hello</p></body></html>');

Reload the page using sammy.js

I am using sammy.js for single page application in asp.net mvc. Everything is fine, but I am facing one problem which is that I can not reload the page. For example When I am in the dashboard my URL is
http://localhost:1834/#/Home/Index?lblbreadcum=Dashboard
layout.cshtml
<script>
$(function () {
var routing = new Routing('#Url.Content("~/")', '#page', 'welcome');
routing.init();
});
</script>
routing.js
var Routing = function (appRoot, contentSelector, defaultRoute) {
function getUrlFromHash(hash) {
var url = hash.replace('#/', '');
if (url === appRoot)
url = defaultRoute;
return url;
}
return {
init: function () {
Sammy(contentSelector, function () {
this.get(/\#\/(.*)/, function (context) {
var url = getUrlFromHash(context.path);
context.load(url).swap();
});
}).run('#/');
}
};
}
I want to reload the page by clicking the dashboard menu/link. But click event not firing because link is not changing. But if I want to go another page then it is fine. Please help me out. Thanks.
I think you have to append the same partial again. You can't "update" the partial in that meaning.
As you say in your post, when you click another link and then back again it works.
That's what you'll have to do. Append the same page/partial again, by doing that you clear all variables and recreate them, by that simulating a refresh.
EDIT: Added example
Observe that I didn't copy your code straight off but I think you'll understand :)
And I don't use hash (#) in my example.
var app = Sammy(function () {
this.get('/', function (context) {
// context is equalient to data.app in the custom bind example
// currentComponent('home'); I use components in my code but you should be able to swith to your implementation
var url = getUrlFromHash(context.path);
context.load(url).swap();
});
this.bind('mycustom-trigger', function (e, data) {
this.redirect('/'); // force redirect
});
this.get('/about', function (evt) {
// currentComponent('about'); I use components in my code but you should be able to swith to your implementation
var url = getUrlFromHash(context.path);
context.load(url).swap();
});
}).run();
// I did an easy example trigger here but I think you will need a trigger on your link-element. Mayby with a conditional check wheter or not to trigger the manually binding or not
$('.navbar-collapse').click(function () {
app.trigger('mycustom-trigger', app);
});
Please read more about events and routing in sammy.js
Good luck :)
An easier and cleaner way to force the route to reload is to call the Sammy.Application refresh() method:
import { sammyApp } from '../mySammyApp';
const url = `${mySearchRoute}/${encodeURIComponent(this.state.searchText)}`;
if (window.location.hash === url) {
sammyApp.refresh();
else {
window.location.hash = url;
}

How to test whether window.print() is not overriden?

How to test with (JavaScript and/or) FirefoxDriver whether the window.print() method is overridden?
It is possible to disable all print buttons on a page like with this:
window.print = function() { alert("Bazinga") }
With this a normal "Print" link like the following one is not working anymore:
<a onclick="window.print()">....</a>
I need to check whether a window.print() call executes the original print dialog.
Maybe it is possible to inject some JavaScript into the FirefoxWebdriver?
Since window.print is a native function you can:
if( window.print.toString().indexOf('[native code]') > -1 ) {
//native
}
I've created a fiddle based on Luca's answer and Christoph's comment. This function will detect native print dialog even in IE6.
function isNativePrint() {
var isNative = false;
try {
if (window.print.toString().indexOf('[native code]') > -1) isNative = true;
} catch (e) {
isNative = true;
}
return isNative;
}

Categories