Chrome扩展程序。使用jquery重定向后获取元素

时间:2018-05-25 16:41:29

标签: javascript jquery google-chrome-extension

的manifest.json:

{
"manifest_version": 2,

"name": "Name",
"description": "Description",
"version": "1.0",

"browser_action": {
    "name": "Manipulate DOM",
    "icons": ["icon.png"],
    "default_icon": "icon.png",
    "default_popup": "index.html"
},
"content_scripts": [ {
    "js": [ "jquery.min.js", "custom.js" ],
    "matches": [ "http://*/*", "https://*/*"]
  }],
"permissions": [
    "activeTab", "tabs", "<all_urls>", "webRequest "
]}

custom.js:

function hello() {
chrome.tabs.executeScript({
    file: 'alert.js'
}); 
}
document.getElementById('clickme').addEventListener('click', hello);

alert.js:

$('input[name="firstname"]').val('Alex');
$('input[name="lastname"]').val('Jefferson');
$('button[name="submit"]').trigger('click');

触发后(“点击”),重定向到另一个第二种形式的页面。 我怎样才能填写第二张表格? “$( '#ELEM')VAL();”。在新页面中不起作用。

UPD:我的background.js代码:

function secondStep() { 
    chrome.tabs.executeScript(null, {file: "secondStep.js"}); 
    chrome.webNavigation.onCompleted.removeListener(boundX); 
} 

chrome.browserAction.onClicked.addListener(function(tab) { 
    chrome.tabs.executeScript(null, {file: "firstStep.js"}, function() { 
        var boundX = secondStep.bind(); 
        chrome.webNavigation.onCommitted.addListener(boundX);   
    }); 
});

0 个答案:

没有答案