2013-05-01 15 views
10

geçen Popup.js mesaja DOM ...Chrome Uzantısı - Bir mesaj/değişkeni aşağıdaki adımların her akar çalışan basit bir Google Chrome uzantısını almaya çalışıyorum

  1. DOM içeriği Gönderen (belirli HTML etiketi)
  2. Contentscript.js
  3. background.js
  4. Popup.js
  5. Popup.html

Ben bir yönde (Background.js -> Popup.js veya Background.js -> Contentscript.js) içinde ondan bir mesaj/değişken için background.js ve göndermek nasıl düşündüm, ama başarılı üçü (Contentscript.js -> Background.js -> Popup.js) aracılığıyla elde edemezsiniz. İşte demodaki dosyalar.

Dom

<h1 class="name">Joe Blow</h1>

Content.js

fromDOM = $('h1.name').text(); 

chrome.runtime.sendMessage({contentscript: "from: contentscript.js", title: fromDOM}, function(b) { 
    console.log('on: contentscript.js === ' + b.background); 
}); 

background.js

chrome.tabs.getSelected(null, function(tab) { 
    chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) { 

     sendResponse({background: "from: background.js"}); 
     console.log('on: background.js === ' + msg.title); 

    }); 
}); 
.210

Popup.js

chrome.extension.sendMessage({pop: "from: popup.js"}, function(b){ 
    console.log('on: popup.js === ' + b.background); 

    $('.output').text(b.background); 
}); 

Popup.html

<html> 
<head> 
    <script src="jquery.js"></script> 
    <script src="popup.js"></script> 
</head> 
<body> 

<p class="output"></p> 

</body> 
</html> 

manifest.json içimde bir ses

{ 
"name": "Hello World", 
"version": "1.0", 
"manifest_version": 2, 
"description": "My first Chrome extension.", 
"background" : { 
    "scripts": ["background.js"] 
}, 
"permissions": [ 
    "tabs" 
], 
"browser_action": {  
    "default_icon": "icon.png", 
    "default_popup": "popup.html" 
}, 
"content_scripts": [ 
    { 
     "matches": ["http://*/*"], 
     "js": ["jquery.js","contentscript.js"], 
     "run_at": "document_end" 
    } 
] 

} 

Ben gezi-up ne olduğunu ancak belgeiçin ciddi şekilde eksik, zor çözmek için zor. Basit, yeniden kullanılabilir bir örnek, öğrenme sürecinde çok yardımcı olacaktır, çünkü bunun yaygın bir sorun olduğuna eminim.

cevap

16

Pekala, kodunuzdaki bazı şeyleri değiştirmek istediğiniz gibi çalışmasını sağlamalıdır. Yapacağım tüm değişiklikler gerekli değil, ama bunu nasıl yapabilirim.

İçerik Senaryo

var fromDOM = $('h1.name').text(); 
chrome.runtime.sendMessage({method:'setTitle',title:fromDOM}); 

Arkaplan

var title; 
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){ 
    if(message.method == 'setTitle') 
    title = message.title; 
    else if(message.method == 'getTitle') 
    sendResponse(title); 
}); 

Popup.js

chrome.runtime.sendMessage({method:'getTitle'}, function(response){ 
    $('.output').text(response); 
}); 
+0

Bu tam aradığım şeydir. Çok takdir! –

+0

Bunu denedim. pop up hiçbir şey göstermiyor.Web sayfamın başına gelen "OFDOM" değerini değiştirdim ancak –

+0

@ Venky kodunuzla kendi sorunuzu sorun ve size daha iyi yardımcı olacağım. – BeardFist

İlgili konular