2015-04-27 12 views
1

的manifest.jsonpopup.js何時啓動?

{ 
"manifest_version": 2, 

"name": "YouTellMe", 
"description":"FIND AND COMPARE OVER 10.000.000 PRODUCTS AND GET THE BEST PRICES FROM ALL MAJOR INDIAN WEBSHOPS. GET DISCOUNTS TO HAVE THE CHEAPEST PRICE!", 
"version":"0.0", 

"browser_action": 
{ 
    "default_icon":"logoytm.png", 
    "default_popup": "offers.html", 
    "badge" : "YTM" 
}, 

"background" : 
{ 
    "scripts" : ["find_offers.js"], 
    "persistent" : false 
}, 

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'", 

"permissions": 
[ 
    "tabs", 
    "activeTab", 
    "webNavigation", 
    "notifications", 
    "https://ajax.googleapis.com/", 
    "http://localhost/*", 
]} 

find_offers.js

if(! window.jQuery) 
 
{ 
 
\t console.log("importing jquery...") 
 
\t script = document.createElement('script'); 
 
\t script.source = 'http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js'; 
 
\t document.getElementsByTagName("head")[0].appendChild(script) 
 
} 
 

 
chrome.tabs.onUpdated.addListener(tab_activated); 
 
chrome.webNavigation.onCompleted.addListener(load_iframe); 
 

 
var tablink; 
 

 
function tab_activated() 
 
{ 
 
\t chrome.tabs.query({'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT}, 
 
     function(tabs) 
 
     { 
 
\t \t \t tablink = tabs[0].url; 
 
     }); 
 
} 
 

 
function load_iframe() 
 
{ 
 
\t console.log("in load iframe"); 
 
\t ytm_product_url = "http://localhost/bookmarklet/product/"; 
 
    console.log("current URL " + tablink); 
 
    if(tablink != undefined) 
 
    { 
 
    \t var uri = ytm_product_url+"?retailer_url="+tablink; 
 
    } 
 
    else 
 
\t { 
 
    \t var uri = ytm_product_url+"?retailer_url="+document.location.href; 
 
\t } 
 
    chrome.extension.sendMessage({url:uri}); 
 
}

offers.html

<html> 
 
<head> 
 
<title></title> 
 
</head> 
 
<link rel="stylesheet" type="text/x-scss" href="bookmark_offers.css" /> 
 
<script src='jquery.min.js'></script> 
 
<script src='load_offers.js'> </script> 
 
<body> 
 
<div id="YTM_offers"> </div> 
 
</body> 
 
</html>

load_offers.js

chrome.runtime.onMessage.addListener 
 
(
 
\t function (request) 
 
    { 
 
\t \t alert("message received"); 
 
     $.ajax 
 
     (
 
      { 
 
       url : request.url, 
 
      } 
 
     ) 
 
     .done 
 
     (
 
      function(data) 
 
      { 
 
      \t notifyUser(); 
 
       console.log($("#YTM_offers")[0]); 
 
//    document.getElementById('YTM_offers').innerHTML += data; 
 
       $("#YTM_offers").html(data); 
 
       console.log($("#YTM_offers")); 
 
      } 
 
     ) 
 
    } 
 
) 
 

 

 
function notifyUser() 
 
{ 
 
\t console.log("notification...") 
 
\t if (! Notification) 
 
\t { 
 
\t \t alert('Notifications are supported in modern versions of Chrome, Firefox, Opera and Firefox.'); 
 
\t  return; 
 
\t } 
 
\t 
 
\t if(Notification.permission !== "granted") 
 
\t \t Notification.requestPermission(); 
 
\t 
 
\t var notification = new Notification("YouTellMe", 
 
\t \t \t { icon : 'logoytm.png', 
 
\t \t \t \t body : "We've got more offers for you." + 
 
\t \t \t \t \t \t "\nClick on extension Icon for more details." 
 
\t \t \t }); 
 
}

現在的問題發言。 我發送當前頁面的網址從find_offers.jsload_offers.js,然後load_offers.js從服務器中提取相關的報價,這是正在填寫名爲YTM_offers的div。

load_offers.js正在偵聽來自find_offers.js的消息(url),但這裏的發送部分工作正常,但接收部分不工作,直到我檢查popup.html並重新加載當前頁面。

我做錯了什麼? 幫助thank.Thanks

回答

2

你是錯誤的假設彈出監聽消息,而它關閉。

事實上,當彈出窗口關閉時,包含load_offers.js的HTML文檔會被完全卸載,並且每次都會從頭開始重新加載。

因此,向彈出窗口發送消息通常是一個糟糕的主意,除非首先詢問彈出窗口。

你需要修改你的邏輯;要麼只是在彈出窗口打開時查詢信息,要麼從存儲信息中存儲信息,以便彈出窗口可以在打開時請求它。後者可能很難,因爲你宣佈你的背景爲persistent: false(然而,chrome.storage是一個選項)。

從查看當前的邏輯,當彈出窗口打開時,當前頁面不可能發生變化;因此,當您打開彈出窗口時,您應該只查詢有關當前選項卡的信息,而您並不需要當前窗體中的背景腳本。

+0

感謝您的回覆。是的,你正在寫,我知道在閱讀問題標題後。所以現在我想從後臺js中的服務器中獲取數據,並通知用戶數據已準備就緒,點擊數據圖標,因爲popup不能實際打開。 –

+0

所以。做吧!提取數據,如果有用戶的有趣信息 - 使用'browserAction.setBadgeText'或更改圖標以表明您有信息要顯示,並將該信息存儲在'chrome.storage'中。在彈出窗口中,打開時從'chrome.storage'中取出數據。 – Xan

+0

感謝您的想法(chrome.storage) –