简体   繁体   English

如何从 Chrome 扩展程序中捕获提交操作

[英]How to catch submit action from Chrome extension

I have to write an extension for saving user credentials to my database server (like in LastPass).我必须编写一个扩展来将用户凭据保存到我的数据库服务器(就像在 LastPass 中一样)。 How can I catch submit action from authentication form on active tab to grab entered login and password for saving using my extension and show dialog to a user, if he wants to save credentials or not?如何从活动选项卡上的身份验证表单中捕获提交操作以获取输入的登录名和密码以使用我的扩展程序保存并向用户显示对话框(如果他想保存凭据)?

Add a content script in your manifest and set it to run on some webpage:在清单中添加内容脚本并将其设置为在某些网页上运行:

"content_scripts": [{
    "matches": ["http://*.example.com/*"],
    "js": ["form_submits.js"],
    "run_at": "document_start"
}]

Also add permissions:同时添加权限:

"permissions": [
    "tabs", "http://*.example.com/*"
],

In this content script listen to event "onsubmit" on forms, then contact with your extension using chrome.extension.sendRequest():在此内容脚本中,侦听表单上的“onsubmit”事件,然后使用 chrome.extension.sendRequest() 与您的扩展程序联系:

for (var i = 0; i < document.forms.length; i++) {
    document.forms[i].addEventListener("submit", function(){
        var form = this.form;
        var data = [form.elements["user"], form.elements["password"]];
        // contact with your background page and send the form data.
        chrome.extension.sendRequest({'name':'form_submit', 'data':data}, function(){ /* callback from request */ }, false);
    });
}

In your background script add listener to these calls:在您的后台脚本中,为这些调用添加侦听器:

chrome.runtime.onMessage.addListener(function(request, sender, callback) {
    switch (request.name) {
         case 'form_submit':
             var data = request.data;
             // do something with your form credentials.
             break;
     }
});

****Edited.**** ****已编辑。****

You could also execute JavaScript directly on current active Tab and add form submit listeners:您还可以直接在当前活动选项卡上执行 JavaScript 并添加表单提交侦听器:

First get the current window and active tab:首先获取当前窗口和活动选项卡:

chrome.windows.getCurrent(function callback)
chrome.tabs.getSelected(integer windowId, function callback)

http://code.google.com/chrome/extensions/windows.html#method-getCurrent http://code.google.com/chrome/extensions/windows.html#method-getCurrent

http://code.google.com/chrome/extensions/tabs.html#method-getSelected http://code.google.com/chrome/extensions/tabs.html#method-getSelected

Then execute JavaScript in it:然后在其中执行 JavaScript:

chrome.tabs.executeScript(integer tabId, object details, function callback)

http://code.google.com/chrome/extensions/tabs.html#method-executeScript http://code.google.com/chrome/extensions/tabs.html#method-executeScript

A comprehensive Google Chrome extensions API is here:一个全面的 Google Chrome 扩展 API 在这里:

http://code.google.com/chrome/extensions/getstarted.html http://code.google.com/chrome/extensions/getstarted.html

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

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