2

I am writing a chrome extension that one-click toggles javascript for the browser (all pages), but I want those sites that I've whitelisted to still have JS permissions. From the Chrome Extension API documents, it seems like setting 'block' on chrome.contentSettings.javascript should not override the site-specific allow list I have manually entered in chrome's JS settings.

Am I reading the API wrong? Does the extension always override the whitelist?

More detail:

const { setting } = await chrome.contentSettings.javascript.get({ primaryUrl: 'http://*/*' });
const newSetting = setting === 'allow' ? 'block' : 'allow';

chrome.contentSettings.javascript.set({
  primaryPattern: '<all_urls>',
  setting: newSetting
});

This code works great, but when the setting is toggled to 'block', it blocks all javascript from running, even those sites for which I have a whitelist in chrome://settings/content/javascript, e.g.:

screenshot of chrome's javascript settings showing claude.ai being whitelisted

Full extension code:

chrome.runtime.onInstalled.addListener(() => {
  console.log('JS Toggle extension installed');
  updateIcon();
});

// Update icon based on current JavaScript setting
async function updateIcon() {
  const { setting } = await chrome.contentSettings.javascript.get({ primaryUrl: 'http://*/*' });
  const enabled = setting === 'allow';

  chrome.action.setTitle({
    title: `JavaScript ${enabled ? 'ON' : 'OFF'} - Click to toggle`
  });

  chrome.action.setBadgeText({
    text: enabled ? 'ON' : 'OFF'
  });

  chrome.action.setBadgeBackgroundColor({
    color: enabled ? '#4CAF50' : '#F44336'
  });
}

// Toggle JavaScript on icon click
chrome.action.onClicked.addListener(async () => {
  const { setting } = await chrome.contentSettings.javascript.get({ primaryUrl: 'http://*/*' });
  const newSetting = setting === 'allow' ? 'block' : 'allow';

  await chrome.contentSettings.javascript.set({
    primaryPattern: '<all_urls>',
    setting: newSetting
  });

  updateIcon();
});

I've tried changing <all_urls> to http*://*/* and other variants, to no avail. I don't want to go to site-specific banning of JS, but that could be a solution if nothing else works.

1
  • It apply this JavaScript setting (allow/block) to all websites, overriding the default global behavior. You can apply this setting on specific tab . Commented Oct 18 at 19:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.