<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: theoldman</title>
    <description>The latest articles on DEV Community by theoldman (@cokacode).</description>
    <link>https://dev.to/cokacode</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F298321%2F756ba440-3919-484e-b365-45bb5eb92b56.png</url>
      <title>DEV Community: theoldman</title>
      <link>https://dev.to/cokacode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cokacode"/>
    <language>en</language>
    <item>
      <title>what are Gestures anyway?</title>
      <dc:creator>theoldman</dc:creator>
      <pubDate>Tue, 01 Nov 2022 16:28:50 +0000</pubDate>
      <link>https://dev.to/cokacode/what-are-gestures-anyway-3dk0</link>
      <guid>https://dev.to/cokacode/what-are-gestures-anyway-3dk0</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Boring content alert, Not coool stuff.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So I was playing around with webShare API (the reason being now it supports desktop as well, it was only available in mobile when I last checked.) (&lt;code&gt;navigator.share&lt;/code&gt;), and one interesting thing I found was you can not call &lt;code&gt;navigator.share&lt;/code&gt; randomly anywhere, you need a "&lt;strong&gt;user gesture&lt;/strong&gt; (that's what the error said )", I thought maybe scroll could be used to abuse the &lt;code&gt;navigator.share&lt;/code&gt; and various other APIs that need user action, or rather I should say user gesture.&lt;/p&gt;

&lt;p&gt;Of course, if you think about it, it should be very natural to think that things like &lt;code&gt;scroll&lt;/code&gt; and &lt;code&gt;hover&lt;/code&gt; should be considered user gestures, at least that's what I used to think, and things like &lt;code&gt;navigator.share()&lt;/code&gt; should work when using them on hover right?&lt;/p&gt;

&lt;p&gt;If this was the case I would have done something like this,&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// bool shareOpened = false; 

let shareOpened = false;

