繁体   English   中英

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

[英]For chrome extension, how can main.js call function from popup.js?

我已经从网页中提取了用户名和密码(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");
}

清单文件

{
  "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"
    }]
}

主文件

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

弹出窗口.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if (request === 'What I want to recieve') {
        // Do something
        sendResponse('A response');
    } 
});

请参阅按摩传递文档

暂无
暂无

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

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