Javascript chrome扩展:创建新选项卡并将焦点转移到页面

Javascript chrome扩展:创建新选项卡并将焦点转移到页面,javascript,google-chrome-extension,focus,Javascript,Google Chrome Extension,Focus,尝试创建一个扩展,创建新选项卡并将其导航到某个站点,并使一些成为焦点 我检查了这个问题,但无法解决我的问题: 下面是一个示例代码,我正试图打开一个新的选项卡,导航到谷歌,并试图将重点放在谷歌搜索栏上 这是我的content\u script.js尝试点击指定的网页和指定的,并向我的background.js发送消息 $("#kybs_eklenti_edin_btn").hide(); $(document).ready(function (e) { var host = $(location

尝试创建一个扩展,创建新选项卡并将其导航到某个站点,并使一些
成为焦点

我检查了这个问题,但无法解决我的问题:

下面是一个示例代码,我正试图打开一个新的选项卡,导航到谷歌,并试图将重点放在谷歌搜索栏上

这是我的
content\u script.js
尝试点击指定的网页和指定的
,并向我的
background.js发送消息

$("#kybs_eklenti_edin_btn").hide();
$(document).ready(function (e) {
var host = $(location).attr('hostname');
var url = $(location).attr('pathname');
var search = $(location).attr('search');
console.log(url,"|",host,"|",search);
if (host.indexOf("kybs.com.tr")!=-1){
    if (url == "/yoneticiv2/kursiyerlistesi"){
        $("#tn_logo_holder").click(function(){
            chrome.runtime.sendMessage({mesaj: "init_program"}, function(response) {
            console.log(response);
            });             
        }); 
    }
}
});
var openedTab = 0;
var isTabOpen = false;
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.mesaj == "init_program"){
        if (!isTabOpen){
            chrome.tabs.create({selected: true},function(tab){
            isTabOpen= true;
            openedTab = tab.id;
            tabCreated();
            });         
        }else {
            chrome.tabs.get(openedTab, function(tab) { 
                if (chrome.runtime.lastError) {
                    chrome.tabs.create({selected: true},function(tab){
                        isTabOpen= true;
                        openedTab = tab.id;
                        tabCreated();
                    });         
                }else {
                    tabCreated();
                }
            });

        }       
        sendResponse("check");
    }

});
function tabCreated(){
    chrome.tabs.update(openedTab,{url:"https://google.com.tr"});

    chrome.tabs.executeScript(openedTab, {file: "jquery.js"},function(tab){
            if (chrome.runtime.lastError) {
                console.log(chrome.runtime.lastError.message);
                chrome.tabs.executeScript(openedTab, {file: "jquery.js"},function(tab){
                chrome.tabs.executeScript(openedTab, {file: "inject.js"});
                });
            }else {
                chrome.tabs.executeScript(openedTab, {file: "inject.js"});
            }
        });

}
$(document).ready(function(){
    console.log("buradayız");
    var kursNoInput = document.getElementById("lst-ib");
    kursNoInput.value="840337";
    kursNoInput.focus();        
});
manifest.json

{
    "manifest_version": 2,
    "name": "E-Yaygın Kursiyer Aktarımı",  
    "description": "E-Yaygın Kursiyer Aktarımını Kolaylaştıran Yazılım",  
    "version": "1.0",    
    "permissions": ["tabs", "<all_urls>"],  
    "background": {
    "scripts": ["background.js"]
  },
    "content_scripts": [{
        "matches": [
            "http://*/*",
            "https://*/*"
        ],
        "js": ["jquery.js","content_script.js"]
    }],

    "icons": {
        "16": "images/icons/16.png",
        "19": "images/icons/19.png",
        "38": "images/icons/38.png",
        "64": "images/icons/64.png",
        "128": "images/icons/128.png"
    }
}
background.js

$("#kybs_eklenti_edin_btn").hide();
$(document).ready(function (e) {
var host = $(location).attr('hostname');
var url = $(location).attr('pathname');
var search = $(location).attr('search');
console.log(url,"|",host,"|",search);
if (host.indexOf("kybs.com.tr")!=-1){
    if (url == "/yoneticiv2/kursiyerlistesi"){
        $("#tn_logo_holder").click(function(){
            chrome.runtime.sendMessage({mesaj: "init_program"}, function(response) {
            console.log(response);
            });             
        }); 
    }
}
});
var openedTab = 0;
var isTabOpen = false;
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.mesaj == "init_program"){
        if (!isTabOpen){
            chrome.tabs.create({selected: true},function(tab){
            isTabOpen= true;
            openedTab = tab.id;
            tabCreated();
            });         
        }else {
            chrome.tabs.get(openedTab, function(tab) { 
                if (chrome.runtime.lastError) {
                    chrome.tabs.create({selected: true},function(tab){
                        isTabOpen= true;
                        openedTab = tab.id;
                        tabCreated();
                    });         
                }else {
                    tabCreated();
                }
            });

        }       
        sendResponse("check");
    }

});
function tabCreated(){
    chrome.tabs.update(openedTab,{url:"https://google.com.tr"});

    chrome.tabs.executeScript(openedTab, {file: "jquery.js"},function(tab){
            if (chrome.runtime.lastError) {
                console.log(chrome.runtime.lastError.message);
                chrome.tabs.executeScript(openedTab, {file: "jquery.js"},function(tab){
                chrome.tabs.executeScript(openedTab, {file: "inject.js"});
                });
            }else {
                chrome.tabs.executeScript(openedTab, {file: "inject.js"});
            }
        });

}
$(document).ready(function(){
    console.log("buradayız");
    var kursNoInput = document.getElementById("lst-ib");
    kursNoInput.value="840337";
    kursNoInput.focus();        
});
inject.js

