You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Chrome扩展新标签页执行脚本失效,疑与新政策相关

Hey there! Let’s break down why your Chrome extension stopped working—since you suspect it’s tied to Chrome’s recent extension policy changes, you’re absolutely on the right track. These updates have tightened up permissions, script execution rules, and manifest requirements, so let’s walk through the most likely issues and fixes step by step.

1. Fix Your Host Matching Rule & Permissions

First off, the domain pattern you mentioned (://.website.com/*) is invalid. Chrome’s host matching requires a scheme wildcard (*://) and a proper wildcard for subdomains. You’ll need to update this to:

*://*.website.com/*
  • For Manifest V3: Add this to the host_permissions array in your manifest, and make sure you’ve included the scripting permission if you’re dynamically injecting scripts.
  • For Manifest V2: Include the pattern in the permissions array (though note that V2 extensions are being phased out by Chrome, so upgrading to V3 is strongly recommended).

Example Manifest V3 snippet:

{
  "manifest_version": 3,
  "name": "Your Extension",
  "version": "1.0",
  "permissions": ["scripting", "tabs"],
  "host_permissions": ["*://*.website.com/*"],
  "background": {
    "service_worker": "event.js"
  },
  "action": {
    "default_icon": "icon.png"
  }
}

2. Fix New Tab Script Injection Logic

Your event.js opens a new tab and needs to execute thirdFunction.js inside it—Chrome’s new policies have strict rules around when and how you can inject scripts:

  • Wait for the tab to finish loading: Don’t try to inject scripts immediately after creating the tab. Use chrome.tabs.onUpdated to listen for the complete status before injecting.
  • Manifest V3 note: Background scripts are now service workers, which don’t have access to the DOM. Make sure your event.js doesn’t rely on DOM APIs (like document) that will fail in a service worker context.

Example updated event.js logic:

chrome.action.onClicked.addListener((activeTab) => {
  // Verify we're on the target domain before proceeding
  if (!activeTab.url.includes('website.com')) return;

  chrome.tabs.create({ url: 'https://your-specific-link.website.com' }, (newTab) => {
    // Set up a one-time listener for when the tab loads
    const loadListener = (tabId, info) => {
      if (tabId === newTab.id && info.status === 'complete') {
        chrome.tabs.onUpdated.removeListener(loadListener);
        // Inject thirdFunction.js into the loaded tab
        chrome.scripting.executeScript({
          target: { tabId: newTab.id },
          files: ['thirdFunction.js']
        });
      }
    };
    chrome.tabs.onUpdated.addListener(loadListener);
  });
});

3. Diagnose thirdFunction.js Failure

If thirdFunction.js was working before but now doesn’t return a list, check these common policy-related issues:

  • Content Security Policy (CSP) Violations: Manifest V3 enforces a strict default CSP (script-src 'self'). If your script uses eval(), new Function(), or inline event handlers, Chrome will block it. Rewrite those parts to avoid unsafe code.
  • Isolated World Context: Content scripts run in an isolated world separate from the page’s JavaScript. If thirdFunction.js relies on accessing page-specific global variables, you’ll need to use chrome.runtime.sendMessage or custom DOM events to communicate between the content script and the page.
  • Check for Errors: Open Chrome’s DevTools for the extension:
    1. Go to chrome://extensions/
    2. Enable "Developer mode"
    3. Click "Service Worker" (for V3) or "Background page" (for V2) next to your extension to open its console
    4. Also, inspect the new tab your extension opens and check the console for content script errors

4. Upgrade to Manifest V3 (If You Haven’t Already)

Chrome has started restricting Manifest V2 extensions—they can’t be published to the Web Store anymore, and existing ones will stop working in future Chrome versions. If your extension is still on V2, migrating to V3 is critical to keep it functional.

内容的提问来源于stack exchange,提问作者Asaf Kfir

火山引擎 最新活动