对于chrome扩展,main.js如何从popup.js调用函数?

时间:2015-06-24 14:41:23

标签: javascript google-chrome-extension

我从网页中提取了用户名和密码(main.js),并尝试将它们存储在indexeddb(popup.js)中。到目前为止,popup.html(popup.js)可以手动存储用户名和密码。我想在有用户名和密码时调用popup.js中的函数。非常感谢你。

main.js中的功能

if (username && password)    
{            
    if(confirm("Store domain with username and password?"))      
    {        
        //want to call function test() from popup.js       
    }
    else
    {
    }    
}

popup.js中的函数

function test()
{
    alert("function called");
}

的manifest.json

{
  "name": "PM Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "a Google Chrome extension",
  "permissions": ["tabs"],

  "browser_action": {  
    "default_icon": "icon.png" ,
    "default_title": "PM Extension",
    "default_popup": "popup.html"
  },

  "content_scripts": [
    {
      "matches": ["http://*/*", 
      "https://*/*"
      ],
      "js": ["jquery.min.js", "main.js"],
      "run_at": "document_start"
    }]
}

1 个答案:

答案 0 :(得分:0)

main.js

select top 5 * from (select * from (values(null),(null),(null),(null),(null)) as Category(nil)) a
left join Brand on 1=1 
LEFT OUTER JOIN
          Product ON Brand.BrandId = Product.BrandId
GROUP BY      Brand.BrandName
ORDER BY      NoSold desc

popup.js

chrome.runtime.sendMessage('Your string/ object/ whatever', function(response) {
    console.log(response.farewell);
});

请参阅Massage Passing documentation