Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对于chrome扩展,main.js如何从popup.js调用函数?_Javascript_Google Chrome Extension - Fatal编程技术网

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

Javascript 对于chrome扩展,main.js如何从popup.js调用函数?,javascript,google-chrome-extension,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?"))

我已经从网页中提取了用户名和密码(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"
    }]
}
main.js popup.js
请参阅。

您可以像在同一文件中的何处一样调用它,只要它包含在页面中(前提是没有其他名为test()的函数,在这种情况下,您应该查看名称空间),在此处命名您的文件是不够的。请添加您的清单,以便我们了解您如何使用
main.js
。@NING注释不适合这样做。改为编辑您的问题。通过添加到相关文档的链接,您的代码转储式答案可以得到很大改进。
chrome.runtime.sendMessage('Your string/ object/ whatever', function(response) {
    console.log(response.farewell);
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if (request === 'What I want to recieve') {
        // Do something
        sendResponse('A response');
    } 
});