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
Related
I need to open "File Browse" item ("P50_BLOB") on page load.
I tried clicking the item with dynamic action on page load with javascript:
$('#P50_BLOB').click();
it didn't work, though it does work using the console.
I have also tried using async / wait / promise / wait for document to load.
I even tried to do it with another apex item that will make that click when it is changed, and manually changing the item is working, but on page load it's not.
This is supposed to be very simple but nothing works.
Thanks.
If you try that with the developer tools open in Chrome you will see an error:
Googling that led to this SO question:
File chooser dialog can only be shown with a user activation error while using web scraping through Javascript
What I am trying to do is pretty simple though I can`t figure it out.
So imagine you browsing a particular website and every time you switch to a new page the source code of html is changing.
I want to popup an alert message if a particular element is found on that page, but if the element is not found I keep browsing, and as soon as this element appears on another page it shows an alert again.
So I`ve got something like this:
function elementCheck(){
if (document.getElementsByName("project_test") {
alert("This page is tested");
}
else {
//keep browsing until this element is found
}
}
You'll need an Userscript for that, ran by TamperMonkey/GreaseMonkey. You just have to invoke the function you already have in the UserScript and that's it.
If the page you're browsing loads HTML asynchronously, via AJAX(i.e. the source code changes without changing the URL), you need to call your function periodically via setInterval.
I making this bot in JavaScript in the chrome console. And one of the lines in my script is window.open('thewebsite','_self') so it opens a different website in the same window. However I cant seem to execute code on that new website that I opened with window.open(). For example I want to do document.getElementById().click() however its not clicking on the new website I made.
For example:
If I was on google.com the script would open googleimages. But I want the next line of code for example typing in the search bar to happen on google images.
window.open refreshes console, so you can't execute any code after.
You could try to get target url content and replace first page content, but you'll be blocked with Access-Control-Allow-Origin
What you're trying to achieve is indeed a macro. You should take a look at iMacros and see how they chain instructions from page to page.
I am trying to create a bookmarklet that follows this process:
From any page, click the bookmarklet
load a specific page.
Trigger an input button which causes page to reload
Once reloaded; open a new page; in this case a social media page.
This is what I've written so far:
if (!($ = window.jQuery)) {
// Inject jQuery to make life easier
script = document.createElement( 'script' );
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
script.onload=SocialCredentials;
document.body.appendChild(script);
}
else {
SocialCredentials();
}
function SocialCredentials(){
$('input:submit').trigger('click');
window.open('http://www.facebook.com', '_blank');
}
The button submit click causes the page to process and clear the credentials of the user so they can visit social sites.
The above code works, but I want to make sure the page finishes loading before opening a new social media page.
Do I need to add some kind of wait() function? If I do add a wait method, will it kill the JavaScript and not open the new window? I'm fairly new to JavaScript, so I'm not familiar with these types of mechanics.
Edit:I should note that I don't have access or control to the page this bookmarklet will work on so I can't alter it's flow. What I'm trying to do is more of a favor for another department as a quick fix until they can make changes on their end.
Edit2: Updated the process.
If a page reloads, any code currently running on that page, including code from a bookmarklet, is ended and removed. Traditionally bookmarklet code ceases to work after a page load and user clicks it again.
There are three workarounds that I know of.
A.) Change the process that loads the page to instead use AJAX.
B.) Change the process that loads the page to instead open a new window, and then use JavaScript to manipulate the new window.
C.) Before triggering the page load, open a new child window and insert code into it. The code in that child window can then monitor its parent and take actions on the parent even after the parent has reloaded.
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.