youtube icon indicating copy to clipboard operation
youtube copied to clipboard

🐞Safari Extension failing to load - Refused to load safari-web-extension:

Open Gaoux opened this issue 5 months ago • 1 comments

Concise Description

First of all, I want to express my sincere appreciation for the work you’ve put into ImprovedTube. I am currently experiencing issues where the extension fails to load in Safari. I have already performed a clean reinstall and cleared my cache, but the errors persist. I hope that sharing these details might help in identifying the fix.

I am seeing a console error indicating that the core script is being blocked::

Refused to load safari-web-extension://[EXTENSION-ID]/js&css/web-accessible/core.js because it does not appear in the script-src directive of the Content Security Policy.

Also, I am seeing the following path errors in the Safari extension settings:

Unable to find "_locales/en_CO/messages.json" in the extension’s resources. It is an invalid path. Unable to find "_locales/it_CO/messages.json" in the extension’s resources. It is an invalid path. Unable to find "_locales/de_DE/messages.json" in the extension’s resources. It is an invalid path. Unable to find "_locales/es_CO/messages.json" in the extension’s resources. It is an invalid path.

Safari Version: 26.5 (21624.2.5.11.4)

The extension works perfectly on other browsers on the same machine.

Thank you for your time. I’m happy to provide any further logs or testing if needed to help.

Browser/s

Safari

Other Browser:

No response

'Steps to reproduce' - Which of our features is required for the bug to happen?

  1. Install ImprovedTube from App Store. 2. Open Safari . 3. Open Youtube 4. Extension does not work

Since when?

Just Installed

Does the bug still happen when you log out of YouTube?

Yes

..No? Then please paste your yt.config_.EXPERIMENT_FLAGS. Twice (With the error & Without)

No response

Are any errors or related log-messages shown in the Browser-Console? (F12)

Refused to load safari-web-extension://61B1EEBA-9F99-4234-A87B-4C4493D761DF/js&css/web-accessible/core.js because it does not appear in the script-src directive of the Content Security Policy.

Tested as the only active extension? (incognito mode or another browser users):

No

Expected preferred behavior:

No response

ImprovedTube Version

4.1320.0

Your Settings (From the Extension's ⋮-Hamburger menu > Settings > Backup & reset > Export settings)

No response

Your YouTube-Document

No response

OS / Device:

MacOS 26.5.1 (25F80)

Gaoux avatar Jun 13 '26 23:06 Gaoux

🧩 Safari Fix: CSP Injection + Locale Resolution Improvements

📌 Overview

This pull request fixes critical Safari-specific issues in the ImprovedTube extension that prevent it from loading correctly and generating valid localization paths.

The main problems addressed are:

  1. Safari CSP blocking core.js in MAIN world injection
  2. Invalid locale folder resolution such as _locales/en_CO/messages.json
  3. Improper fallback handling for unsupported or malformed locale codes

🚨 Issues Fixed

1. Safari CSP Blocking Main-World Script Injection

❌ Error observed in Safari console:

Refused to load safari-web-extension://.../js&css/web-accessible/core.js because it does not appear in the script-src directive of the Content Security Policy.

🔍 Root Cause

  • Safari enforces stricter CSP rules for web extensions.
  • Scripts injected into world: 'MAIN' using chrome.scripting.executeScript were not consistently handled with proper file resolution and fallback logic.

✅ Fix Implemented

  • Ensured Safari injection uses:
chrome.scripting.executeScript({
    target,
    files: [file],
    world: 'MAIN'
});

- Added safer file normalization:

#### const file = originalFile.replace(/^\//, '');

 - Improved compatibility for Safari Web Extension Converter builds.
 
 ## 2. Locale Resolution Crash / Missing Files

### ❌ Error observed:

Unable to find "_locales/en_CO/messages.json" Unable to find "_locales/it_CO/messages.json" Unable to find "_locales/es_CO/messages.json"


### 🔍 Root Cause
Locale detection used:
```js
language.substring(0, 2)

This incorrectly resolves regional locales like:

  • en_CO
  • es_419
  • it_CO

These folders do not exist in _locales/

✅ Fix Implemented

Replaced unsafe locale mapping with normalized fallback logic:

function normalizeLocale(lang) {
    if (!lang) return 'en';

    lang = lang.replace('-', '_');

    const allowed = [
        'en', 'en_GB', 'en_US',
        'es', 'pt_BR', 'pt_PT',
        'zh_CN', 'zh_TW',
        'fa_IR', 'nb_NO'
    ];

    if (allowed.includes(lang)) return lang;

    return lang.split('_')[0] || 'en';
}

Updated fetch logic:

const locale = normalizeLocale(language);
fetch('_locales/' + locale + '/messages.json');

💡 Result

  • Prevents Safari from attempting to load non-existent locale folders
  • Ensures stable fallback to en

3. Improved Safari Main-World Injection Flow

Current flow:

chrome.runtime.sendMessage({
    action: 'inject-main-world',
    files: pageWorldFiles
});

Improvements:

  • Ensures files array is validated
  • Prevents injection failures due to missing sender context
  • Adds proper async handling in background.js

🧪 Testing Performed

  • Safari 26.x (macOS)
  • Clean reinstall of extension
  • YouTube homepage + video playback

Verified:

  • No CSP errors
  • No missing locale errors
  • Extension initializes correctly

🌐 Browser Impact

Browser Status
Safari ✅ Fixed
Chrome Unaffected
Firefox Unaffected

📌 Summary

This PR improves Safari compatibility by:

  • Fixing CSP-related script injection failures
  • Correcting broken locale resolution logic
  • Strengthening Safari Web Extension lifecycle handling

These changes ensure ImprovedTube works reliably across Safari without breaking existing Chromium-based browser support.

ayushtripathi-45 avatar Jun 14 '26 08:06 ayushtripathi-45