PhantomJS: Going to another URL - phantomjs

I am using a call of page.open, and inside that callback I want to go to another URL. Is calling page.open inside page.open okay, or should I use window.location = url in page.evaluate? I tried calling page.open inside a page.open and the script didn't work, and I'm not sure why.

It is always better to use page.open() to navigate to another page instead of using window.location = url from a page.evaluate() call.
It is clearer what happens, because it is easier to read.
There is no way to register a callback when the next page is loaded when window.location is used. One would still need to register to page.onLoadFinished. This is not necessary, because it is already integrated into page.open().

Related

Nuxt.js: how to redirect inside methods

I'm learning Nuxt.js and I'm puzzled by how difficult it seems to be to simply redirect a user to another page from a method that is triggered via a click.
Here is the set up: I'm using Nuxt Auth to authenticate users, once authenticated I want them to be forwarded away from the signup page to another route. I already have middleware set up that redirect logged-in users, but it is only triggered when I refresh the page, not when I first log them in.
I have a method like this:
async login(event) {
event.preventDefault()
try {
await this.$auth.loginWith('local', this.loginData)
// this is where my redirect logic should go
} catch(error) { ... }
}
So far I've tried using this.$nuxt.refresh() which doesn't do anything at all and I've also tried this.$router.push('/route') which seems to hang the page completely. Ideally, I would prefer the refresh approach so that I don't have to specify the landing page for logged in users in multiple places, but I also need to know how to use redirections inside methods, I would have thought it should be the most simple operation imaginable and yet it seems to be difficult to find.
Any tips would be highly appreciated!
UPDATE:
I've found a solution, although it's not an ideal one. I've added an if-statement into beforeCreate that checks this.$auth.loggedIn and if it's true, then it calls this.$auth.redirect('home') when "home" is defined in nuxt.config.js under auth redirect. The reason this solution is not ideal is that it relies on the auth module (as opposed to being a general redirect mechanism).
There's a way in Nuxt to reload the page like so:
this.$router.go(0)
also you can find more information here
if this.$router is not working, try this
this.$nuxt.$options.router.push(url);

WKWebView - replace web action

Inside my app, I'm using the WKWebView to display a website. My goal is, when user is pressing a button on this website, I want to stop an action linked to this event and replace it with my own, natively made (custom action outside the WKWebView). I've been trying to search for any solution to fetch mentioned event but unsuccessful. What more came to my mind, if there is a way to fetch a JavaScript in WKWebView, I have a possibility to add some JS script code to this site (not to delete the action I want to block). Thank you for any help.
First, do you have a permission to mess with this web site's behaviour? I assume you do, otherwise it is likely illegal.
Second, try using Safari Web Inspector with a device/simulator, and use the DOM tree and console tools to find out what is the HTML/javascript that is involved with this action on this site.
If you can't find what happens in HTML/JS yourself, feel free to post a new separate question on SO with your target URL, some HTML/JS code, and which link/action you want to replace. Tag the question with "javascript" and ask if it is possible to write some javascript to replace that particular action to some custom JS code.
Usually there are 2 types of actions: either it is something that provokes AJAX calls to a server API triggered by an event handler, or it is a plain HTML link that results in a web navigation. For both cases it is possible to write a JS script that overrides the action.
Finally, use WKUserScript to inject javascript into the page, and override the action. Use window.webkit.messageHandlers to send an event from your custom action to the app side. Use WKScriptMessageHandler to process the event in the Objective-C or Swift code.
See an example here: http://nshipster.com/wkwebkit/

Safari Extension and getJSON Wikipedia

I am trying to implement a GetJSON Call to Wikipedia API from a Safari Extension, but i get always the Origin Error, if i add a callback=ß i get callback undefined Error
A simple:
$.getJSON('http://en.wikipedia.org/w/api.php?action=parse&page=google&prop=text&format=json&callback=?', function(data) {
console.log(data);
});
Without triggering an Error would be nice, alternatively, is there another way to get a Wikipedia short summary on a keyword inside safari Extension?
thx for help
Found a Workaround1
If you use a Globle Page or a PopOver, let it do this stuff, it doesn't trigger Orinin Errors, use than Messaging to send the Data from Global Page to the Injected Script, it works here fine

Safari extension: how to get current url from injected script?

I am trying to get the current url from injected script but it seems impossible because of the permission.
From the official document, I got:
Injected scripts cannot access the SafariApplication or SafariExtension classes.
Is there any solution or w/a? Thanks.
Use window.location.href to get the URL of the page the script is injected into.
Scripts are injected into each iframe as well as the main page, so you might want to use a check like if (window == window.top) to if you are only interested in the main page.

Rails: periodically_call_remote then redirect

I need to check some database values with a periodically_call_remote function.
I want to redirect if some values have already a certain state.
How to do this? redirect_to in the function does not seem to work.
Do you need to do a refresh on the entire page? You could use javascript to update your content.
The following javascript may work as well.
window.location = "http://www.address.com/"
redirect_to in the return function does not work because it redirects the ajax request to the new page, it doesn't tell the browser to redirect the entire page. You will need to either render back some javascript in the controller that causes the page to redirect, or better: add a callback to the periodically_call_remote call that checks the return value and runs the javascript that Ben suggested.
Check this thread:
Rails 3 equivalent for periodically_call_remote
The first answer to use javascript setInterval worked perfectly.