Chrome扩展程序 - 执行不在新标签中注入的脚本

时间:2017-10-05 15:53:36

标签: javascript google-chrome-extension

我无法让执行脚本工作。我按照这个例子,无法执行任何操作。我已经尝试编辑权限,我尝试注入一个console.log,一个settimeout。我反复阅读文档,我不知所措。我只是尝试注入一个输入用户名和密码的脚本,然后单击登录按钮。

var script =  ' var e = document.getElementById("email"); var p = document.getElementById("password");'+
              'e.value ="'+currentEmployee.email+'"; p.value = "'+currentEmployee.password+'";'+
              'var osbut = document.getElementById("osLoginButton"); osbut.submit();';
chrome.tabs.create({
    url: 'https://www.example.net/login'
}, function (tab) {
    chrome.tabs.executeScript(tab.id, {
        code: script
    });
});

这是我的manifest.json

{
   "name": "Example",
  "description": "example",
  "version": "1.0",
  "manifest_version": 2,

    "browser_action": {
        "default_icon": "icon.png",
    "default_popup": "popup.html",
    "default_script": "popup.js",
        "default_title": "example!"
},
  "permissions": [
    "activeTab",
    "storage", 
    "http://*/",
    "https://*/",
    "tabs",
    "*://www.example.net/*"
  ]
}

1 个答案:

答案 0 :(得分:0)

这就是我最终的工作。我需要的最大的事情是标签需要不活动。这是什么搞砸了一切。新创建的选项卡阻止弹出窗口继续执行这些功能。因此创建了新选项卡,即使有回调,弹出窗口也被破坏,因此无需回调。希望这可以帮助其他人,使用popup.js和新创建的标签。

chrome.tabs.create({
    url: web, active: false
}, function (tab) {myTab = tab; 
  setTimeout(checkLoad, 2000);
  }
)
}//end func

function checkLoad(){ console.log(web);
chrome.tabs.query({status: "complete", url: web }, function (tab) {
  console.log(tab);
  for(var i = 0; i < tab.length; i++){ if(tab[i].title =! undefined){setTimeout(pressure, 1000)}
else{setTimeout(checkLoad, 1000) }

}} )}

function pressure(){

var script = "var e = ''; var p = ''; var j = '';"+
'setTimeout(function(){'+
'var l = document.getElementsByTagName("input");'+
'console.log(l);for(var i = 0; i < l.length; i++){if (l[i].type == "email" || l[i].name == "email" || l[i].id == "email" || l[i].id.includes("user") ){ e = l[i]; console.log(e.name)};if (l[i].type == "password" || l[i].id == "password" ||  l[i].id.includes("pass") || l[i].name.includes("pass")){ p = l[i]; console.log(p.name)};if (l[i].type == "submit"){ j = l[i]; j.click(); }};'+
'e.focus();'+'e.value ="";'+
'e = document.execCommand("insertText", true,'+ '"'+currentEmployee.email+'");'+
'p.value = "";'+
'p.focus();'+
'p = document.execCommand("insertText", true, '+'"'+currentEmployee.password+'");'+
'}, 750);'+
'setTimeout(function(){j.click()}, 1000)';
 chrome.tabs.executeScript(myTab.id, {allFrames : true, code : script})
}