実現したいこと
Chrome拡張機能のプログラムでUncaught TypeErrorを回避したい。
前提
初質問のプログラミング初心者です。
Chrome拡張機能でkuromojiライブラリを使用したいのですが下記のエラーが発生します。
そもそもkuromojiライブラリと関係のないエラーな気もします。
エラーのないソースコードにしたいです。
よろしくお願いいたします。
発生している問題・エラーメッセージ
Uncaught TypeError: Cannot read properties of undefined (reading 'onUpdated')
at script.js:1:13
該当のソースコード
manifest.json
{
"manifest_version": 3,
"name": "a",
"description": "a",
"version": "0.0.1",
"permissions": [
"activeTab",
"<all_urls>",
"tabs",
"://example.com/"
],
"content_scripts": [
{
"matches": [
"://example.com/",
"<all_urls>"
], "js": [ "kuromoji/build/kuromoji.js", "script.js" ] } ], "background.service_worker": { "scripts": ["script.js"], "persisten,t": false }, "web_accessible_resources": [{ "resources": [ "/kuromoji/dict/*", "script.js" ], "matches": [ "<all_urls>" ] }], "icons": { "128": "icon128.png" }, "action": { "default_title": "a", "default_icon": "icon128.png" }
}
script.js
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
// Do something when the tab is updated
console.log(tabId, changeInfo, tab);
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) {
if (changeInfo.status === 'complete') {
chrome.tabs.executeScript(tabId, { code: "var bodyText = document.body.innerText; console.log(bodyText); const dicPath = chrome.extension.getURL('/kuromoji/dict/'); kuromoji.builder({ dicPath: dicPath }).build(function (err, tokenizer) { var path = tokenizer.tokenize(bodyText); console.log(path); });" });
}
})
試したこと
マニフェストのバージョンの2と3を切り替えたり、それに合わせてmanifest.jsonの構文を変えたりしました。
0 コメント