document.body.addEventListener("mouseover", function () {
    if (!shareOpened &amp;amp;&amp;amp; "share" in navigator) {
        navigator.share({
            data: "spam",
            url: "https://spam_link.com"
        }).then(val =&amp;gt; {
            console.log("ok");
            shareOpened = true;
        }).catch(e =&amp;gt; {
            console.log(e);
            console.log("!ok");
        })
    } else {
        shareOpened = true;
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, I'm not a spammer I don't know what someone would achieve by banging the web share API, but as a developer, I can tell you you will get exceptions like this&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DOMException: Failed to execute 'share' on 'Navigator': Must be handling a user gesture to perform a share request. at HTMLBodyElement.&amp;lt;anonymous&amp;gt; (https://my_shit_code.com/script.js:17:19)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Now I want you to try something -
&lt;/h2&gt;

&lt;p&gt;Click anywhere in the screen, hover out of the screen and hover again, to see if this works now, you don't need a gesture now, it works with just hovering.&lt;/p&gt;

&lt;p&gt;This is crazy right, some of you might even try to open the chromium bug website to report it there, at least that is what I was about to do, but no it's not a bug this is how chrome user gestures work.&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;So today I would like to share some stuff with you.&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;To prevent the abuse of APIs like this, chromium based browsers ( I don't know about firefox ) have come up with some solutions, the solution is the separation of user activations and user-gesture&lt;/p&gt;

&lt;p&gt;Almost all things are user activations (scroll, hover, and various others and spammers use these things to abuse the APIs).&lt;/p&gt;

&lt;p&gt;There are only four (4) things that are considered user gestures, and here is how chrome has implemented it&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bool IsUserInteractionInputType(blink::WebInputEvent::Type type) {
  // Ideally, this list would be based more off of
  // https://whatwg.org/C/interaction.html#triggered-by-user-activation.
  return type == blink::WebInputEvent::Type::kMouseDown ||
         type == blink::WebInputEvent::Type::kGestureScrollBegin ||
         type == blink::WebInputEvent::Type::kTouchStart ||
         type == blink::WebInputEvent::Type::kRawKeyDown;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Once a single gesture&lt;/strong&gt; is fired many APIs allow running the things like &lt;code&gt;navigator.share&lt;/code&gt; without any gesture, that is what happened in the above code, and this is only allowed to do for 5 seconds after 5 seconds (time may differ, I'm saying based on my research ) it will not work. so let's try running our code once again by modifying a little&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.body.addEventListener("mouseover", function () {
    if ("share" in navigator) {
        navigator.share({
            data: "spam",
            url: "https://spam_link.com"
        }).then(val =&amp;gt; {
            console.log("ok", val);
        }).catch(e =&amp;gt; {
            console.log("ok!", e);
        })
    }else{
        console.log("not supported");
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If I run the above code on a fresh page, &lt;code&gt;navigator.share&lt;/code&gt; will not open but if I click anywhere the user gesture will be registered and for the next 5 seconds my code will work without any gesture, as soon as I will hover the popup for share will open.&lt;/p&gt;

&lt;p&gt;There are also things called consumable gestures, which means they require user gestures for everything. The 5-second thing doesn't work on those calls, so you have no way to abuse these APIs, you will have to rely on a user gesture.&lt;/p&gt;

&lt;p&gt;I myself haven't read all the details yet, so I will not pretend to know everything, but if you want more on this topic check the references below. Things are not very simple, it needs a thorough reading, in my opinion, to understand in detail.&lt;/p&gt;

&lt;p&gt;Here is a simple youtube video &lt;a href="https://youtu.be/r1z2R3qX9uQ"&gt;https://youtu.be/r1z2R3qX9uQ&lt;/a&gt;  (sorry Reddit takes forever to upload ) without audio (sorry), as you can see first it did not work but once I clicked it started to show the share popup, and again it expired after about 5 seconds, you can try it yourself.&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;h2&gt;
  
  
  Code I used
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Web Share&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body style="height:100vh;"&amp;gt;
    &amp;lt;button&amp;gt;Share this page, just for nothing&amp;lt;/button&amp;gt;
    &amp;lt;script&amp;gt;

document.body.addEventListener("mouseover", function () {
    if ("share" in navigator) {
        navigator.share({
            data: "spam",
            url: "https://spam_link.com"
        }).then(val =&amp;gt; {
            console.log("ok", val);
        }).catch(e =&amp;gt; {
            console.log("ok!", e);
        })
    }else{
        console.log("not supported");
    }
})

    &amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;p&gt;​&lt;/p&gt;

&lt;h2&gt;
  
  
  References and tools that might help you
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Chrome Code - &lt;a href="https://source.chromium.org/chromium/chromium/src/+/main:content/browser/web_contents/web_contents_impl.cc;l=266;drc=9d82515060b9b75fa941986f5db7390299669ef1"&gt;https://source.chromium.org/chromium/chromium/src/+/main:content/browser/web_contents/web_contents_impl.cc;l=266;drc=9d82515060b9b75fa941986f5db7390299669ef1&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;If anyone is interested in reading more boring content - &lt;a href="https://html.spec.whatwg.org/multipage/interaction.html#triggered-by-user-activation"&gt;https://html.spec.whatwg.org/multipage/interaction.html#triggered-by-user-activation&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://mustaqahmed.github.io/user-activation-v2/"&gt;https://mustaqahmed.github.io/user-activation-v2/&lt;/a&gt; (user activation)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://bayden.com/test/popup/#timers"&gt;https://bayden.com/test/popup/#timers&lt;/a&gt; ( a tool some nice person made for timers - the 5s thingy )&lt;/li&gt;
&lt;li&gt;BTW Here is the list of APIs that need user gesture - &lt;a href="https://docs.google.com/document/d/1mcxB5J_u370juJhSsmK0XQONG2CIE3mvu827O-Knw_Y/edit"&gt;Google Docs Document&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
