<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Zameer Haque on Medium]]></title>
        <description><![CDATA[Stories by Zameer Haque on Medium]]></description>
        <link>https://medium.com/@haque864?source=rss-1114e55a1e10------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*SLlvQ3TudkSIzqIFWbgb0A.jpeg</url>
            <title>Stories by Zameer Haque on Medium</title>
            <link>https://medium.com/@haque864?source=rss-1114e55a1e10------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 17:28:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@haque864/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Curious Case of missing Object Properties in a Next.js Project]]></title>
            <link>https://medium.com/@haque864/curious-case-of-missing-object-properties-in-a-next-js-project-7bc5230c2e9?source=rss-1114e55a1e10------2</link>
            <guid isPermaLink="false">https://medium.com/p/7bc5230c2e9</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Zameer Haque]]></dc:creator>
            <pubDate>Sun, 10 Mar 2019 17:27:18 GMT</pubDate>
            <atom:updated>2019-03-10T17:28:32.848Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Uim_lZBwIx4MG_RgcW-jvQ.jpeg" /><figcaption>Just an Image (Photo by <a href="https://unsplash.com/photos/fvMeP4ml4bU?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Pankaj Patel</a> on <a href="https://unsplash.com/search/photos/json?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a>)</figcaption></figure><p>Hi there 👋 Hope you are doing well! So this blog post is about the Curious Case of “missing” object properties that were added during coding but went missing during the runtime.</p><p>Ok. Might be a shocker! How can some properties get deleted/removed? Read on!</p><p>So we recently ported our whole codebase from a custom SSR setup to a Nextjs based project. This specific case appeared in one module that gets fed a list of objects with properties that describe each object, which is usually a few default values, fields and validation functions. These validation functions are the properties that were getting removed during the run time. However, this was not always the case! While I was working on it, the module worked and gave the desired results. But, a simple change in the way we loaded the list of objects lead to the validation function properties being removed from the object. It seems the objects were now loaded inside the `getInitialProps` lifecycle hook provided in next and then pass as a prop to the component whereas earlier the data was loaded in the render portion of the component itself.</p><p>It did not seem like a big deal but in Next.js `getInitialProps` it is. It seems loading the data(list of objects) in `getInitialProps` leads to the `serialization` of the object, and so the function is removed from objects properties when the object is sent from the server to the client. Go ahead and try to serialize an object with a function as a property.</p><blockquote>let<em> </em>obj<em> </em>=<em> </em>{<em> a</em>:<em> </em>‘some val’,<em> </em>fun()<em> </em>{<em> return true</em>}<em> </em>};</blockquote><blockquote>JSON<em>.</em>stringify(obj);</blockquote><p>If you run this piece of code in your browser console, you will notice that the functions are dropped in the serialization.</p><p>Oh, darn, then what do we do?</p><p>Well, All is not lost yet. You can convert the functions into strings that you want to preserve through serialization, as discussed in this <a href="https://stackoverflow.com/questions/11697863/what-is-the-correct-way-to-serialize-functions-in-javascript-for-later-use">StackOverflow answer</a>.</p><p>Also, you can provide your own Stringification function to the JSON.stringify() function as a optional parameter. Here is a really good <a href="https://gist.github.com/Yang03/a47317babadcaa20aca2ddedfaaf3e6d">GitHub gist</a> on the same.</p><p>See you next time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7bc5230c2e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Contrast Principle]]></title>
            <link>https://medium.com/@haque864/the-contrast-principle-82e7d4dafe4f?source=rss-1114e55a1e10------2</link>
            <guid isPermaLink="false">https://medium.com/p/82e7d4dafe4f</guid>
            <category><![CDATA[influence]]></category>
            <category><![CDATA[books]]></category>
            <category><![CDATA[psychology]]></category>
            <category><![CDATA[sales]]></category>
            <category><![CDATA[marketing]]></category>
            <dc:creator><![CDATA[Zameer Haque]]></dc:creator>
            <pubDate>Sun, 12 Aug 2018 15:07:19 GMT</pubDate>
            <atom:updated>2018-08-14T14:27:48.602Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*p0ImQp3zjmCBIXgw" /><figcaption>Photo by <a href="https://unsplash.com/@philinit?utm_source=medium&amp;utm_medium=referral">Philippe Leone</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Have you heard about the Contrast Principle? By the name of the principle itself, You can guess somewhat what it is.</p><p>I have recently started reading <a href="https://www.amazon.in/Influence-Psychology-Persuasion-Business-Essentials-ebook/dp/B002BD2UUC/ref=sr_1_6?s=books&amp;ie=UTF8&amp;qid=1534053594&amp;sr=1-6&amp;keywords=influence+the+psychology+of+persuasion"><strong>Influence: The psychology of persuasion</strong></a><strong> </strong>by <strong>Robert Cialdini </strong>and I have to say, the first chapter itself is amazing.</p><p>The Contrast Principle is one of those laws that master salesmen have been using on us from a long time without us even acknowledging it.</p><h4>What is the Contrast Principle?</h4><p>According to the Contrast Principle,</p><blockquote>One will perceive two things in a drastically different way if they are presented one before another depending on the order in which these things are presented.</blockquote><p>Consider the case when you want to buy a Suit and some accessories and the salesman starts to show you the Suit first and then the accessories instead of showing the accessories first and then the suit.</p><h4>Why?</h4><p>They are making use of the Contrast Principle. Once you buy a <strong>$499</strong> suit and he shows you some <strong>$99</strong> accessories after that, there are more chances of you buying the accessories since the perception of the price is drastically different. Compared to <strong>$499</strong> what is <strong>$99</strong>? It is a pretty good deal.</p><p>But if he showed you the <strong>$99</strong> accessories first you might be reluctant to buy it, and if you are not buying the <strong>$99</strong> accessories then there are very huge chances that you will conceive the<strong> $499</strong> as too expensive.</p><p>So this is my understanding of the Contrast Principle. I might be wrong with my understanding. If you have any feedback, please comment below. I would love to hear from you. Thanks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82e7d4dafe4f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maintaining Extension State in SUSI.AI Chrome Bot Using Chrome Storage API]]></title>
            <link>https://medium.com/@haque864/maintaining-extension-state-in-susi-ai-chrome-bot-using-chrome-storage-api-e63f70c588ef?source=rss-1114e55a1e10------2</link>
            <guid isPermaLink="false">https://medium.com/p/e63f70c588ef</guid>
            <category><![CDATA[chrome]]></category>
            <category><![CDATA[storage]]></category>
            <category><![CDATA[chrome-extension]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Zameer Haque]]></dc:creator>
            <pubDate>Sun, 18 Feb 2018 00:00:00 GMT</pubDate>
            <atom:updated>2018-02-18T10:48:52.372Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*EQJrW26OGaSuZDFe2UfxyQ.jpeg" /><figcaption>susi_chromebot extension</figcaption></figure><p><a href="https://github.com/fossasia/susi_chromebot">SUSI Chrome Bot</a> is a browser action chrome extension which is used to communicate with <a href="https://github.com/fossasia/susi_chromebot">SUSI AI</a>.The browser action extension in chrome is like any other web app that you visit. It will store all your preferences like theme settings and speech synthesis settings and data till you are interacting with it, but once you close it, it forgets all of your data unless you are saving it in some database or you are using cookies for the session. We want to be able to save the chats and other preferences like theme settings the user makes when interacting with SUSI AI through Susi Chrome Bot. In this blog, we’ll explore Chrome’s chrome.storage API for storing data.</p><h3>What options do we have for storing data offline?</h3><p><em>IndexedDB</em>: IndexedDB is a low-level API for client-side storage of data. IndexedDB allows us to store a large amount of data and works like RDBMS but IndexedDB is javascript based Object-oriented database.</p><p><em>localStorage API</em>: localStorage allows us to store data in key/value pairs which is much more effective than storing data in cookies. localStorage data persists even if the user closes and reopens the browser.</p><p><em>Chrome.storage</em>: Chrome provides us with chrome.storage. It provides the same storage capabilities as localStorage API with some advantages.</p><p>For susi_chromebot we will use chrome.storage because of the following advantages it has over the localstorage API:</p><ol><li>User data can be automatically synced with Chrome sync if the user is logged in.</li><li>The extension’s content scripts can directly access user data without the need for a background page.</li><li>A user’s extension settings can be persisted even when using incognito mode.</li><li>It’s asynchronous so bulk read and write operations are faster than the serial and blocking localStorage API.</li><li>User data can be stored as objects whereas the localStorage API stores data in strings.</li></ol><h3>Integrating chrome.storage to susi_chromebot for storing chat data</h3><p>To use chrome.storage we first need to declare the necessary permission in the extension’s manifest file. Add <strong>“storage”</strong> in the permissions key inside the manifest file.</p><pre>&quot;permissions&quot;: [ &quot;storage&quot; ]</pre><p>We want to store the chat user has made with SUSI. We will use a Javascript object to store the chat data.</p><pre>var storageObj = { senderClass: &quot;&quot;, content: &quot;&quot; };</pre><p>The <strong>storageObj</strong> object has two keys namely <em>senderClass</em> and <em>content</em> . The <em>senderClass</em> key represents the sender of the message(user or susi) whereas the <em>content</em> key holds the actual content of the message.</p><p>We will use <strong>chrome.storage.get</strong> and <strong>chrome.storage.set</strong> methods to store and retrieve data.</p><pre>var susimessage = newDiv.innerHTML; storageObj.content = susimessage; storageObj.senderClass = &quot;susinewmessage&quot;; chrome.storage.sync.get(&quot;message&quot;,(items) =&gt; { if(items.message){ storageArr = items.message; console.log(&quot;this was true&quot;); } storageArr.push(storageObj); chrome.storage.sync.set({&quot;message&quot;:storageArr},() =&gt; { console.log(&quot;saved&quot;); }); });</pre><p>In the above code snippet, <strong>susimessage</strong> contains the actual message content sent by the SUSI server. We then set the correct properties of the <strong>storageObj</strong> object that we declared earlier. Now we can use <strong>chrome.storage.set</strong> to save the <strong>storageObj</strong> object but that would overwrite the current data that we have inside chrome’s <strong>StorageArea</strong>. To prevent the old message data from getting overwritten, we’ll first get all the message content in our storage using <strong>chrome.storage.sync.get</strong>. Notice how we are passing the <strong>“message”</strong> string as the first perimeter to the function. This is done because we only want our message content which was saved in the <strong>StorageArea</strong>. If we pass <strong>null</strong> instead, it will return all the content inside storageArea. Once we have our messages (which will be an array of objects that we store as storageObj), we will store that into a new array <strong>storageArr</strong>. We will then push our new <strong>storageObj</strong> that contains the <strong>message</strong> and the <strong>sender</strong> into the array. Finally, we use chrome.storage.sync.set to save the message content in chrome’s <strong>StorageArea</strong> which can later be retrieved using the <strong>“message”</strong> key.</p><pre>storageArr.push(storageObj); chrome.storage.sync.set({&quot;message&quot;:storageArr},() =&gt; { console.log(&quot;saved&quot;); });</pre><p>We use the same procedure to save messages sent by the user.</p><p>Note: chrome.storage is not very large, so we need to be careful about what we store or we may run out of storage space. Also, we should not store confidential data in storage since the storage area is not encrypted.</p><h3>Resources:</h3><ul><li><a href="https://developer.chrome.com/extensions/storage">chrome.storage API (developer.chrome.com/extensions/storage)</a></li><li><a href="https://developer.mozilla.org/en/DOM/Storage#localStorage">Web Storage API (Mozilla Development Network Web Docs)</a></li><li><a href="https://softwareengineering.stackexchange.com/a/219964">How is localStorage different from indexedDB?</a></li></ul><p><em>Originally published at </em><a href="https://zamhaq.github.io/blog/posts/2018-02-18/"><em>zamhaq.github.io</em></a><em> on February 18, 2018.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e63f70c588ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Susi Chrome Bot and Web Speech: Integrating Speech Synthesis and Recognition in Susi Chrome Bot]]></title>
            <link>https://medium.com/@haque864/susi-chrome-bot-and-web-speech-integrating-speech-synthesis-and-recognition-in-susi-chrome-bot-bc143cfa6bcf?source=rss-1114e55a1e10------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc143cfa6bcf</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-speech]]></category>
            <category><![CDATA[speech-recognition]]></category>
            <category><![CDATA[speech-synthesis]]></category>
            <dc:creator><![CDATA[Zameer Haque]]></dc:creator>
            <pubDate>Thu, 12 Oct 2017 00:00:00 GMT</pubDate>
            <atom:updated>2017-11-18T13:04:46.602Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*Bo9hXmwh88aaNHAWf4pROQ.png" /><figcaption>susi_chromebot extension</figcaption></figure><p><a href="https://github.com/fossasia/susi_chromebot">Susi Chrome Bot</a> is a chrome extension which is used to communicate with Susi AI. The advantage of having chrome extensions is that they are very accessible for the user to perform certain tasks which sometimes needs the user to move to another tab/site.</p><p>In this blog post, we will be going through the process of integrating the web speech API to susi_chromebot.</p><h3>Web Speech API</h3><p>Web Speech API enables web apps to be able to use voice data. The Web Speech API has two components:</p><p><em>Speech Recognition</em>: Speech recognition gives web apps the ability to recognize voice data from an audio source. Speech recognition provides the speech-to-text service.</p><p><em>Speech Synthesis</em>: Speech synthesis provides the text-to-speech services for the web apps.</p><h3>Integrating speech synthesis and speech recognition in susi_chromebot</h3><p>Chrome provides the webkitSpeechRecognition() interface which we will use for our speech recognition tasks.</p><pre>var recognition = new webkitSpeechRecognition();</pre><p>Now, we have a speech recognition instance <strong>recognition</strong>. Let us define necessary checks for error detection and resetting the recognizer.</p><pre>var recognizing; function reset() { recognizing = false; } recognition.onerror = function(e){ console.log(e.error); }; recognition.onend = function(){ reset(); };</pre><p>We now define the <strong>toggleStartStop()</strong> function that will check if recognition is already being performed in which case it will stop recognition and reset the recognizer, otherwise, it will start recognition.</p><pre>function toggleStartStop() { if (recognizing) { recognition.stop(); reset(); } else { recognition.start(); recognizing = true; } }</pre><p>We can then attach a event listener to a mic button which calls the <strong>toggleStartStop()</strong> function to start or stop our speech recognition.</p><pre>mic.addEventListener(&quot;click&quot;, function () { toggleStartStop(); });</pre><p>Finally, when the speech recognizer has some results it calls the onresult event handler. We’ll use this event handler to catch the results returned.</p><pre>recognition.onresult = function (event) { for (var i = event.resultIndex; i &lt; event.results.length; ++i) { if (event.results[i].isFinal) { textarea.value = event.results[i][0].transcript; submitForm(); } } };</pre><p>The above code snipped tests for the results produced from the speech recognizer and if it’s the final result then it sets textarea value with the result of speech recognition and then we submit that to the backend.</p><p>One problem that we might face is the extension not being able to access the microphone. This can be resolved by asking for microphone access from an external tab/window/iframe. For susi_chromebot this is being done using an external tab. Pressing on the settings icon makes a new tab which then asks for microphone access from the user. This needs to be done only once, so that does not cause a lot of trouble.</p><pre>setting.addEventListener(&quot;click&quot;, function () { chrome.tabs.create({ url: chrome.runtime.getURL(&quot;options.html&quot;) }); }); navigator.webkitGetUserMedia({ audio: true }, function(stream) { stream.stop(); }, function () { console.log(&#39;no access&#39;); });</pre><p>In contrast to speech recognition, speech synthesis is very easy to implement.</p><pre>function speakOutput(msg){ var voiceMsg = new SpeechSynthesisUtterance(msg); window.speechSynthesis.speak(voiceMsg); }</pre><p>This function takes a message as input, declares a new <strong>SpeechSynthesisUtterance</strong> instance and then calls the speak method to convert the text message to voice.</p><p>There are many properties and attributes that comes with this speech recognition and synthesis interface. This blog post only introduces the very basics.</p><h3>Resources:</h3><h3>Resources:</h3><ul><li><a href="https://developers.google.com/web/updates/2013/01/Voice-Driven-Web-Apps-Introduction-to-the-Web-Speech-API">Voice Driven Web Apps: Introduction to the Web Speech API By Glen Shires</a></li><li><a href="https://developers.google.com/web/updates/2014/01/Web-apps-that-talk-Introduction-to-the-Speech-Synthesis-API">Web apps that talk — Introduction to the Speech Synthesis API By Eric Bidelman</a></li><li><a href="https://w3c.github.io/speech-api/speechapi.html">W3C Web Speech API Specification Editors: Glen Shires (Google Inc), Hans Wennborg (Google Inc)</a></li></ul><p><em>Originally published at </em><a href="https://zamhaq.github.io/blog/posts/2017-10-12/"><em>zamhaq.github.io</em></a><em> on October 12, 2017.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc143cfa6bcf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>