简体   繁体   English

popup.js何时启动?

[英]When does popup.js gets initiated?

manifest.json 的manifest.json

{
"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 find_offers.js

 if( ! window.jQuery ) { console.log("importing jquery...") script = document.createElement('script'); script.source = 'http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js'; document.getElementsByTagName("head")[0].appendChild(script) } chrome.tabs.onUpdated.addListener(tab_activated); chrome.webNavigation.onCompleted.addListener(load_iframe); var tablink; function tab_activated() { chrome.tabs.query({'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT}, function(tabs) { tablink = tabs[0].url; }); } function load_iframe() { console.log("in load iframe"); ytm_product_url = "http://localhost/bookmarklet/product/"; console.log("current URL " + tablink); if(tablink != undefined) { var uri = ytm_product_url+"?retailer_url="+tablink; } else { var uri = ytm_product_url+"?retailer_url="+document.location.href; } chrome.extension.sendMessage({url:uri}); } 

offers.html 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 load_offers.js

 chrome.runtime.onMessage.addListener ( function (request) { alert("message received"); $.ajax ( { url : request.url, } ) .done ( function(data) { notifyUser(); console.log( $("#YTM_offers")[0] ); // document.getElementById('YTM_offers').innerHTML += data; $("#YTM_offers").html(data); console.log($("#YTM_offers")); } ) } ) function notifyUser() { console.log("notification...") if (! Notification) { alert('Notifications are supported in modern versions of Chrome, Firefox, Opera and Firefox.'); return; } if(Notification.permission !== "granted") Notification.requestPermission(); var notification = new Notification("YouTellMe", { icon : 'logoytm.png', body : "We've got more offers for you." + "\\nClick on extension Icon for more details." }); } 

Now the problem statement. 现在问题陈述。 I send current page's url from find_offers.js to load_offers.js and then load_offers.js pulls the related offers from server, that is being populated in div called YTM_offers . 我将当前页面的url从find_offers.js发送到load_offers.js ,然后load_offers.js从服务器提取相关报价,该报价已填充在名为YTM_offers div中。

load_offers.js is listening for message(url) from find_offers.js but here sending part works perfectly, but receiving part does not work until I inspect popup.html and reload current page. load_offers.js正在侦听来自find_offers.js的message(url),但是在这里发送部分工作正常,但是在我检查popup.html并重新加载当前页面之前,接收部分不起作用。

What I am doing wrong? 我做错了什么? Help appreciated.Thanks 感谢帮助。谢谢

You are wrong in assuming that the popup listens for messages while it's closed. 您以为弹出窗口关闭时会监听消息是错误的。

In fact, the HTML document that contains load_offers.js is completely unloaded when the popup is closed, and is loaded again from scratch every time. 实际上,当关闭弹出窗口时,包含load_offers.js的HTML文档会完全卸载,并且每次都会从头开始重新加载。

Therefore, sending a message to a popup is generally a bad idea, unless the popup asked first. 因此,将消息发送到弹出窗口通常是个坏主意,除非弹出窗口首先被询问。

You need to modify your logic; 您需要修改逻辑; either only query information when the popup opens, from within it, or store the information so that the popup can request it when opened. 要么仅在弹出窗口打开时从其中查询信息,要么存储该信息,以便弹出窗口在打开时可以请求信息。 The latter can be difficult since you declared your background as persistent: false ( chrome.storage is an option, however). 后者可能很困难,因为您将背景声明为persistent: false (但是chrome.storage是一个选项)。

From looking at your current logic, it seems unlikely that the current page changes while the popup is open; 从您当前的逻辑来看,打开弹出窗口时当前页面似乎不太可能改变。 therefore, you should just query information about the current tab when you open the popup, and you don't really need the background script in its current form. 因此,您应该在打开弹出窗口时查询有关当前选项卡的信息,并且您实际上并不需要当前形式的背景脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM