How to Build a Safari QR‑Code Extension from Scratch

This step‑by‑step guide shows how to create a Safari web extension that generates a QR code for the current page, covering project setup in Xcode, manifest configuration, UI design, JavaScript communication, and optional offline QR generation.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
How to Build a Safari QR‑Code Extension from Scratch

Overview

Safari web extensions are less mature than Chrome. This guide shows how to build a Safari Extension App that generates a QR code for the active page URL.

Project creation

In Xcode create a new Safari Extension App using the Multiplatform template (iPhone + macOS). Xcode generates the required targets and folder layout.

Folder layout

Shared (App): Main.html, Icon.png, Style.css, Script.js, ViewController.swift

Shared (Extension): _locales, images, manifest.json, background.js, content.js, popup.html, popup.css, popup.js

manifest.json

{
  "content_scripts": [{
    "js": ["content.js"],
    "matches": ["http://*/*", "https://*/*", "<all_urls>"],
    "run_at": "document_idle"
  }],
  "host_permissions": ["http://*/*", "https://*/*"],
  "permissions": ["activeTab", "tabs", "scripting", "messaging"]
}

This grants the extension access to all pages and allows querying the active tab and messaging.

Popup UI

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" href="popup.css">
  <script type="module" src="popup.js"></script>
</head>
<body>
  <div id="loader"></div>
  <div id="img_back">
    <img id="qrcode" src=" " alt="QR Code"/>
  </div>
</body>
</html>
body { width: 360px; height: 360px; text-align: center; margin: 0; background-color: white; }
#loader { position: absolute; left: 50%; top: 50%; width: 120px; height: 120px; border: 16px solid #f3f3f3; border-top: 16px solid #3498db; border-radius: 50%; animation: spin 2s linear infinite; transform: translate(-50%, -50%); }
#img_back { position: absolute; left: 50%; top: 50%; width: 320px; height: 320px; transform: translate(-50%, -50%); }
#qrcode { width: 100%; border-radius: 9px; }
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }

JavaScript logic

popup.js queries the active tab, sends a message to content.js to obtain the current URL, builds a QR‑code request URL, and sets the src of the image. The loading spinner is hidden once the image loads.

const kQRAPI = "https://qrcode.tec-it.com/API/QRCode?data=";
function generateQRCode(methodName, message) {
  browser.tabs.query({active: true, currentWindow: true}, function(tabs) {
    const activeTab = tabs[0];
    browser.tabs.sendMessage(activeTab.id, {title: methodName, message: message}, function(res) {
      if (res.title == "targetURL") {
        const encoded = encodeURIComponent(res.urlStr);
        const img = document.getElementById("qrcode");
        img.onload = () => document.getElementById("loader").style.display = "none";
        img.src = kQRAPI + encoded + "&istransparent=true";
      }
    });
  });
}
generateQRCode("getPageURL", "generate current page URL");

content.js runs in the page context and returns the page URL when requested.

browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.title == "getPageURL") {
    sendResponse({title: "targetURL", urlStr: window.location.href});
  }
});

Running the extension

Build and run the macOS target. The extension appears in Safari’s toolbar; clicking the icon opens the popup and immediately shows the QR code for the active page.

Offline QR generation (optional)

Replace the external API with a pure‑JavaScript QR‑code library (e.g., qrcode.js) to generate the image locally, eliminating network latency.

References

Apple Safari Web Extensions documentation

Chrome Web Extensions guide

MDN compatibility tables for manifest.json GitHub – QR‑Code extension example

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptXcodeTutorialQR codeSafari ExtensionWebExtension
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.