0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

scripting.executeScript実行時、URL Permissionエラーの解決法

Last updated at Posted at 2023-04-20

変更前

manifest.json

{
    ...
    "manifest_version": 3,
    "permissions": [
		"tabs",
		"scripting"
	],
    ...
}

content.js

chrome.runtime.sendMessage({}, () => {});

background.js

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
    getNextTabAction();    
	return true;
});


async function getNextTabAction() {

    const next_tab = ...;
	
    chrome.scripting.executeScript({
        target: {tabId: next_tab},
        function: () => {
            console.log("-_-");
        }
      });
          
	return true;
}

エラーコード

caught (in promise) Error: Cannot access contents of url "「タブのURL~」".
Extension manifest must request permission to access this host.

変更後

manifest.json

{
    ...
    "manifest_version": 3,
    "permissions": [
		"tabs",
		"scripting"
	],
	// 以下を追加するだけ
	"host_permissions": ["http://*/*", "https://*/*"],
    ...
}

これで目標のタブ(今回はnext_tab)のコンソールに「-_-」が追加されると思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?