Kullanıcının şu anda açık olduğu web sayfasındaki tüm resimleri bulmaya çalışıyorum ve kullanıcı uzantıyı tıkladığında bunları gösteriyorum. Birçok gönderiye baktım ve bunu yapmanın en iyi yolunu buldum: popup.js komut dosyası, etkinken content.js komut dosyasına sahip olmalı ve sonra content.js betiğinin resimleri almasını ve geri göndermesini sağladım. popup.js komut dosyasına. Ancak, her zaman bir hata alıyorum (aşağıda gösterilmiştir). Bu alakalı dosyalar şunlardır:Chrome Uzantısı hatası, içerik komut dosyasından açılan komut dizisine dizi göndermeye çalışıyor
manifest.json
{
"manifest_version": 2,
"name": "Picture Finder",
"version": "1.0",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"run_at": "document_end",
"all_frames": false
}
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs"
]
}
popup.html
<!DOCTYPE html>
<html>
<head>
<title>Picture Finder</title>
<script src="popup.js"></script>
</head>
<body>
<h1><center>Picture Finder</center></h1>
<img id="pic" src="" height="400" width="400">
</body>
</html>
popup.js
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.runtime.sendMessage(tabs[0].id, {type: "getContent"},
function(response) {
console.log(response); //this never gets logged
document.getElementById('pic').src = response[0].src;
});
});
content.js
var picArray = document.getElementsByTagName("img");
var srcArray = [];
for(var i = 0; i < picArray.length; i++){
srcArray.push(picArray[i].src);
}
//console.log(picArray); //this gets logged correctly
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
switch(message.type){
case "getContent":
console.log(srcArray);//this never gets logged
sendResponse(srcArray);
break;
default:
console.error("unexpected message: ", message);
}
});
Error in response to tabs.query: Error: Invalid value for argument 3. Property 'type': Unexpected property. at Object.callback (chrome-extension://mhnnhbbcahnfjmfgofalcmikdedelicg/popup.js:2:17) at chrome-extension://mhnnhbbcahnfjmfgofalcmikdedelicg/popup.js:1:13
Ben krom uzantıları geliştirmek için çok yeni ve gerçekten bu sorunu gidermek için nasıl bir fikrin yok:
benim uzantısını tıklayarak ve onu teftiş yaparken aşağıdaki hatayı alıyorum fark ettim. Hata, geri arama işlevinin chrome.tabs.query dosyasında geçersiz bir argüman olduğunu öne sürüyor gibi görünüyor, ancak nasıl emin değilim. Yardım için teşekkürler. Arka plan.js komut dosyasının boş bir dosya olduğunu unutmayın.
Bu chrome.runtime.sendMessage dosyasında (sekmeler [0] .id, {type: "getContent"}, 'neden sekme kimliğini geçiriyorsunuz? Gerekliyse, 'chrome.runtime.sendMessage olarak iletmelisiniz. ({id: tabs [0] .id, type: "getContent"}, function() {}) '. – Apb
@RafiqueMohammed yukarısına yazılır! Sadece bir fonksiyonum var – patrickd
@Apb False, çağrı doğru – Xan