How to create auto-executing script for Chrome console - javascript

The idea is that when I open a certain site in Chrome using a desktop shortcut, I want a certain script to be automatically executed via the console. It is desirable that it starts executing when the page loads. This can be done? Through the properties of the shortcut or maybe a .bat file?
I want to make a shortcut for a quick reboot of the router, which, after opening the settings page, will log into the account and click on the necessary buttons to reboot. In manual mode, everything works, but I don’t know how to automate.

You can create your own chrome extension that does certain actions depending on what happens in the browser, it is actually quite easy to do and there are multiple guides out there on how to do it.
I will shamelessly plug my own guide on how to create one with react, be aware there are many other and probably better guides out there:
Guide

Related

Minimizing apps via nodejs || plain js

I'm currently working on personal automation project and I want to add 'minimize app' feature.
For example: When I click button, I want to minimize the current browser. I already have window.close(); but this will close the app definitly. I found things like window.minimalize(); or window.minimize();, but none of them worked for me. So is there a way to minimize app?
Edited: It does not have to be only client-side JS, it can be used as terminal based nodejs app. Ex: I type minimize Google Chrome, and it will minimize it.
One Google search for how to minimize browser gives the following result:
There is no way to minimize the browser window within javascript. No, there isn't. However, depending on what you're doing and which browsers you're targeting, you could play around with the blur and focus events of the window to achieve similar effect.
Alternatively, if such control is required, you could always port your code to Electron.js or Neutrino.js, which were made to let you create desktop applications using JavaScript.
Maybe if you post some code and give us a train of thought to follow, we could work something out.

How to run your own javascript in google chrome

So there's an online quiz which uses flash, and my friend claims he has a script which pulls the answers from the flash app and displays them. I want to test this out, but I have a question:
How can I add or run this script to chrome so that this happens? In general, how can you run your javascript on a certain site on chrome? This concept interests me, and I didn't know it was possible to force sites to do stuff.
You can open the developers tools by pressing your F12 key or Ctrl+Shift+i. You can also right click on any element in the page and select "inspect element".
Once the developers tools are open, you can navigate to the right most tab called "console". There you can enter arbitrary JavaScript code to be executed on the currently open page.
Be careful what you paste into that console though! Someone might give you some malicious code to run. When you open the console while you browse Facebook, they actually display this warning:
Facebook is just an example. The same warning is applicable for any site.
It's quite a broad question. However. there are 3 main answers:
For one-time fiddling, you have the power of Dev Tools at your fingertips. You can run code in the context of the page, examine and debug its own code, make realtime modifications to the DOM etc.
To make a script execute every time you load some page, the traditionally used approach is Userscripts, a notion that came from Firefox. Google Chrome is able (or at least used used to be able) to handle them directly, but now most people use an extension, TamperMonkey, that runs userscripts for you.
Finally, if you need more powerful tools for interacting with your browser, you have Chrome Extensions. They are, basically, JavaScript scripts empowered with access to Chrome APIs. In this context, JS code injected into pages is called Content Scripts.

automate javascript clicks within code

I want to code some script (language is not a problem), which will open a site in webbrowser or webbrowser control. but the problem I am facing is that some sites have javascript alerts/confirm (ok/cancel) buttons appear during start or if some error occurs. I don't want the user to click these buttons manually, but write some code which clicks these messages. is there any way to do so?
EDIT:
I have requirement from a client to create a new desktop application or plugin for browser which will send OK message for all confirm or alerts. Please note that I don't have access to code for these websites user want to visit. Hope this will make it more clear.
Depends what are your exact requirements you could check something like Selenium, which is a free and powerful UI automation tool.
If you can be bound to exactly one browser, you could maybe take advantage of augmented browsing tools like greasemonkey.
Although I don't know if this satisfies your requirements - I have a feeling that you require this for mobile, since you are mentioning webbrowser control. But I hope it's a good start point for figuring it out further.

IE settings through Javascript or jquery