$("#kybs_eklenti_edin_btn").hide();
$(document).ready(function (e) {
var host = $(location).attr('hostname');
var url = $(location).attr('pathname');
var search = $(location).attr('search');
console.log(url,"|",host,"|",search);
if (host.indexOf("kybs.com.tr")!=-1){
    if (url == "/yoneticiv2/kursiyerlistesi"){
        $("#tn_logo_holder").click(function(){
            chrome.runtime.sendMessage({mesaj: "init_program"}, function(response) {
            console.log(response);
            });             
        }); 
    }
}
});
var openedTab = 0;
var isTabOpen = false;
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.mesaj == "init_program"){
        if (!isTabOpen){
            chrome.tabs.create({selected: true},function(tab){
            isTabOpen= true;
            openedTab = tab.id;
            tabCreated();
            });         
        }else {
            chrome.tabs.get(openedTab, function(tab) { 
                if (chrome.runtime.lastError) {
                    chrome.tabs.create({selected: true},function(tab){
                        isTabOpen= true;
                        openedTab = tab.id;
                        tabCreated();
                    });         
                }else {
                    tabCreated();
                }
            });

        }       
        sendResponse("check");
    }

});
function tabCreated(){
    chrome.tabs.update(openedTab,{url:"https://google.com.tr"});

    chrome.tabs.executeScript(openedTab, {file: "jquery.js"},function(tab){
            if (chrome.runtime.lastError) {
                console.log(chrome.runtime.lastError.message);
                chrome.tabs.executeScript(openedTab, {file: "jquery.js"},function(tab){
                chrome.tabs.executeScript(openedTab, {file: "inject.js"});
                });
            }else {
                chrome.tabs.executeScript(openedTab, {file: "inject.js"});
            }
        });

}
$(document).ready(function(){
    console.log("buradayız");
    var kursNoInput = document.getElementById("lst-ib");
    kursNoInput.value="840337";
    kursNoInput.focus();        
});
还包括一个JQuery库

我试图用JQuery注入javascript,但它没有成功。一切正常,但无法将焦点从地址栏转移到网页

第二件事是,当页面试图加载
focus()
时,如果我单击新创建的页面,效果会非常好


我试图在谷歌资源上搜索,但找不到任何解决方案,也不知道有没有办法将焦点转移到页面。

我想出了我的解决方案。想法很简单,当您尝试创建一个新的
选项卡时,默认情况下
所选的
属性为
true
。当您创建新的
选项卡时,它会显示出来,并且地址栏会自动聚焦。这样,您就无法将焦点从
窃取到
页面

要防止聚焦地址栏,请创建新的
选项卡
,并选择属性
:false,如下所示

chrome.tabs.create({selected: false},function(tab){
                        isTabOpen= true;
                        openedTab = tab.id;
                        tabCreated();
                    });   
创建选项卡后,您可以更新其
链接
所选属性
,然后可以使用注入的javascript从地址栏获取焦点

function tabCreated(){
    chrome.tabs.update(openedTab,{url:"https://google.com.tr"});
    chrome.tabs.executeScript(openedTab, {file: "jquery.js"},function(tab){
            if (chrome.runtime.lastError) {
                console.log(chrome.runtime.lastError.message);
                chrome.tabs.executeScript(openedTab, {file: "jquery.js"},function(tab){
                chrome.tabs.executeScript(openedTab, {file: "inject.js"});
                });
            }else {
                chrome.tabs.executeScript(openedTab, {file: "inject.js"});
            }
        });
chrome.tabs.update(openedTab,{selected:true});

}

比您的
focus()
函数工作起来没有问题。

请回答主题中的问题:包括一个与问题重复的完整问题。包括manifest.json、一些后台/内容/弹出脚本/HTML。寻求调试帮助的问题(“为什么此代码不工作?”)必须包括:►想要的行为,►特定的问题或错误,以及►在问题本身中复制它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:“如何创建”,以及。在加载和执行扩展时,中到底显示了什么?@wOxxOm那么我们可以做些什么。有什么建议吗?您链接的答案显示了一些解决方法,请尝试使用它们。我的观点是,您不能直接从地址栏中窃取焦点。@wOxxOm我找到了解决此问题的方法。我可以回答我的问题吗?这只适用于您的扩展以编程方式创建新选项卡的情况,但当用户单击“新建选项卡”按钮创建新选项卡时,您不能窃取焦点。@ospider您可以收听tabs.onCreate并关闭它,然后以编程方式打开它。@KamranEyyubov是的,但这似乎有点尴尬