chrome.tabs.executeScript仅在开发者控制台处于打开状态时触发 [英] chrome.tabs.executeScript only fires when the Developer Console is open

查看:431
本文介绍了chrome.tabs.executeScript仅在开发者控制台处于打开状态时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为chrome制作一个自动登录按钮扩展程序。我的代码如下:

options.html:

 函数gotoAdmin(){
chrome.tabs.create({'url':http:// www。+ currentTabDomain +/ admin},函数(tabId,changeInfo,tab){
chrome。 tabs.executeScript(null,{file:login.js});
});
}

...

< img src =admin.pngonClick =gotoAdmin()>

login.js:

  $(document).ready(function(){
$('input [name = username]')。val('foo');
$('input [ ('bar');
$('#form')。submit();
});

manifest.json:

 permissions:[
tabs,http:// * / *,https:// * / *
],
content_scripts:[
{
matches:[< all_urls>],
js:[jquery-1.7.2.min.js],
run_at:document_start
}
],
background:{
scripts:[background.js]
}

我的问题是当我通过开发人员控制台打开选项选项卡并启动gotoAdmin()时,jQuery事件进行得很顺利,自动登录工作,但是当我没有打开开发者控制台的情况下点击图片时,它根本不会做任何事情(甚至没有打开警报)。

$ b $我知道,我参加派对的时间太晚了,但是如果有人在寻找类似问题的答案时出现这个问题(比如说,我做到了) - 这里是我使用的解决方案:



首先,事情第一:



问题是只要打开新选项卡,弹出窗口关闭,导致其中的任何JS执行都停止。因此,由于 chrome.tabs.create(< specs>,< callback>)< / code>在弹出窗口中,回调函数永远不会执行。



解决方案:

解决方案是有一个背景页面(或者更好的是 活动页面 )为您处理标签创建过程。然后,您必须通过通知背景/活动页面来启动该过程。



第1步:

在弹出窗口中,使用

  //在< msg>中添加您需要传递的任何参数。 
chrome.runtime.sendMessage(msg);

第2步:

让背景/活动页面接收消息并执行操作:


  // a。听一条消息,添加一个监听器。 
chrome.runtime.onMessage.addListener((msg,sender,sendResponse)=> {
//我得到了消息,让我们解析它
...

// b。依据它,创建标签等
chrome.tabs.create(< specs>,< callback>);
...
}) ;

另请参阅文档以获取有关 消息传递


I'm trying to make an auto-login button extension for chrome. My code is the following:

options.html:

function gotoAdmin(){
    chrome.tabs.create({'url': "http://www."+currentTabDomain+"/admin"}, function(tabId,changeInfo,tab) {
        chrome.tabs.executeScript(null, {file: "login.js"});
    });
}

...

<img src="admin.png" onClick="gotoAdmin()">

login.js:

$(document).ready(function(){
    $('input[name=username]').val('foo');
    $('input[name=password]').val('bar');
    $('#form').submit();
});

manifest.json:

"permissions": [
    "tabs", "http://*/*", "https://*/*"
],
"content_scripts": [
    {
        "matches": ["<all_urls>"],
        "js": ["jquery-1.7.2.min.js"],
        "run_at": "document_start"
    }
],
"background": {
    "scripts": ["background.js"]
}

My problem is that when I open the options tab via the developer console and start gotoAdmin(), the jQuery events go just fine and the auto-login works, but when I click the image without having the developer console open, it will not do anything at all (not even open an alert) from login.js.

解决方案

I know, I am joining the party too late, but -just in case someone lands on this question while looking for an answer to a similar problem (like I did)- here is the solution I used:

First, things first:

The problem is that as soon as the new tab is opened, the popup window closes, which causes any JS executing in it to stop as well. So, since the chrome.tabs.create(<specs>, <callback>) was in the popup window, the callback function is never executed.

The solution:

The solution is to have a background page (or better yet an event page) handle the tab creation process for you. Then, you'll have to initiate the process, by notifying the background/event page.

Step 1:
In the popup, use

// Add whatever arguments you need to pass, in <msg>
chrome.runtime.sendMessage(msg);

Step 2:
Have the background/event page receive the message and act:

// a. Listen for a message, adding a listener.
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
  // I got the message, let's parse it.
    ...

  // b. Act upon it, creating the tab etc.
  chrome.tabs.create(<specs>, <callback>);
  ...
});

Take, also, a look at the docs for more info regarding Message Passing.

这篇关于chrome.tabs.executeScript仅在开发者控制台处于打开状态时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