Open an url

The right place to talk JavaScript
Post Reply
User avatar
TrianguloY
Posts: 106
Joined: Thu Jan 24, 2019 9:46 am

Open an url

Post by TrianguloY » Wed Jan 30, 2019 11:41 am

A script to open an url from a shortcut can be made with 3 lines of code. This script does that but also allows for multiple features regarding the creation of those shortcuts. To be precise the script allows you to:

1) If you run the script with an url as data (via lightning action->run script) that url will be opened. This can be used to open urls on gesture actions (long tap, swipe, stop points...) without explicitly create the shortcut. Note: the script must exist to open the url, but the url can be changed easily from the action configuration screen.

2) If you run the script from an item, you can change the intent of the item so that it will open the url (replacing any existing intent). Note: once the intent is set the script is no longer needed, but to change the url you need to edit the intent again. [remember to mark the 'Item menu' category so that it is displayed in the list].

3) If you run the script from a container, you can create a new shortcut to open a url. This is the same as creating a new empty item and changing the intent with option 2 (the script is not needed for the item to work, but to change the url you need to edit the intent) [remember to mark the 'Lightning menu' category so that it is displayed in the list].

4) You can also run the script to launch manually a url...if for some reason you don't want to open your browser directly :P

Wiki url: https://www.lightninglauncher.com/wiki/ ... t_open_url

Script:

Code: Select all

//check data, if found open that url
var data = getEvent().getData();
if(data != null){
open(data);
return;
}

//no data, check item. if found replace intent
var i = getEvent().getItem();
if(i != null && confirm("Do you want to set the intent of the item '"+i+"' to open a url? Warning! this will replace the existing intent!!!")){
setIntent(i);
return;
}

//no data nor item, check container. if found ask to create item
var c = getEvent().getContainer();
if(c != null && confirm("Do you want to create a new item to open a url when clicked?")){
createItem(c);
return;
}

//no data nor item nor container, just ask and open
if(confirm("Do you want to manually open a url? (for script testing purposes)")){
manual();
return;
}

//nothing
toast("No option selected :(")





//Opens the url in a browser
//Shows toast on error
function open(url){

if( !getBackgroundScreen().startActivity(createIntent(url)) ){
toast("The url '"+url+"' could not be opened.");
}

}



//returns an intent to open the url
function createIntent(url){

var intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
return intent;

}



//asks the user to enter a url
//returns the user input (null if cancelled)
function askUrl(){

return prompt("Write the url you want to open","https://");

}



//asks for a url
//opens it (nothing if cancelled)
function manual(){

var url = askUrl();
if(url == null) return;

open(url);

}



//asks the user a url
//sets the intent of item i to open that url (nothing if cancelled)
function setIntent(i){

var url = askUrl();
if(url == null) return;

i.setIntent(createIntent(url));
toast("item modified");

}



//asks the user a url and a name
//creates a new item in the container c to open that url (nothing if cancelled)
function createItem(c){

var url = askUrl();
if(url == undefined) return;
var name = prompt("Name of item?","Open "+url);
if(name == undefined) return;
var px = getEvent().getTouchX();
var py = getEvent().getTouchY();

var i = c.addShortcut(name,createIntent(url),px,py);
i.setDefaultIcon(Image.createTextIcon("Z",100,Color.GRAY,Color.TRANSPARENT,null));//"Z" is the web icon
toast("item added");

}



//Shows a toast with the message m
function toast(m){

Toast.makeText(getBackgroundScreen().getContext(),m,Toast.LENGTH_LONG).show()

}

jalanchilo
Posts: 11
Joined: Sun Jan 27, 2019 5:31 pm

Re: Open an url

Post by jalanchilo » Sun May 24, 2020 7:10 pm

Running the script from a shortcut with a url in data doesn't do anything. Running it without any data gives the following error:
At line 33: TypeError: Cannot call method "forEach" of null

Post Reply