ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript-将控制权从弹出窗口转移到内容脚本-Google Chrome扩展程序

2019-12-08 18:37:48  阅读:183  来源: 互联网

标签:google-chrome google-chrome-extension javascript


打印屏幕以了解我的问题:link

我正在构建一个扩展程序,该扩展程序将一个按钮插入到特定网页的DOM中.该插入是通过insert.js中的injectScript()函数实现的,该函数已正确执行并正确输入了清单文件的“ content_scripts”:字段中.

injectScript()注入(当然,在当前正在加载的网页的DOM中,如果它是我要查找的页面)包含相同inject.js(包含按钮的onclick事件)的脚本标签标记,包含按钮的CSS,最重要的是,一个div,其中包含通过调用chrome.extension.sendRequest({greeting:“ dami”},function(response){…})从background.html获得的文本.通过单击我插入的按钮,该div中插入的文本可用于从网页填充其他div.

这很完美,但是我还需要解决另一个问题:

仅当chrome.tabs.onSelectionChanged,chrome.tabs.onUpdated和chrome.history.onVisited事件发生时,才会注入我注入到网页DOM中的特定文本.我还想在我的popup.html通过更改localStorage [“ builtin”]对其进行修改时重新注入该文本.所以我想要的是:当我从popup.html页面单击OK按钮时,我想使用chrome.extension.sendRequest({greeting:“ reintrodu”},function(response){…});从popup.html发送请求到管理我的第一次文本注入的相同内容脚本,但似乎不起作用.

当我单击按钮时什么也没有发生!测试警报框不显示任何内容.当我按OK按钮时,popup.html只是关闭,似乎没有发生数据交换.

这是由我的popup.html中的OK按钮触发的功能:

function closer() 
{
    if ($('input[name=check]').is(':checked'))
    {
        localStorage["builtin"] = document.getElementById("tkey_1").value;
        localStorage["build"] = "true";
    }
    else 
    {
        localStorage["builtin"] = document.getElementById("tkey_1").value;
        localStorage["build"] = "false";            
    }

    chrome.extension.sendRequest({greeting: "reintrodu"}, function(response)
    {
        alert(response.farewell);
    });         

    window.close();
}

chrome.extension.sendRequest({greeting:“ reintrodu”},function(response){…})应将请求发送到内容脚本inject.js:

chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
//      console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension");
if (request.greeting === "history")
 {
    console.log("registered in history");
    sendResponse({farewell: "goodbye from history"});
    PageShowHandler();
 }   
else if (request.greeting === "historyrem")
 {
    console.log("registered in historyrem");
    sendResponse({farewell: "goodbye from historyrem"});
    PageShowHandler();
 }       
else if (request.greeting === "exploit")
 { 
    console.log("registered in exploit");
    sendResponse({farewell: "goodbye from exploit"});
    PageShowHandler();
 }
else if (request.greeting === "reintrodu") 
 { 
    console.log("registered in reintrodu");
    sendResponse({farewell: "goodbye from reintrodu"});
    //PageShowHandler();
    alert('prins reintrodu');
 }   
else if (request.greeting === "selected")
 {
    console.log("registered in selected");
    sendResponse({farewell: "goodbye from selected"});
    PageShowHandler();
 }

else
    sendResponse({}); // snub them.
});

这是background.html,似乎对初始注入有效,与popup.html不同:

<html>
<head>
<script type="text/javascript">

//Fired when a URL is visited, providing the HistoryItem data for that URL.
chrome.tabs.onSelectionChanged.addListener(function(tabId, selectInfo)
{
    chrome.tabs.sendRequest(tabId, {greeting: "selected"}, function(response) {
        console.log(response.farewell);
    });
});

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) 
{
    chrome.tabs.sendRequest(tab.id, {greeting: "exploit"}, function(response) {
            console.log(response.farewell);
    });
});

chrome.history.onVisited.addListener(function(result)
{
    //alert(result.url);
    //chrome.tabs.sendRequest(null, {greeting: "history"}, function(response) {
    //      console.log(response.farewell);
    //});
    chrome.windows.getCurrent(function(w) 
    {
        chrome.tabs.getSelected(w.id, function (tabul)
        {
            //alert(tabul.id);
            chrome.tabs.sendRequest(tabul.id, {greeting: "history"}, function(response) {
                    console.log(response.farewell);
            });
        });
    });     
});


chrome.history.onVisitRemoved.addListener(function(removed)
{
    //alert(result.url);
    //chrome.tabs.sendRequest(null, {greeting: "historyrem function(response) {
    //      console.log(response.farewell);
    //});
    chrome.windows.getCurrent(function(w) 
    {
        chrome.tabs.getSelected(w.id, function (tabul)
        {
            //alert(tabul.id);
            chrome.tabs.sendRequest(tabul.id, {greeting: "historyrem"}, function(response) {
                    console.log(response.farewell);
            });
        });
    });     
});

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) 
{
    if(request.greeting == "dami")
    {
        if(localStorage["build"] == "true")
        {   
            sendResponse({farewell: localStorage["builtin"]});
        }
        else
        {
            sendResponse({farewell: "textul default"});
        }
    }

    else 
    {
        sendResponse({farewell: "n-am ba; du-te dracu!"});
    }
});


</script>
</head>
<body></body>
</html>

这是清单:

{
"name" : "gMail Adder ",
"version" : "1.0",
"description" : "Google Chrome Gmail Adder",
"options_page": "options.html",
"background_page": "background.html",
"run_at": "document_start",
"permissions": [
   "tabs",
   "history",
   "http://*/*",
   "https://*/*"
],
"content_scripts": [
  {
   "matches": ["*://*.google.mail.com/*", "https://*.google.mail.com/*"     ,"http://mail.google.com/*" ,"https://mail.google.com/*", "https://www.google.com/*", "http://www.google.com/*", "file:///*"],
   "css": ["toggle.css"],
   "js": ["jquery-1.4.4.min.js", "inject.js"]
  }
],
"browser_action" : {
"default_icon" : "Quest Icon 11.png",
"default_popup": "popup.html"
}
}

解决方法:

由于API调用是异步的,因此您将在关闭接收请求前的弹出窗口.

另外,如果您要将请求发送到内容脚本,则需要改用chrome.tabs.sendRequest.

因此,您的弹出窗口应如下所示:

function closer() 
{
    ...

    //todo: get tabid of a tab where the content script you are interested in is

    chrome.tabs.sendRequest(tabId, {greeting: "reintrodu"}, function(response)
    {
        alert(response.farewell);

        //closing only after receiving response
        window.close();
    });         


}

标签:google-chrome,google-chrome-extension,javascript
来源: https://codeday.me/bug/20191208/2093325.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有