Is there any possibility to set the Internet Explorer settings by running Javascript file..?
I want to set the following settings in IE through javascript/Jquery
Go to, ‘Tools’ -> ’Internet options’.
Under ‘General’ tab, click the ‘Settings’ button in the ‘Browsing History’ section.
Choose the radio button "Every time I visit the webpage".
Click ‘OK’ and restart the IE.
This is not possible - it'd be a bit of a security hole if sites were able to do this...
Firstly, the short answer is no: You can't do what you're asking for.
Javascript within the browser is heavily restricted to only being able to access resources for the actual page being viewed. This is an important security feature.
You cannot access other pages or other tabs. You cannot access parts of the browser UI outside of the page itself. You cannot run external programs.
Even if you could, the way you've described it wouldn't work anyway: The settings page you've described is specific to one particular version of IE. So your hypothetical program wouldn't work in any other browser or even any other version of IE.
However, more importantly, you need to ask yourself why you're asking for this.
The thing is that you're trying to solve a problem with your site. You've found something that resolves it, and you've asked a question about how to automate that. But you need to work backward a bit -- stop trying to work out how to automate this particular solution: you need to ask yourself whether there might be a different way of solving the original problem, which could be automated.
The fact is this: the config setting you're trying to change is for handling how the browser deals with caching of files. There are ways of changing the behaviour of caching that can be scripted by your site. You might want to look at questions like this one for example.
The lesson here is this: Don't simply ask how to do something; explain why you're trying to do it as well. There might be an alternative solution that's better than the one you've thought of.
No, it's not possible to do using JavaScript.
This is not what Javascript designed to do. There is only a way to read browser setting from JS - using signed scripts.

Understanding Firefox extension structure

I'm trying to write a Firefox extension that intercepts a certain HTTP request and return static content without the request making it to the actual server (similar to AdBlock).
I've looked up the tutorials and I've got a basic file layout. I've also worked out that I need to use the nsITraceableChannel API and add an observer to do what I want and I have example code for that.
Problem is, where do I actually put this code? And when is my extension actually loaded and executed? Is it running constantly and asynchronously in the background or is it loaded per page view?
The documentation doesn't seem very clear on this. This extension won't need a GUI so I don't need the layouting XUL files (or do I?). I tried writing some XPCOM (I don't think I did it right though) component, registered it in chrome.manifest but it doesn't seem to run.
Can anyone explain exactly how the Firefox extensions work and where should I put my actual JavaScript code to monitor requests? Or have I got the whole idea of what an extension is wrong? Is there a difference between add-ons, extensions and plugins?
Concerning the difference between add-ons, extensions and plugins you should look at this answer. But in general, you seem to have the correct idea.
The problem is, there are currently three very different types of extensions:
Classic extensions (not restartless): these will typically overlay the browser window and run code from this overlay. Since there is one overlay per window, there will be as many code instances as browser windows. However, classic extensions can also register an XPCOM component (via chrome.manifest as of Gecko 2.0). This component will be loaded on first use and stay around for the entire browsing session. You probably want your component to load when the browser starts, for this you should register it in the profile-after-change category and implement nsIObserver.
Restartless extensions, also called bootstrapped extensions: these cannot register overlays which makes working with the browser UI somewhat more complicated. Instead they have a bootstrap.js script that will load when the extension is activated, this context will stay around in background until the browser is shut down or the extension is disabled. You can have XPCOM components in restartless extensions as well but you will have to register them manually (via nsIComponentRegistrar.registerFactory() and nsICategoryManager.addCategoryEntry()). You will also have to take care of unregistering the component if the extension is shut down. This is unnecessary if you merely need to add an observer, nsIObserverService will take any object implementing nsIObserver, not only one that has been registered as an XPCOM component. The big downside is: most MDN examples are about classic extensions and don't explain how you would do things in a restartless extension.
Extensions based on the Add-on SDK: these are based on a framework that produces restartless extensions. The Add-on SDK has its own API which is very different from what you usually do in Firefox extension - but it is simple, and it mostly takes care of shutting down the extension so that you don't have to do it manually. Extensions here consist of a number of modules, with main.js loading automatically and being able to load additional modules as necessary. Once loaded, each module stays around for as long as the extension is active. They run sandboxed but you can still leave the sandbox and access XPCOM directly. However, you would probably use the internal observer-service module instead.

Categories

Resources