Open an url

The right place to talk JavaScript
Post Reply
User avatar
TrianguloY
Posts: 99
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

franc
Posts: 17
Joined: Sun Jan 27, 2019 9:00 pm

Re: Open an url

Post by franc » Sun Feb 07, 2021 12:06 pm

TrianguloY wrote:
Wed Jan 30, 2019 11:41 am
A script to open an url from a shortcut can be made with 3 lines of code.
...
But which 3 lines?
I "need" the opening of an url with my browser through a binding on an existing Tasker-Shortcut.
The Tasker-Shortcut shows some Tasker-LL-Variables, as a kind of self-made Widget.
I want that it opens the browser with an url when I switch down.
So I tried this script, but I don't see in it how I could only open an url.
I kept only the two functions:
createIntent(url) and open(url) and wrote: open("https://www.google.de"); but I only get:
"The url 'https://www.google.de' could not be opened."

The manually open a url (for script testing purpose) of this very script works neither (...could not be opened).
Only if I set the intent of the (Shortcut) item (replace the existing intent) to open a url, then

Any hint how just open an url with that or any script?
Thanks
frank

EDIT: I tried this:

Code: Select all

var url = "https://www.google.de";

// Opens the url in a browser
function open(url){
  var intent = new Intent(Intent.ACTION_VIEW);
  intent.setData(Uri.parse(url));
  if( !getBackgroundScreen().startActivity(intent)){
    toast("The url '"+url+"' could not be opened.");
  }
}
open(url);
But I get the error toast. Whats wrong?

I found some extra apk for LL for permissions, so I installed that "Lightning Launcher Internet Permission" but nothing changes, there is no permission setting for it anyway. I guess these apks are just old stuff not anymore relevant.
Could it be missing permission anyway?

EDIT2: never mind, I found the very old app URL Shortcut (by KebapApps) which works still good and I can create exactly that url shortcut in LL, so forget about my question :)

Post Reply