I have been trying to create a bookmarklet to run quick javascript, and am having trouble doing so. I have tried
javascript:var runjs = prompt("Enter JS:");function runjs() {runjs};
it seems you want to do the following
javascript:eval(prompt("Enter JS:"));
this will evaluate (run) the code entered in the prompt
as per comment
javascript:try{eval(prompt("Enter JS:"));}catch(e){alert(e);}
this will catch most? errors
Related
I am new to Javascript and trying out this code from a book. It says the window object is to be displayed with a browser. I have Node.js installed. But when I run the code below (with F5), I get this message:
Process exited with code 1
Uncaught ReferenceError: alert is not defined
Here is the code:
var age = 29;
var sayAge = () => alert(this.age);
alert(window.age);
sayAge();
window.sayAge();
How do I go about seeing what this code output is?
Use console.log() instead. Alert is for a popup type alert in a browser.
console.log() can be used in either the browser or NodeJS
E.g. console.log(age)
It will either log to the NodeJS console that's running your application, or the browser console if you're using it in a browser environment
That error means that your code has no idea where that alert method is from.
In order to make use of that alert i use html. If u dont know how to code html i suggest u take a look because is very handy.
Follows an example from w3schools.com
I suggest u use Visual Studio Code if you are working on a project, or just edit from w3schools or a similar website.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Button Text!</button>
<script>
function myFunction() {
alert("Alert!");
}
</script>
</body>
</html>
So, I want to make a javascript bookmarklet that starts a prompt to type in a password. if the password is correct it will alert a secret message. I put together some code from my other bookmarklets, and made this:
javascript: prompt("Password?");if(12345) alert(Correct)
But alas, it doesn't work. The only javascript experience i have is tinkering a little with a bunch of bookmarklets so...
This will work for tinkering etc. It is not actually secure.
let password = prompt('Password?');
if (password === 'asdf') {
alert('correct');
}
W3schools has a good JavaScript intro series:
https://www.w3schools.com/js/default.asp
In a Mac mail rule, I am trying to run a javascript instead of an applescript. This has been asked in Mail Rules using JavaScript for Automation instead of AppleScript but the answer is not working for me!
I have tried to simplify the code as much as I can. So, the following applescript works fine:
on perform mail action with messages theMessages
say "running!"
end perform mail action with messages
but the equivalent javascript is not working.
function performMailActionWithMessages(messages) {
app = Application.currentApplication()
app.includeStandardAdditions = true
app.say ("running")
}
Edit
Here are my rule parameters
I do it without getting app. Try something like…
// ObjC.import('Cocoa')
ObjC.import('stdlib')
var program="/usr/bin/say"
function performMailActionWithMessages(messages) {
say("running")
}
function say(what){
var command = program + " '"+what+"'"
console.log(command)
$.system(command)
}
I’m not sure you need cocoa. You probably need stdlib.
Follow the following steps.
Steps 1:
Go to google.
Open the javascript console.
Enter the command: document.all.q.value = "hello"
As expected the element with a name of "q" (the search field) is set to "hello").
Steps 2:
Go to google.
In the address bar type javascript: document.all.q.value = "hello!"
Press Enter
If your browser is either Internet Explorer, or Google Chrome, the javascript will have replaced the google website with an entirely blank page, with the exception of the word "Hello".
Finally
Now that you've bugged out your browser, go back to Google.com and repeat Steps 1. You should receive an error message "Uncaught ReferenceError: document is not defined (...) VM83:1
Question:
Am I doing something wrong? And is there another method which works, while still using the address bar for JS input?
The purpose of a javascript: scheme URL is to generate a new page using JavaScript. Modifying the existing page with it is something of a hack.
document.all.q.value = "hello!"; evalues as "hello!", so when you visit that URL, a new HTML document consisting solely of the text hello! is generated and loaded in place of the existing page.
To avoid this: Make sure the JS does not return a string. You can do this by using void.
javascript:void(document.all.q.value = "hello!");
When messing around with javascript: in the adressbar some (if not the most) browsers handle it as a new page, so you have to add a window.history.back(); at the end
javascript: document.all.q.value = "hello!"; window.history.back();
i know that to run a javascript function in webview we need to load it in loadUrl(). In my program everything works fine javascript gets called when i use it in loadUrl, but instead of running javascript on the same page, loadUrl("javascript:function()") vanishes my previous page and run this javascript "function()" in a totally new blank page..
for eg. i tried to fill a form automatically using command:
view.loadUrl("javascript:document.getElementById('password').value = 'my_passoword'");
what happens is, the page which consists of ID-'password' vanishes and a new blank page generates consisting of 'my_password' only
where is the problem?
Using loadUrl to run javascript loads different page
anonymous self-invoking function works fine.. i.e.
view.loadUrl("javascript:(function(){document.getElementById('password').value = 'sb14november';})()");
and as a loop around i think Bojan Kseneman answer will work too..
thanks to all!! :)
EDIT: This library is only for evaluating JavaScript and it created a new WebView instead of using an existing one :/
You can also try js evaluator library
jsEvaluator.evaluate("put your JavaScript code", new JsCallback() {
#Override
public void onResult(final String result) {
// you get the result here (optional)
}
});