⚠️自分はプログラミング初心者なので大幅に勘違いしているところがあるかもしれませんがよろしくお願いいたします。
実現したいこと
Google拡張機能で外部ライブラリを使用したい。
具体的な目的はkuroshiroを使用して、サイト上の文字を全てひらがなに置き換えたい。
前提
Google Chrome 拡張で実行したらサイト上の文字列が全てひらがなに置き換えられるものを作りたい。
gooのひらがな化APIを使用して目的のものは作れたが毎回APIを使うとなると制限がかかったりしそうだと考え、漢字を含めてひらがな化できるkuroshiroを使用したいと思った。
https://github.com/hexenq/kuroshiro
npmでkuroshiro及びkuroshiro-analyzer-kuromojiを作業ディレクトリにダウンロードししたが使えそうにない。
発生している問題・エラーメッセージ
Uncaught SyntaxError: Cannot use import statement outside a module
該当のソースコード
background.js
1import Kuroshiro from 'kuroshiro'; //ここがダメらしい 2import KuromojiAnalyzer from 'kuroshiro-analyzer-kuromoji'; 3 4chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => { 5 if (request.action === 'convertToHiragana') { 6 7 const kuroshiro = new Kuroshiro(); 8 await kuroshiro.init(new KuromojiAnalyzer()); 9 var result = await kuroshiro.convert(request.sentence, { to: 'hiragana' }); 10 try { 11 sendResponse(result); 12 } catch (e) { 13 sendResponse(""); 14 console.log(e); 15 } 16 } 17 return true; 18});
content.js
1function Change() { 2 const elements = document.querySelectorAll('body *'); 3 for (let element of elements) { 4 if (element.children.length === 0 && element.textContent.trim() !== '') { 5 chrome.runtime.sendMessage( 6 { action: 'convertToHiragana', sentence: element.textContent.trim() }, 7 response => { 8 element.textContent = response; 9 } 10 ); 11 } 12 } 13 14}
試したこと
調べて色々追加してみた
manifest.json
1 "content_scripts": [ 2 { 3 "matches": [ 4 "<all_urls>" 5 ], 6 "js": [ 7 "node_modules/kuroshiro/dist/kuroshiro.js", 8 "node_modules/kuroshiro-analyzer-kuromoji/dist/kuroshiro-analyzer-kuromoji.js", 9 "content.js" 10 ] 11 } 12 ],
manifest.json
1 "background": { 2 "scripts": [ 3 "node_modules/kuroshiro/dist/kuroshiro.js", 4 "node_modules/kuroshiro-analyzer-kuromoji/dist/kuroshiro-analyzer-kuromoji.js", 5 "background.js" 6 ] 7 }
以下も追加してみた
popup.html
1 <script type="module" src="popup.js"></script> 2 <script type="module" src="node_modules/kuroshiro/dist/kuroshiro.js"></script> 3 <script type="module" src="node_modules/kuroshiro-analyzer-kuromoji/dist/kuroshiro-analyzer-kuromoji.js"></script>
以上の変更をしてみたがエラーが改善されることはなかった。
補足情報(FW/ツールのバージョンなど)
gooのAPIを使用し、目的の動作は一旦できたのでbackground.js以外はライブラリ関連を除いて問題はないと思います。
background.jsの内容だけ置き換えようとした感じです。
gooAPIで実装したとき(動作した)↓
background.js
1chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { 2 if (request.action === 'convertToHiragana') { 3 const apiUrl = 'https://labs.goo.ne.jp/api/hiragana'; 4 const appId = <ここにAPIキー>'; 5 const payload = { 6 app_id: appId, 7 sentence: request.sentence, 8 output_type: 'hiragana' 9 }; 10 fetch(apiUrl, { 11 method: 'POST', 12 headers: { 13 'Content-Type': 'application/json' 14 }, 15 body: JSON.stringify(payload) 16 }) 17 .then(response => response.json()) 18 .then(data => { 19 sendResponse(data.converted); 20 }) 21 .catch(error => { 22 console.error(error); 23 sendResponse(''); 24 }); 25 return true; 26 } 27});
自分で調べてもあまり情報が見つからなかったです、、
よろしくお願いします。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/06/26 04:27 編集
2023/06/29 14:23