How to call an onclick javascript function directly in url? - javascript

I want to call to a onclick function as I am writting script for MikroTik RouterBoard in order to restart my Modem by just visiting a simple link directly but what I found was that the page from which Modem is rebooting there is a button which calls to a onlick function as :
<input type="button" onclick="btnReset()" value="Reboot">
So is there any way that I can Call this onclick function directly in a http url like :
http://admin:hunter#192.168.1.1/resetrouter.html?btnReset()=Reboot
Here is MikroTik Script which I am writting but it can't do the job..It need a direct link which it visits only and downloads a file..!
If anyone MikroTik Scripting person can help will be greatful..Until then if there is any way to do it in direct url so then that will be great..!
{
/tool fetch url="http://admin:hunter#192.168.1.1/resetrouter.html?btnReset()=Reboot" mode=http
}

That's not possible to perform js code from a hyperlink (unless the page has script inside, specifically for this and it's checking for some parameter...) .
However there are some tools which could help you perform desired action in other way:
Install Custom JS for Web Sites plugin and define js script which would be executed after your http://admin:hunter... page would be loaded in browser.
For your case it would be simple function call:
btnReset()
Analyse body of btnReset() function( probably that function sends http request) and construct same request with cURL, see cURL Tutorial.
Take a look on Bookmarklet, which is a bookmark stored in a web browser that contains JavaScript commands.
Get familiar with PhantomJS or Selenium and write simple script which would perform desired action for you.

The btnReset() function is probably just a dialog followed by a URL fetch. Open the javascript console, type btnReset.toString() and have a look. If you see a URL in there, try visiting that directly.

You need a very specific URL "http://admin:hunter#192.168.1.1/rebootinfo.cgi". In my case the function btnReset() calls "rebootinfo.cgi". I followed the below steps to find "rebootinfo.cgi".
Open the "save/reboot" page or equivalent page of your router.
Inspect the button "Reboot", i.e, right click on the particular button and click "Inspect" from the menu.
In the developer tool now opened to the right, note the function for the onclick attribute in the highlighted line. E.g. in my case, btnReset() from the line "input type="button" onclick="btnReset()" value="Reboot".
Go to the console tab on the developer tool and paste the function name without parenthesis E.g., btnReset (not btnReset() ) and hit enter.
In the function definition for btnReset(), look for the exact command that is responsible for rebooting the router. In my case it was "rebootinfo.cgi".
That's it, construct the URL.

Related

Execute JavaScript function using Python requests_html

I am trying to execute a JavaScript function (run with a button click) within a session using Python's requests_html
I understand the regular requests library does not have JavaScript support so I am trying to use requests_html instead.
Here's what I have (using requests):
s = requests.Session()
r = s.post(url)
print(r.text)
r2 = s.post(url2)
print(r2.text)
url is the link to the page containing the button and url2 is the POST request link the button's JavaScript function executes. (I found url2 through the network tab while in my browser inspector and clicking the button as a test)
However, this does not work and I get this from r2.text:
<h2>Error(500): An error occurred.</h2>
<p>We are sorry but an unexpected error has occurred on our side while handling your request. In the meantime, please retry your request or try the following:</p>
To my understanding, an error 500 means that the issue is server-side, not client-side. However, clicking the button manually on the webpage works fine.
This brings me to attempting to directly execute the JavaScript function instead. I couldn't find anything on the requests_html documentation. I've also looked at Selenium, but that doesn't seem to be up to date.
It is also worth mentioning that the button inspector looks like this: <button onclick="registerInterest(72833,959320000, '')" type="button" class="btn btn-primary"><i class="far fa-clipboard"></i> Register Interest</button>
So essentially, I would like to execute registerInterest(72833,959320000, '') after my first POST request.
Any help would be greatly appreciated,
I will gladly provide any additional needed information.
You need to use Selenium for manipulating html elements. You can use code like this:
from selenium import webdriver
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
#implicit wait
driver.implicitly_wait(0.5)
#maximize browser
driver.maximize_window()
#launch URL
driver.get("https://www.tutorialspoint.com/index.htm")
#identify element
l =driver.find_element_by_xpath("//button[text()='Check it Now']")
#perform click
l.click()
print("Page title is: ")
print(driver.title)
#close browser
driver.quit()
Just check docs on methods of Selenium and find a method which fits you the best.

