Essentially I'm trying to
navigate to a webpage
wait for that webpage to load
execute a JS function/alert/whatever on that page
all from a single bookmarklet. Is this possible? I can't seem to get onload to work for me, but that may be because of my own personal failings here.
The simplest way I found to do this without needing Greasemonkey or something similar is to write your JS so that it checks to see if it is on the appropriate page, and goes there if it isn't. If it is on the page, then it executes the JS/alert/whatever. You have to use the bookmarklet twice, but you just need one bookmarklet, and it may still be quicker/easier the user doing the clicking/whatevering him or herself. So the code would look like this:
if(this.document.location.href != "[url]") { //Are we on the page yet?
this.document.location.href = "[url]"; // If not, go there
}
else {
if (document.readyState === "complete") { //Wait for the page to finish loading
// DO STUFF
}
}
You want to install the Greasemonkey extension for Firefox. (or gm4ie for IE, or greasemetal for Chrome (PersonalizedWeb also works in a much simpler way for Chrome), greasekit for Safari, or user.js for Opera)
Greasemonkey lets you do exactly this... run a script automatically on every page load (you can choose what pages/sites it loads on)
Otherwise you will need to click your bookmarklet on every page load in order to run your script.
Given there's no better solution, I thought I'd toss out that Opera natively supports user scripts to run on every page load. From there, you could have the script check the current url, and run if on appropriate page.
See here for documentation
Another option is to call window.open(...), and use the window object to manipulate the window. It is also possible to navigate multiple pages this way.
Related
I want to inject some JavaScript code that loads a new page and then executes a function. But when it loads the page, it doesn't execute the rest of the code. I have seen on the internet, that when a new page is loaded or refreshed the JavaScript console is cleared. I have tried with a Chrome extension that injects the JavaScript code, and it doesn't work neither.
What can I do? Here is the JavaScript code:
var button = document.getElementById('skip_bu2tton'); // ID of the button
setTimeout(function(){
button.click();
alert("OK");
},12000);
window.open("**URL**","_self"); // URL opened in the same tab
There is some way to make the Chrome Extension Injector to make this automaticaly, so it must open the URL, wait a few seconds, click that button and repeat that process over and over again.
I think chrome local overrides might be just what you are looking for.
Here's the info how to use it: https://developers.google.com/web/updates/2018/01/devtools#overrides
I have a javascript file that was accidentally added to the admin side of our site. The javascript is below,
<script>
if (document.getElementById("errorTitle") != null && document.getElementById("errorTitle").innerHTML === "Insufficient Privileges") {
window.location.replace("/portal/InsufficientPrivileges");
} else {
window.location.replace("/portal/FileNotFound");
}
</script>
The problem is that this code runs on the admin pages so we are unable to remove it. If we disable javascript on the browser the page never renders, dynamic content. How can we disable this from running so we can upload the proper file?
You might be able to edit the page that contains the reference to the problem file. If you can just edit the page to jump over where that code is called with an if statement or goto.
If you can't edit the other pages then you can Use the debugger to change the code executed on the fly. Chrome and Firefox have debuggers that should be able to do this.
Specifically for Chrome you go into the object inspector (available via menus or right clicking on the page). Then you can see the HTML on the left window. You select the script tag of interest, you can right click and select delete or select "Edit HTML"
If the page redirects you before you're even able to edit anything, you can use automated tools.
Fiddler (Windows)
Fiddler lets you see all pages downloaded, and then you can have it send your browser a different page when it tries downloading any page you specify (AutoResponder feature). This way you can temporarily edit a page while you can fix it in the admin panel.
Greasemonkey (Firefox) or Tampermonkey (Chrome)
These plugins let you run JavaScript code on a page as soon as it gets to your browser. This will let you do things such as removing the script tag programmatically.
I'm attempting to use a QR code scanner plugin for a project I'm working on, basically I'm modifying the example posted below so that instead of just scanning the code and outputting the string value to the page, I actually want it to physically open the link using the InAppBrowser.
Now whilst the function I've added fires (as far as I can tell) the InAppBrowser doesn't get invoked, however if I click on a link pre-embedded in the index page after trying a scan, it briefly shows the page I had tried to load via scanning before then loading the contents of the pre-embedding link (if that makes sense).
Original Demo https://github.com/wildabeast/BarcodeDemo
My Fork https://github.com/desrat/BarcodeDemo
Any help would be appreciated.
EDIT: Jonus Solution works great, but what if I wanted to move the function out of the alert callback and just open the browser immediately?
I already tried re-placing the alert with
namedFunc(result.text);
and
function(){namedFunc(result.text);};
When you pass namedFunc(result.text) as callback, the function is invoked immediately and actually its result (undefined) is passed.
Try:
navigator.notification.alert(result.text, namedFunc.bind(null, result.text), 'Scan Result', 'ok')
Or:
navigator.notification.alert(result.text, function() {namedFunc(result.text);}, 'Scan Result', 'ok')
UPDATE:
Your second question is hard to answer. Using namedFunc(result.text); should be right. After some testing (with iOS) it seems to me, that the InAppBrowser is opened but not shown, because I can inspect the opened website with Safari. This is quite strange and I have no idea what the reason is. Maybe it has something to do with the closing barcode scanner.
However you can fix it by using a timeout:
window.setTimeout(namedFunc.bind(null, result.text), 1000);
or maybe you prefer:
window.setTimeout(function() {namedFunc(result.text);}, 1000);
This is surely not a really good solution because the user has to wait a second before the browser opens and I can't even guarantee that one second is always enough (e.g. on slower devices), so it's a bit risky.
Here is the code that i would want to play on one page, imgur in particular, this will click the next button in the gallery i was wondering if i would just have to make my own extension or what
function timeMsg()
{
var t=setTimeout("alertMsg()",3000);
}
function alertMsg()
{
alert("Hello");
}
You could go the way of chrome extensions, or greasemonkey scripts, its really comes down to you not 'doing this on the site' but manipulating your DOM in your browser.
Greasemonkey -- http://www.greasespot.net/
Chrome Extensions -- http://code.google.com/chrome/extensions/getstarted.html
A Chrome extension is probably your best bet. They're really easy to make.
Of course, you can always just paste that code into the console and hit Enter.
I have to use an online time clock for work. I made a bookmarklet that populates the form and submits it. It isn't running on their site, just fiddling with the DOM.
Test your javascript on their site using the developer console
Save a bookmark of the page
edit the newly created bookmark in the bookmark manager and change the URL to something like this
javscript:( your function )();
example
I am trying to use a really old page on a website I do not have control over (so I can't edit it's resources).
The problem is that it is redirecting the page via javascript (to a 'we don't support this browser' rubbish), via setting document.location before I can set any breakpoints to then debug/workaround.
Is it possible to break as soon as the DOM loads, preferably in chrome?
Disabling javascript stops the redirect, but chrome does not allow me to view any scripts to then place break points.
FireBug has a "Break On Next" feature. I'm not sure if it will work in your case, but it might be worth giving it a quick try: https://getfirebug.com/doc/breakpoints/demo.html#suspend
It seems like Chrome likes to do something to prevent you from seeing the code when you click the stop button before the page finishes loading. It'll say something like window.script123456738391=1;. That makes it so you can't set a breakpoint at the right spot inside the code, especially if there's a redirect on the page before you get a chance to pause it.
What I found you can do is set a breakpoint on that first line. Next time you load the page it will break on the very first line, regardless of what it is. Then you can see all the code the page would load and set breakpoints wherever you want :)