前提・実現したいこと
Googleドライブにアップした動画ファイルの編集画面から、
サムネイル画像を保存するためにChrome拡張機能で
HTML imgタグ内のsrcを取得したいです。
発生している問題・エラーメッセージ
JavaScriptでsrc取得コードを実行すると値が取得できますが、
chrome.tabs.executeScriptで実行するとnullになってしまいます。
試しにspanタグのinnerTextをchrome.tabs.executeScriptで
取得したところうまく取得できました。
imgタグ/srcの取得がChrome拡張では出来ないかと思ったのですが、
同様のエラーの質問を見つけられず困っています。
該当のソースコード
■popup.js
'use strict'; var results = document.getElementById('results'); var id = document.getElementById('idresults'); var getText = []; chrome.tabs.query({lastFocusedWindow: true},function(tabs){ for (var i = 0; i < tabs.length; i++) { chrome.tabs.executeScript(tabs[i].id,{ code:"document.querySelector('.ndfHFb-c4YZDc-aTv5jf-bVEB4e img').src;"}, // codeを"document.querySelector('.l1HEBf-r4nke').innerText;"に差し替えるとテキストは取得できる function(result){ getText.push(result); console.log(result); results.value = getText.join("\n"); results.select(); }); }});
■manifest.json
{ "manifest_version":2, "name":"get_thumbnail", "version":"1.0", "browser_action":{ "default_popup":"popup.html" }, "permissions":[ "tabs", "notifications", "https://drive.google.com/*" ]}
■popup.html
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>My Extentions</title> </head> <body> <textarea id="results" rows="5" cols="40"></textarea> <script src="popup.js"></script> </body> </html>
試したこと
・開発者ツールのコンソールでJavaScriptを実行
⇒nullにならずsrcを取得できた
document.querySelector('.ndfHFb-c4YZDc-aTv5jf-bVEB4e img').src;
・popup.jsの取得対象タグを別のinnerTextがあるものに変更
⇒nullにならず取得できた
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。