<?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: Bing Qiao</title>
    <description>The latest articles on DEV Community by Bing Qiao (@bingqiao).</description>
    <link>https://dev.to/bingqiao</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%2F784770%2Fcf1192b0-221d-49d7-9c5a-1f83dfefb885.png</url>
      <title>DEV Community: Bing Qiao</title>
      <link>https://dev.to/bingqiao</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bingqiao"/>
    <language>en</language>
    <item>
      <title>Creating a browser extension for Safari and Chrome</title>
      <dc:creator>Bing Qiao</dc:creator>
      <pubDate>Wed, 19 Jan 2022 21:14:20 +0000</pubDate>
      <link>https://dev.to/bingqiao/creating-a-browser-extension-for-safari-and-chrome-493m</link>
      <guid>https://dev.to/bingqiao/creating-a-browser-extension-for-safari-and-chrome-493m</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5m3nyylfe4fr5vlwkr1p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5m3nyylfe4fr5vlwkr1p.png" alt="Projects diagram"&gt;&lt;/a&gt;&lt;br&gt;
This article is not a detailed tutorial on how to create Web extensions for either Safari or Chrome. It is mainly an introduction to two demo projects hosted on Github on how to develop extensions that work on both Safari and Chrome (possibly Mozilla Firefox but not tested), using React/TypeScript/esbuild.&lt;/p&gt;

&lt;p&gt;Safari extension requires a Swift project that contains iOS/macOS parent apps plus their extension apps that share a bunch of JavaScript and other resources.&lt;/p&gt;

&lt;p&gt;The extension from my first attempt &lt;a href="https://medium.com/@bingqiao/safari-auto-refresh-web-extension-javascript-only-sort-of-9298970ec0ac" rel="noopener noreferrer"&gt;here&lt;/a&gt; was a crude implementation in plain, vanilla JavaScript. There was no bundling, minifying, framework or typing. There wasn't even a separate JavaScript project. All JavaScript&amp;amp;resources belonged to the Swift project and were managed by Xcode.&lt;/p&gt;

&lt;p&gt;After some more research and learning, I recreated the same extension using React/TypeScript, not just for Safari but Chrome too. The new project uses esbuild to create bundled and minified code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The extension project for Safari and Chrome
&lt;/h2&gt;

