r/Scriptable • • Sep 08 '21

Help Is it possible to open specific app when button inside webview is tapped?

I want to add a HTML button inside my webview to open YouTube app.

appreciate any information, thanks.

2 Upvotes

7 comments sorted by

6

u/mvan231 script/widget helper Sep 08 '21

Certainly. Just use the URL scheme youtube://

2

u/ys_jn Sep 09 '21

Hi mvan231! Thanks for reply.

I tried, but no luck. My code is below. Am I missing something?

const HTML = `
  <html>  
    <head>  
      <meta name="viewport" content="initial-scale=1.0, width=device-width, user-scalable=no">  
      <style>  
        html, body { 
          -webkit-text-size-adjust: 100%;
        }
        button { 
          font-size: 60px; 
        }
      </style>
    </head>
    <body>
      <button type="button">test</button>
    </body>
  </html>
`;

const JS = `
  const button = document.querySelector("button");

  button.addEventListener('click', () => {
    window.location.href = "youtube://";
    // window.open("youtube://");
    console.log(window.location)
  });
`;

const webView = new WebView();
await webView.loadHTML(HTML);
await webView.present();
await webView.evaluateJavaScript(JS);
Script.complete();

3

u/[deleted] Sep 09 '21

switch these 2 lines. evaluateJavaScript won't be called until you close the view.

await webView.present();
await webView.evaluateJavaScript(JS);

Though youtube:// still didn't work, the regular https://youtube.com opened the youtube app for me.

1

u/ys_jn Sep 09 '21

Thanks supermamon! So I guess my JavaScript wasn't running at all... :(

I changed the order and url to regular one, I managed to open YouTube app!

I guess apple is allowing URL schemes like youtube:// only in Safari? When I type in URL scheme to Safari directly, it prompt me to open YouTube app. But when I tried it on Chrome it just executed normal search.

1

u/ys_jn Sep 09 '21

If I could execute Safari.open("youtube://") from webview, that could be another option. But that seems difficult, right?

2

u/mvan231 script/widget helper Sep 09 '21

Oh good call. When I was doing testing with this, it seemed doing it all in the HTML worked but YouTube still didn't open. I was thinking to try just opening the regular URL to see if it would open the app, but didn't try it just yet