私は最初の「すべてをまとめる」Chrome 拡張機能に戸惑っています。何をしようとしているのか、次にスクリプトの抜粋を使用してどのように取り組んできたのかを説明します。
- options.html ページと、ユーザーがテキスト フィールドに URL を設定できるようにする options.js スクリプトがあります。これは、localStorage を使用して保存されます。
function load_options() { var repl_adurl = localStorage["repl_adurl"]; default_img.src = repl_adurl; tf_default_ad.value = repl_adurl; } function save_options() { var tf_ad = document.getElementById("tf_default_ad"); localStorage["repl_adurl"] = tf_ad.value; } document.addEventListener('DOMContentLoaded', function () { document.querySelector('button').addEventListener('click', save_options); }); document.addEventListener('DOMContentLoaded', load_options );
- 私の contentscript は、スクリプト 'myscript' をページに挿入します (そのため、ページの html から img 要素にアクセスできます)
var s = document.createElement('script'); s.src = chrome.extension.getURL("myscript.js"); console.log( s.src ); (document.head||document.documentElement).appendChild(s); s.parentNode.removeChild(s);
- myscript.js は、何らかの方法でローカル ストレージ データを取得し、画像要素の操作方法を決定することになっています。
HTML ソースから画像を取得するのに問題はありませんが、localStorage データにアクセスできないようです。環境が異なる2つのスクリプトに関係しているに違いないことはわかっていますが、この問題を克服する方法がわかりません. html ソースにアクセスできます。
うまくいけば、ここの誰かが私が見逃していることを提案してくれるでしょう。
ありがとう、私はあなたが提供できる助けに感謝します!
-アンディ