Vanilla JS Single Sign-On attempt with MSAL.js leading to pop-up window showing a copy of the same page

I've copied the example app at this repository to try to implement single sign-on: https://github.com/Azure-Samples/ms-identity-javascript-v2. I've changed the config values match those of the Azure configuration. I'm using the public version of the authority: "https://login.microsoftonline.com/[APP VALUE HERE]" in this configuration as well.
For additional background - I had previously not had the redirectURL correctly matching, and the popup window from running the example showed my user account name before failing with an error. From this I take it to mean that the sign-on itself was successful in recognizing me, and that any problems are happening after that point.
The problem I'm running into now is that the SSO popup just loads an exact copy of the page that I used to launch the request in the first place - exact same display and everything. My logging shows that the request to myMSALObj.loginPopup({scopes: ["User.Read"]}) never completes, it simply hangs until I close the popup window, at which point it fails into the catch block for that request with the following error: "BrowserAuthError: user_cancelled: User cancelled the flow."
So it seems like the process is waiting for some step that never occurs, presumably coming as part of the call within the popup window. Has anyone else encountered this issue before? Does anyone have any recommendation for how to fix it or how to dig deeper into what's occurring?
This usually happens if the page you use as your redirectUri is either clearing the url hash on load or redirecting to another page on load. We usually recommend people use a blank page that doesn't implement any logic as their redirectUri to avoid issues like this. If that's not possible try to see what might be causing the server response to be removed from the popup.

Execute command on another tab

I'm new at JavaScript, so can someone help me? How can I execute a command on another tab? For example, I have opened new tab from my main tab and opened translate.com (just for textbox) on it, and the problem is I don't know how to put text in search textbox?
I open the page with this command:
var page = window.open('https://www.translate.com/');
On the page, I can enter text with this code:
$string1 = $("#source_text");
$string1.val("text");
I have tried this, but this code doesn't work the way I want it to.
var page = window.open('https://www.translate.com/');
setTimeout(function(){page.f1()},20000);
setTimeout(function(){page.close()},30000);
function f1() {
$string1 = $("#source_text");
$string1.val("ka tu");
}
I find the function that you are trying to run very abnormal. So, let's start with small steps.
The page.close() function is perfect you can do that and it works. The other part won't work first of all because the page object created by window.open has no function called f1 on it.
Furthermore it is very important from where you are trying to run the script on the other window you always must take into consideration the cross-origin limitations. Easily explained if you try to run a function from a google.com tab on a separate window yahoo.com it won't work this is a security issue.
In order to run function f1 in that window it is important that f1 function is declared globally and ussualy you try and do this the following way page.window.f1() - and there you have it. So for example your code would be
page.window.$("#source_text").val('something');
refactoring your code would be like this:
var page = window.open('https://www.translate.com/');
setTimeout(function(){ page.window.$("#source_text").val('something');},20000);
setTimeout(function(){page.close()},30000);
open translate.com in a tab open dev tools in chrome and paste the above code in the console tab and see the results, it will work.
I recommend that before running code in another window you should check that that window is loaded first of all (in your case works) because of the long timeout.
A better solution would be using HTML5's postMessage API: check this link here for a good tutorial: https://robertnyman.com/2010/03/18/postmessage-in-html5-to-send-messages-between-windows-and-iframes/ - also this meens of course that the window you are opening is listening for some sort of postMessages.
In general you do this things with hosts that you manage and not other hosts because it might not work always. Also it will always work if you are on a host and open the same host in another window otherwise you end up with this security error: VM88:2 Uncaught SecurityError: Blocked a frame with origin "https://some_other_host" from accessing a frame with origin "https://www.translate.com". Protocols, domains, and ports must match.
Hope this helps, and you understand the way it works now.
Cheers