&lt;p&gt;A much stripped down version of the extension resources project is hosted here &lt;a href="https://github.com/bingqiao/browser-ext-react-esbuild" rel="noopener noreferrer"&gt;browser-ext-react-esbuild&lt;/a&gt; while the container app for iOS/macOS is hosted here &lt;a href="https://github.com/bingqiao/browser-ext" rel="noopener noreferrer"&gt;browser-ext&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first issue I had to address was how to create a Web extension using React/TypeScript/esbuild. Luckily there is already a template project that does exactly just that. &lt;a href="https://github.com/martonlederer/esbuild-react-chrome-extension" rel="noopener noreferrer"&gt;esbuild-react-chrome-extension&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next issue is how to code in TypeScript against Web extension API for both Safari and Chrome. As it turns out Safari and Mozilla Firefox are very similar in their API but there are enough differences between them and Chrome to require different treatment especially when it comes to the use of "callbacks" and "promises" &lt;a href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Build_a_cross_browser_extension" rel="noopener noreferrer"&gt;Building a cross-browser extension&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Initially I created wrapper functions to convert Chrome functions that require callback to return promise instead. The better approach, as I found out later, is probably to use   &lt;a href="https://github.com/mozilla/webextension-polyfill/" rel="noopener noreferrer"&gt;webextension-polyfill&lt;/a&gt; from Mozilla and its &lt;a href="https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/webextension-polyfill" rel="noopener noreferrer"&gt;types&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A caveat here is, I had to set &lt;code&gt;module&lt;/code&gt; in "tsconfig.json" to &lt;code&gt;"commonjs"&lt;/code&gt; as shown below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
  "compilerOptions": {
    ...
    "module": "commonjs",
    ...
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then do import assignment in JavaScript files that call extension API:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import browser = require('webextension-polyfill');


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Using &lt;code&gt;import&lt;/code&gt; like below didn't work for me:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import * as browser from 'webextension-polyfill';


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code generated by esbuild for the &lt;code&gt;import&lt;/code&gt; above calls &lt;code&gt;__toESM&lt;/code&gt; for &lt;code&gt;require_browser_polyfill()&lt;/code&gt; which renders the polypill proxy ineffective.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;var browser2 = __toESM(require_browser_polyfill());&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  The container Swift project for Safari&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Another issue is how to manage the React/extension project with the container Swift project.&lt;/p&gt;

&lt;p&gt;The boilerplate extension resources (JavaScript/css, manifest and html files) created with a new Safari extension project are managed by Xcode. But I need them to be simply copied over from the React project, instead of having Xcode creating reference for every JavaScript/html/css/image file that needs to be part of the bundle it creates.&lt;/p&gt;

&lt;p&gt;The figure below shows how those resource files are added to the Swift bundle after a Safari extension project is created in Xcode.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbthhs3ctb0uho9afyunt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbthhs3ctb0uho9afyunt.png" alt="How extension resources get added to Swift app bundle by default"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The problem is, we might have different files from the React project depending on whether it's a prod or dev build, especially if the bundler (such as Parcel) used generates randomised file names.&lt;/p&gt;

&lt;p&gt;Instead, create an empty folder such as &lt;code&gt;build&lt;/code&gt; under extension &lt;code&gt;Resources&lt;/code&gt; via "finder" (not in Xcode).&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcqkqrscrviyvyozu4ojb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcqkqrscrviyvyozu4ojb.png" alt="Create a new empty folder "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then add this new empty folder to &lt;code&gt;Resources&lt;/code&gt; in Xcode.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8sp5i25gc6mkynnpdjtd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8sp5i25gc6mkynnpdjtd.png" alt="Add folder to "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, add the folder to &lt;code&gt;Copy Bundle Resources&lt;/code&gt; build phase. This needs to be done for both iOS and macOS extension targets.&lt;/p&gt;

&lt;p&gt;Now, all it takes to import new extension resources from the React project is to copy everything over to &lt;code&gt;Resources/build&lt;/code&gt; folder in the Swift project.&lt;/p&gt;

&lt;p&gt;The two sample projects are setup to work together as long as they are checked out side-by-side in the same directory.&lt;/p&gt;

&lt;p&gt;Now you can develop and test the extension against Chrome solely in the extension resources project. To test against Safari, just run an npm command to build extension resources and copy contents of &lt;code&gt;dist&lt;/code&gt; to the container Swift project, then build/run it in Xcode.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mechanism
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fllep3xn2qli5xu1dvl9g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fllep3xn2qli5xu1dvl9g.png" alt="auto-refresh"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Auto-refresh is implemented using &lt;code&gt;setTimeout()&lt;/code&gt;, &lt;code&gt;browser.tabs.reload()&lt;/code&gt; and &lt;code&gt;browser.storage.local&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every managed (marked to auto-refresh) browser tab has an entry in a map persisted in extension storage local: &lt;code&gt;tabId: boolean&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;Upon loading, &lt;code&gt;content.js&lt;/code&gt; looks up its tabId in this map;&lt;/li&gt;
&lt;li&gt;If there is an entry and the result is &lt;code&gt;true&lt;/code&gt;, &lt;code&gt;content.js&lt;/code&gt; will set up a timer of fixed interval (obviously the interval can be exposed to users too) to send a runtime message to &lt;code&gt;background.js&lt;/code&gt;, asking for reload;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;background.js&lt;/code&gt; receives the request and reloads the sender tab via &lt;code&gt;browser.tabs.reload()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The approach above is different from my first attempt on auto-refresh extension:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I was using a variable in &lt;code&gt;background.js&lt;/code&gt; to hold tabs states which proves problematic. In Safari iOS, property &lt;code&gt;persistent&lt;/code&gt; for &lt;code&gt;background.js&lt;/code&gt; in &lt;code&gt;manifest.json&lt;/code&gt; needs to be &lt;code&gt;false&lt;/code&gt;, which means it can and will get reloaded. That explains why the extension was losing tab states whenever iPhone screen went dark. Using &lt;code&gt;browser.storage.local&lt;/code&gt; seems to be the only viable alternative to tackling this issue, even though it adds quite a bit of complexity to the code base.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;browser.storage.local&lt;/code&gt;, I now have to figure out a way to clean up tabs states once the browser is restarted. This is a bit tricky for Safari which does not implement extension session API. The approach I used is to do a clean up in &lt;code&gt;browser.runtime.onStartup&lt;/code&gt; event. This seems to work well but I'm not certain how water-tight this is.&lt;/li&gt;
&lt;li&gt;I was using &lt;code&gt;window.location = window.location.href&lt;/code&gt; to do the reload. The better way is to call extension API &lt;code&gt;browser.tabs.reload()&lt;/code&gt; which allows &lt;code&gt;bypassCache&lt;/code&gt; argument to bypass browser cache.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Being able to test Safari extension easily in Chrome during development has saved me a lot of time. I'd be interested to hear if you have different approaches to some issues raised here.&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>safari</category>
      <category>esbuild</category>
    </item>
  </channel>
</rss>
