<?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 Dániel Stein on Medium]]></title>
        <description><![CDATA[Stories by Dániel Stein on Medium]]></description>
        <link>https://medium.com/@steindani?source=rss-1d5edb13d383------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*4FlxKRqUJsRLE5C0.jpeg</url>
            <title>Stories by Dániel Stein on Medium</title>
            <link>https://medium.com/@steindani?source=rss-1d5edb13d383------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 14:50:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@steindani/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[Transforming a Web Application into a Browser Extension]]></title>
            <link>https://medium.com/tresorit-engineering/transforming-a-web-application-into-a-browser-extension-10d246ebd94b?source=rss-1d5edb13d383------2</link>
            <guid isPermaLink="false">https://medium.com/p/10d246ebd94b</guid>
            <category><![CDATA[file-sharing]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[chrome]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[chrome-extension]]></category>
            <dc:creator><![CDATA[Dániel Stein]]></dc:creator>
            <pubDate>Tue, 17 Dec 2019 14:55:44 GMT</pubDate>
            <atom:updated>2019-12-17T14:55:44.442Z</atom:updated>
            <content:encoded><![CDATA[<p>You may realize that your workflow is slowly transforming, using one application more than any other one — your browser. You do not want to switch between applications, so you surround yourself with small tools, built-in features, extensions that complement your everyday tasks.</p><p><strong>In this article, I am going to show you how we have transformed one of our web apps¹ into a Chrome extension so it is always ready for use.</strong></p><blockquote><strong>¹ Tresorit Send. </strong>A free, end-to-end encrypted file sharing solution where you can share your files (up to 5 GB) without registering.</blockquote><p>While opening the web app every time you want to send a file in private — every business email, every Facebook or Slack message — it is easier if you have it handy and with a few clicks, you have your link on your clipboard. With our Chrome extension, this is possible.</p><h3>Anatomy of a Browser Extension</h3><p>While it has been possible to extend web browsers for quite some time now, the add-on model of the browsers was rather diverse. For more than 5 years the Browser Extension Community Group has been working on establishing a set of standards for interoperable browser extensions.</p><p>Modern web browsers are able to extend their core services and thus enable the user to tailor their experience — all with web technologies (HTML, JavaScript, and CSS). Besides the summary in this section, you can read up on modern web extensions at:</p><ul><li><a href="https://browserext.github.io/browserext">the draft specification by the Browser Extension CG</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions">the documentation by Mozilla at MDN</a></li><li><a href="https://developer.chrome.com/extensions">the documentation by Google at Chrome Developer</a></li></ul><h4>Manifest</h4><p>Extensions can provide additional behavior to the sites visited by the user, or the browser itself. As an entry point, the <strong>manifest.json</strong> file is the descriptor of the features and requirements of the extension, and the declaration of the various contexts the extension runs in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_EjRUf-_ndMu6DB3LSGdIw.png" /><figcaption>Anatomy of the extension, based on <a href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Anatomy_of_a_WebExtension">MDN documentation</a></figcaption></figure><p>An extension has several ways to tie itself to the browser. The most well-known is the <strong>browser action</strong> button that appears beside the URL bar of the browser. The extension can either put its persistent icon there or inside the URL bar as a <strong>page action</strong> for selected websites declared beforehand (with <strong>browser_action</strong> and <strong>page_action</strong> attributes respectively). Clicking on this icon activates a popup that loads the registered HTML resource.</p><p>For actions without views (loaded HTML), or long-lasting, independent computations, always-ready applications, there is also an option to run scripts or one page in the background (<strong>background</strong> attribute). This background page may be persistent or run only on specific events.</p><p>The extension can also have a designated <strong>options page</strong> (<strong>options_page</strong> attribute) available from the context menu of the <strong>browser_action</strong> and the extension lists page. <strong>Other resources</strong> can also be exposed using the <strong>web_accessible_resources</strong> attribute, such as specific HTML pages, image or script resources that internet-hosted sites or content scripts can access.</p><p>An extension can target specific websites and inject a JavaScript code (<strong>content_scripts</strong> attribute). These <strong>content scripts</strong> run in a separate context from the scripts in those websites but can modify the DOM of the page. (Using this the extension can inject scripts, and resources now running in the same context.)</p><p>For more information see the <a href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json">documentation of manifest.json on MDN</a>.</p><h4>JavaScript APIs for Web Extensions</h4><p>The scrips of a browser extension run in an extended context, where — with explicit permissions — the extension can access more than a simple website. The extension can alter the way the browser behaves on user input; access open tabs, cookies; send notifications; modify network requests, or simply change its icon. It all depends on what the user has permitted the extension to do while installing the extension.</p><p>For detailed descriptions, see the <a href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API">JavaScript APIs documentation on MDN</a>. The Tresorit Send extension only needs access to <strong>cookies</strong> on <strong>send.tresorit.com</strong> and the site itself to insert a content script that disables the <em>“Add to Chrome” </em>popup, since the user already has the extension installed. The most important API is thus the <strong>runtime</strong> module that facilitates communication inside of the extension (besides other functions), detailed in the next section.</p><h4>Communication</h4><p>With these extra APIs the scripts and processes of an extension can send and receive messages, thus communicate with other page scripts: the script running in the background, in the content script, the visited website, or even with other extensions. Messaging with native applications is also possible.</p><p><strong>runtime.connect().</strong> Make a connection between two extensions; or from a content script to the background scripts.</p><p><strong>runtime.sendMessage() / </strong><strong>runtime.onMessage.</strong> Send and listen to messages to and from the content script, the background scripts, the browser or page popups, or the options page.</p><p><strong>tabs.sendMessage() / </strong><strong>runtime.onMessage.</strong> Send and listen to messages from various extension pages to the content script running for a site.</p><h3>Tricks that Make the Tresorit Send Chrome Extension Work</h3><p>We have developed the Tresorit Send web application with the Chrome extension in mind right from the beginning. We had to make sure that the components of the web application can be reorganized so that it can follow the lifecycle of the extension while keeping all basic functionality, and even extending it. This section details the decisions and tricks we have applied to make the extension great.</p><h4>Always-Running Background Page</h4><p>The lifecycle of a web application is pretty straightforward. The user loads the page, the HTML is parsed, styling and scripts are loaded and applied. The application starts, and it runs until it is paused or the tab is closed.</p><p>The same thing applies to the registered <strong>browser_action</strong> page that opens when the user clicks on the icon of the extension. However, it is not a persistent page. It is only visible until the user clicks somewhere outside the extension, then it closes. Since our extension is uploading large files, up to 5 GB total, we can not expect the user not to use their browser until the upload is finished.</p><p>We have moved the core upload logic and everything that needs to be always running into the <strong>background</strong> page. We have implemented a runtime service routing that connects to the background page instead of the main application.</p><p>For handling static and dynamic workers and communicating between processes inside the application (main GUI thread, session handling worker, crypto workers, background page), we are using our open-source <a href="https://github.com/tresorit/WorkerServiceManager"><strong>WorkerServiceManager</strong></a> framework (similar to <a href="https://github.com/GoogleChromeLabs/comlink"><strong>comlink</strong></a>, but <strong>WSM</strong> supports IE11, workers and windows, frames, and alike with wrappers). <strong>WSM</strong> makes it possible to send and receive messages between two workers, routed through the main GUI thread. A <strong>WSM</strong> instance can be dynamically extended with temporary workers and windows as message targets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sqTGSko2iQw3ECmC3ENQtQ.png" /></figure><p>This is what we use for separating the application into a <strong>FrontendServiceManager</strong> and a <strong>BackgroundServiceManager</strong>. The former declares the services running on the <em>frontend</em> (one service, the <strong>EventService</strong> that can receive events and act upon them, such as when a file upload has completed), and has a list of what <em>remote</em> services will be registered, with message serializing “proxies” that manage type safety and message conversion. Remote services, such as <strong>LinkSessionService</strong>, <strong>PacketManagerService</strong>, or <strong>AsyncAsmCryptoService</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1LpQtknndRQbMFrPAApVtg.png" /></figure><p>When the application is running in an extension, it knows that these remote services are found outside of the context of the GUI thread. It requires a reference to the window of the background page (<strong>runtime.getBackgroundPage()</strong>), where a separate <strong>BackgroundServiceManager</strong> is instantiated and forwards the messages to it.</p><p>When the application is running hosted and not in an extension, the <strong>FrontendServiceManager</strong> simply instantiates the <strong>BackgroundServiceManager</strong> on the same GUI thread and registers the services there.</p><p>With <strong>WorkerServiceManager</strong> the services can run anywhere (such as another tab, window, or a worker), as long a <strong>WorkerServiceManager</strong> is registered there waiting for messages.</p><p>After separating the long-running services, it is up to the main application to handle the first and repeated openings. If it is the first open (such as loading the hosted web application or the first click on the <strong>browser_action</strong> icon), it opens a new upload session. In case of a repeated opening of the extension, it loads the data from the background services.</p><h4>Making the Extension ID Fixed</h4><p>Every extension gets an ID when installed. On Firefox every installment gets a unique ID, while on Chrome it gets a fixed one when installed from the Chrome Store (more on the Chrome Store, the Chrome Dev Console in a later section). Chrome extensions installed locally get a random ID.</p><p>When an extension makes a network request, the origin of the request is derived from the extension id. For Chrome this is <strong>chrome-extension://&lt;fixed-id&gt;</strong> and for Firefox it is <strong>moz-extension://&lt;random-UUID&gt;</strong>. Since the Tresorit Send servers only allow connections from selected origin domains (<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS">CORS</a>), we need fixed IDs. Unfortunately, this is not achievable on Firefox, so the browser is unsupported until these issues (<a href="https://github.com/browserext/browserext/issues/14">#14</a>, <a href="https://github.com/browserext/browserext/issues/17">#17</a>) are resolved.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bRNpIQ8ZdHbD-IgMs65w_g.png" /><figcaption>1) local version installed with production ID ⸻ 2–3) devel and feature versions of the extension installed from the Chrome Store</figcaption></figure><p>Fixed IDs for Chrome extensions are assigned on creation in the <a href="https://chrome.google.com/u/0/webstore/devconsole">Chrome Dev Console</a>. To have the same installment ID for the development version of the extension:</p><ol><li>upload a version of the extension</li><li>publish it</li><li>download the signed extension archive (<strong>.crx</strong> file) from Google from the Developer Dashboard or with a tool like <a href="https://chrome-extension-downloader.com/">Chrome Extension Downloader</a></li><li>unzip the archive, and open the <strong>manifest.json</strong> file</li><li>copy the <strong>key</strong> attribute (RSA-256 public key for the extension assigned by Google) from this <strong>manifest.json</strong> file to the source <strong>manifest.json</strong> file</li></ol><p>Now the developer version will have the same ID as the published version in the Chrome Store and overwrite the already installed local version.</p><h4>Communication with the Live Web Application</h4><p>Using the hosted Tresorit Send web app while you have the Chrome extension installed? Hey, we are not judging! But just so we do not nudge you to install the extension again, the extension tells the web app that it is present and feeling well, installed in your browser.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*29t5ryRQgjxwR5FTy1Va3g.png" /></figure><p>Unfortunately, it is not possible to detect from the web app whether the extension is already installed. So we are using <strong>content_scripts</strong> running on only the <strong>send.tresorit.com</strong> domain.</p><p>We run a script <em>after</em> the page has finished loading, which injects a small script into a DOM. This script sends the window itself a message, that is received by the <em>already listening</em> application. Otherwise, the app may get the message before the listener is declared.</p><h4>Handling Updates</h4><p>Publishing a new version of a software is exciting. While releasing a new version of a web application can mean that the next visitor will automatically get the latest and newest version of our application, we have realized that published extensions do not work like this.</p><p>Taking a look at the statistics provided by the Chrome Developer Dashboard, it is easy to see that our extension needs 2 to 4 weeks to fully deploy and update on every user’s machine. Also, a new version can be delayed with even an hour, before it is available to the public after a publish.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zgVsKIzlh2Ij9KY9NcW9iQ.png" /></figure><p>Chrome extensions with persistent background pages are reloaded only when the browser is restarted. For most users this does not happen very often. To fix this, we need to manually restart (and thus install the update to the) extension. Chrome has an API for extension update handling (<strong>chrome.runtime.onUpdateAvailable</strong>), that checks for updates with a few-hour-long interval, and notifies the extension when it found one.</p><p>When the extension gets the update available event, it checks whether every window and popup has been closed and there is no upload in progress, then silently restarts itself.</p><p>In case we really need to fix a broken release and want to force deprecate a version of the extension, the server can reject the connection with an <strong>DeprecatedClientAgent</strong> response. Then the extension displays a modal and forces Chrome to check for a new version (using <strong>runtime.requestUpdateCheck()</strong>).</p><h4>Angular Navigation</h4><p>Tresorit Send has an Angular frontend, even in the extension, as a single page application (SPA). On the hosted web application we are utilizing virtual resource paths (<strong>PathLocationStrategy</strong>, such as <strong>/home</strong> <strong>/users</strong> <strong>/login</strong>). However, when the user presses the <strong>F5</strong> refresh button while the focus is in the <strong>browser_action</strong> page, the virtual resource will not be found.</p><p>For browser extensions, the <strong>HashLocationStrategy</strong> is recommended, where the current virtual path is appended to the relative URL of the <strong>browser_action</strong> page after a <strong>#</strong> as a query string (<strong>index.html#login</strong>). This survives a page reload and displays the right component.</p><h4>Badges</h4><p>While displaying badges has an API (<strong>browserAction.setBadgeBackgroundColor()</strong>, <strong>browserAction.setBadgeText()</strong>), making it possible to display text with arbitrary background color, this API is not prepared to display custom icons, badges over the main icon.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*iLcM5L0zkaMcYoeGt6BGOw.png" /></figure><p>It is, however, possible to draw a new <strong>browser_action</strong> image onto a canvas with the same icon resource, then drawing the badge onto it, and setting it as the displayed icon (<strong>browserAction.setIcon()</strong>). With timers it is also feasible to animate the icon, displaying progress even when the popup is not open, and the user is waiting for a long-lasting operation, such as uploading large files.</p><h4>Pop-Ups and File Selection on Linux</h4><p>In Chrome the <strong>browser_action</strong> HTML resources are opened in size-constrained popups, inside the window of the browser, just below the <strong>browser_action</strong> icon. This popup can stay open as long the user does not click outside of it.</p><p>When uploading files using the Tresorit Send extension on Windows and macOS, the user can select files from the file selector, and the popup will still be open. This is not the case on Linux based operating systems. On these systems the popup closes as soon the user starts selecting files.</p><p>To solve this, the extension opens a new window on Linux, with size constraints, approximating the location where it should open. Since the location of the main <strong>browser_action</strong> resource is known, it is easy to imitate the popup behavior. This window, however, has to be closed manually.</p><p>Since this window is not counted as a view of the extension, we have to handle it being open in the force update scenario described above. (A simple window open listener and reference check is working well.)</p><h4>Content Security Policy</h4><p>Just like on the hosted Tresorit Send web application, we are setting the <strong>content_security_policy</strong> attribute in the <strong>manifest.json</strong> of the extension. This way we can declare:</p><ul><li>what domains do we allow the extension to communicate with (our servers)</li><li>from where do we accept incoming messages</li><li>which sites can insert the extension as a frame</li><li>where can images, scripts, worker files come from</li><li>where to report content security policy violations</li></ul><h3>Publishing a Chrome Extension</h3><p>Once you have a browser extension ready, with <strong>manifest.json</strong> as the entry point, the required fields set, and the resources in place, you are ready to load it into Chrome. Check whether it works correctly, then send it to the Chrome Store so everybody can use it!</p><p>In this section I detail how to get started with the Chrome Store for developers, what trick we use to have various testing releases and how to publish a new Chrome extension version on every deploy of the web application.</p><h4>Chrome Store for Developers</h4><p>To learn about how to publish your extension to the Chrome Store, please read it straight from the source: <a href="https://developer.chrome.com/webstore/publish">Publish in the Chrome Web Store</a>.</p><p>After you have a developer account, you can access two versions of the Chrome Store extension managing sites:</p><ul><li><a href="https://chrome.google.com/webstore/developer/dashboard/">Developer Dashboard</a> — the old site, that has all the necessary settings, albeit with old looks.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AmZCWyp1F35CG2UxnciNuQ.png" /></figure><ul><li><a href="https://chrome.google.com/u/1/webstore/devconsole/">Developer Console</a> — the new site with fancy editing options, but not always working functions. It does not handle store screenshot images well but can reorganize already uploaded ones. The statistics are more detailed, and it is easier to navigate this version.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2NoyG0JRPmDdkQg75_jwxQ.png" /></figure><p>You can and should use both versions when uploading your extension. If you are not working alone, do not forget to create a new group publishing account. A Google Groups backed group publishing account makes it possible to add and remove your developer colleagues. (You will have to pay the one-time developer fee for each and every account.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cNijJ8Y_JQa1IBcsl6LESQ.png" /></figure><p>Here you can set the public information for the publishing group, and edit the tester accounts that can access restricted extensions.</p><h4>Creating Test Versions for Internal Testing</h4><p>Chrome extensions can be in the following 4 categories:</p><ul><li>Unpublished</li><li>Public — Everyone (in selected countries) can see it.</li><li>Unlisted — Only people with the link can see it.</li><li>Private — Only selected testers can see it.</li></ul><p>It is not possible to have one version of an extension published to the public, while another version available only to testers. For this, you have to create another extension, that acts as a testing version.</p><p>If you do not want to lose a huge chunk of your userbase, test what happens when your extension is updated. Also, test what happens when a user with 3 months old version suddenly updates your extension. For more, read how the developers of <a href="https://www.gettoby.com/blog/post/i-almost-lost-10000-users">Toby almost lost ten thousand users</a>.</p><h4>Automated Publishing to the Chrome Store</h4><p>Every time the Tresorit Send web application is deployed, we release the Chrome extension too. After everything is copied to the <strong>dist-ext</strong> folder, it is zipped, and sent to the Chrome Store as a new version. Then the new version is published to either the testers or to the public.</p><p>For further information, read the documentation at <a href="https://developer.chrome.com/webstore/using_webstore_api">Using the Chrome Web Store Publish API</a>.</p><h4>About Signing the Extension</h4><p>Unfortunately, there is no way to sign the extension on the developers’ side, then upload it to the Chrome Store. There are two options: let Google handle the signing and thus the private key associated with the extension, or generate a private key, and upload it every time a new version is published. None of these solutions are great, but only these are our options.</p><p>Liked what you have read? Try it out live at <a href="https://send.tresorit.com/">Tresorit Send</a>, or download the <a href="https://chrome.google.com/webstore/detail/tresorit-send/ibeahcfhgmcggdgahfmnhmnjdinbakoo">Tresorit Send Chrome extension</a>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E5KMOSwzoPR9d1G3L_sIZQ.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10d246ebd94b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tresorit-engineering/transforming-a-web-application-into-a-browser-extension-10d246ebd94b">Transforming a Web Application into a Browser Extension</a> was originally published in <a href="https://medium.com/tresorit-engineering">Tresorit Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>