Javascript in asp.net MVC... Beginner issue

I created an Asp.Net MVC Internet Aplication and in my Index view of the Home Controller I have this
This is the first line, before the script results.
<script type="text/javascript" src="~/Script/Teste.js"></script>
<br />
This line comes after the script.
In my Teste.js I have this:
document.write("Yes! I am now a JavaScript coder!");
But nothing happens. If I change the src attribute and put some random name src="aaaa", despite the fact "aaaa" doesnt exist, I get no error in runtime.
EDIT
Also, check your path again. The default MVC templates in VS create a folder called Scripts, not Script. ("~/Scripts/teste.js")
Per the comment below, this was not the root cause of the issue, but in other cases can easily bite new JavaScript developers.
Most likely, your document.write function is firing before the document is ready, leading to the appearance that nothing is happening. Try the following in your Teste.js file
window.onload = function ()
{
document.write("Yes! I am now a JavaScript coder!");
//or even better as a test
alert("This alert was called");
}
Check the source of your page as well, it could be the document is being written to, you just can't see it due to markup/page styling.
As for you second issue, there will be no 'Runtime Exception' thrown if you reference a non-existent file. If you are using tools like Firebug or Chrome's developer tools, you should see a request to http://siteDomain/Scripts/aaaa.js with a response of 404, not found.
You generally should avoid using document.write() unless you absolutely have to use it for some reason... I don't think I've ever come across such a situation, and write a lot of Javascript.
Try this:
1) Put this in your HTML:
<script src="/scripts/teste.js"></script>
2) Put this in your JS:
alert('Yes! I am now a JavaScript coder!');
3) Open Chrome since it makes it easy to look for external resources loading and open the Network tab in Developer Tools (click the menu button at top-right, Tools > Developer Tools, Network tab).
4) Run your project and copy/paste the URL in the browser that comes up into this Chrome window, and hit enter.
When your page loads one of 2 things will happen:
A) You'll get the alert box you wanted or
B) You'll find out why it isn't loading because the Network tab will show the browser attempting to fetch teste.js and failing in some fashion, for example a 404, which would indicate you've got a typo in the path, or the script isn't where you thought it was, etc.
Put the following line at the very end of your document. There should not be anything after. Then try to load the page.
<script type="text/javascript" src="~/Script/Teste.js"></script>
Also, try pressing F12 once the page loads to see the source. Check if you script is there.
In MVC, the tilde is used to refer to the root URL of your application. However, it cannot normally parse this information. If you write:
<script src="~/Script/Teste.js"></script>
The lookup will fail, because the ~ means nothing special in HTML. If you're using Razor as your view engine (not ASPX), you need to wrap that call in Url.Content like so:
<script src="#Url.Content(~/Script/Teste.js)"></script>
Doing this will ensure a valid URL is provided to the browser.
With that in mind, you need to check that you have the file name and folder name both correct. You also need to ensure that the file is being deployed with your application. You can do this my opening the properties panel while the file is selected in the Solution Explorer and pressing F4.

run javascript function on griview pageindexchanging event

I have a gridview, with paging enabled.
This is the link when I want to go to second page
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$gv','Page$2')">
The problem is that I require to run the CheckValid() function before paging is done - user cannot page with invalid details. How can I set the HREF to allow this?
<a href= "if Checkvalid() then "javascript:__doPostBack('ctl00$ContentPlaceHolder1$gv','Page$2')"">
google doesn't return any answers I could find
In your case, pagers are generated by ASP.NET, so you can't do much server-side.
You have to run a client-side script which, in steps:
1-Finds the pager anchors
2-replace (and store) their href with an onclick function
3-create your validation function that, if successfull, calls the original stored function "__doPostBack".
That's not so complicated if you get the help of client-side tools like Firebug or Chrome developer tools.

Categories

Resources