Make a button that saves html page in Chrome (locally) - javascript

I prefer Chrome, but I would like it to operate also in other browsers. So, how to make a simple button that will onClick call a function to save html page as html file? Pretty much the same like user presses Right click > Save As.
I have two buttons already, window.print() for printing and location.reload() for refreshing the page, and I must add that this html page is running locally, so there's no web server.
Is it possible with onClick JavaScript as two examples above?

I don't think this is possible, as this would likely be a security violation.
You mention specifically that you are running this locally. Does that mean you are trying to create some sort of application? If so, you might throw it in to something like Brackets Shell. If you put it in there, you can implement your own native function to trigger the "Save As" dialog. It'd be a bit more complicated, but probably one of the only ways to accomplish it if you really needed an on-page Save As button.

This is not currently possible using pure JS, but you can iterate through the document and export the contents to a file manually.
See this similar post: Export DOM

Related

Weird button behavior while scrapping bet365

I am trying to scrap odd data by Pyppeteer from bet365 follow their UI flow and need for help.
The pages are just like this.
https://www.bet365.com/#/AC/B1/C1/D8/E123540970/F3/
If the match is outdated, just get into one of the matches that not in-played from following.
https://www.bet365.com/#/AS/B1/
For the collapsed button named "Handicap Result" at the bottom of the page, I can normally use my mouse cursor to click on it and make it expand and retrieve its data.
But when I tried to use Pyppeteer's click or javascript to do it, that is querySelector() and click(), it just weirdly failed.
Observing html and xhr after click(), it definitely triggered something and tried to do something, but the content just failed to shown.
I tried to click on different coordinates, key Enter and Space, click with delay, and all failed.
Could someone tell me how could I expand it use javascript?
Was I dissipating wrong events?
Thanks.
This is probably one way they use to avoid scraping.
U tested with pure JS direct from DevTools?
Bet365 its really, really hard to scrape. They use Cloudflare blocking and protection system (including AI to detect autonomous agent like Puppeteer, even if you use stealth mode).

Need a method to capture javascript events in WinForm webbrowser control

I have a WinForms app that uses a .NET webbrowser control. What I need to do, is wire up an event on the WinForms side to fire when a value is set (via javascript) in the loaded HTML page. I had success doing this with an onclick event of a button, but I can't seem to get it to work with a custom event. I don't know if this is a limitation in what the browser control can attach to event wise.
So essentially, I need that when a JS function is called in the HTML page and sets a value of a hidden input element (or it could be a regular input that I style to be hidden), I need to know that in WinForms. If it helps, I am using browser flags in this application to emulate IE11 instead of the default IE9 engine. The HTML page loaded is also mine so I can modify it any way needed to make this work properly. I would just use the onclick events of the buttons, but this is a gmaps integration where there can be upwards of 2000 buttons generated (one per marker placed) so it seems like a huge waste of resources to wire up 2000 onclick events when any of those button clicks only modify 4 input fields with the data I care about.
This project happens to be in VB.NET, but C# solutions would be fine as well. They can be transcoded or if the solution uses C# specific features, we can move this to a separate DLL and reference it.
After spending a lot of time on this today, I found a solution. It isn't a direct solution to the problem I posted, but it got me to where I needed to be. I am still interested in an answer to the original problem if anyone has one, but for now, what I found I could do was to create a class in .NET that I could assign to the ScriptingObject of the browser control and then call window.external.myFunctionName, where myFunctionName is a function within the .NET class. This works great for my specific problem, but would not work if I didn't also control the HTML page I was consuming with the browser. That is why I am still interested in alternate solutions if anyone has one. Thanks.

How to stop scroll event function when the user move to another page

I am adding the scroll event in javascript for one of my pages. The code is like this:
document.getElementById("myProject").addEventListener("scroll", myFunction);
function myFunction() {
document.getElementById("scrollEvent").innerHTML = "These are all my projects so far";
}
So, when users start scrolling, they will see a text "These are all my projects so far".
My problem is how to stop showing this text when users move to another page.
Please help ( I am a verrrry fresh developer)
Thanks so much
A few thoughts.
Without knowing your dev environment (e.g. are you using MVC with a framework?), I will assume you are simply talking about separate/individual HTML pages.
Each HTML page can have its own javascript. Just like HTML and CSS, there is no need to have the same javascript functions on every page. (You don't have the same HTML content on every page, right?) Usually, we divide up the javascript into multiple files - some files are added to every page, some are specific to a certain page. It is easiest to have one (external) javascript file that you reference on every page, and then specific javascript code for each page - either in a second external file that is referenced, or on the HTML page inside <script>//js here</script> tags.
If the DIV with ID myProject is not on the other page, then the javascript won't do anything. However, it is not good to have broken javascript on a page, so make sure it is not included on other pages.
If you are using a framework, like CodeIgniter or ReactJS or Angular, please tell us so we can adjust our answers accordingly.
If the case is a switching between browser tabs, you can use two different events like below.
$(window).blur(function(e) {
// stop scroll event, when switching to another tab
document.getElementById("myProject").removeEventListener("scroll");
});
$(window).focus(function(e) {
// start scroll event
document.getElementById("myProject").addEventListener("scroll", myFunction);
});
I am not sure what you are actually looking for, because when user switch between tabs, he can not see the text anymore no matter there is a scroll event or not. If you are concern about performance, then the above solution would help.

How to simulate keypress in an iFrame using javascript or query

I am currently trying to write a script using javascript along side a bit of html and some css - but let's stick with Javascript as thats where, I think, my problem comes from...
In short what I want my program to do is these following points:
I have an iFrame in the main window of my program.
The iFrame is used to access a website (obviously duh :P)
There is a button outside of the iFrame.
That button will call a function when pressed by the user.
HERE'S MY PROBLEM:
I want that function to simulate the press of a keyboard key (in that case, a number key followed by the ENTER key twice.
The thing is, I need to 'simulate' these keypress inside the iFrame, so they interact with the website that is access through the iFrame.
In short, I can't seem to find a way to interact with the website in iFrame from outside of it. I would prefer a solution using pure JS but I'm fully aware that there would probably be a much simpler solution using Jquery. So I'm open to either one of them.
I don't think that pieces of my code would help as there is basically nothing coded so far, besides the main function and the basic layout of the page. I'm ready to redo the whole architecture of what I've done so far if needed.
Thanks for you help.
Yann

Can I use javascript to detect events of asp.net?

I am now trying to use gridview to create a web application for users to delete some rows from database. However, whenever I refresh the whole page, strange things happen as selectedIndexChanged was called. Is there any way that I can avoid this method being called whenever I refresh the table or is can I use javascript to detect this event? If not, can I generate a yes no dialog for that event like javascript? I am quite new to asp.net and my questions may be quite stupid. Please help!
I think you need to decide if you want to use JS or ASP to handle events on the page.
If you want to use JS, then disable server processing in your ASP tags and handle all the interactions with JS in your page and only process at the server on page submit. ASP can generate the grid for you on page load, but then let JS handle the client side interactions.
If you want to use ASP, then set your grid to process on the server (runat="server") and forget about JS.
Well seems I found out the solution. Actually C# also provides yes no dialog. Seems I could use it to manually stop that event but it seems that I cannot avoid selectedIndexChanged being called when refreshing the page. Thank you every one for your help.

Categories

Resources