Chrome.extensions:打开标签> 执行脚本> 关闭标签

Bonjour Gurus,我想创建一个像网络爬虫一样的 chrome 扩展。


“标签”按钮应该:打开标签>执行脚本>关闭标签


但目前,选项卡已创建,但“执行脚本”和“删除”被忽略,我错过了什么???


我认为这是一个非常简单的功能,但我对 chrome.extensions 还是很陌生。


希望你会有美好的一天。


问候。神经网络。


[app.js]


var stock;


chrome.runtime.onMessage.addListener(function(request)

{

    stock = request;

    $.get('https://myweb.com/GetData.php?THEDATA=' + stock, function(data)

    {


    });

});


$('#tab').click(function()

{

    chrome.tabs.create({ url: 'https://stackoverflow.com/' },   function(tab)

    {

        chrome.tabs.executeScript(tab.id,

            {

                code : "chrome.runtime.sendMessage(mycommand);"

            }, () => chrome.runtime.lastError);


        chrome.tabs.remove(tab.id);

    });

});

[弹出窗口.html]


<h1>MYtest</h1>

<button id="tab">Open Tab</button>



<script src="js/jquery.js"></script>

<script src="js/app.js"></script>

[manifest.json]


{

    "manifest_version": 2,

    "name": "TestJB",

    "description": "TESTDESC",

    "version": "1.0",

    "permissions":

    [

        "http://*/*",

        "https://*/*",

        "tabs",

        "activeTab",

        "alarms",

        "clipboardRead",

        "clipboardWrite",

        "bookmarks",

        "contextMenus",

        "contentSettings",

        "downloads",

        "history",

        "nativeMessaging",

        "browsingData",

        "proxy",

        "webRequest",

        "webRequestBlocking",

        "cookies",

        "webNavigation",

        "storage",

        "tabCapture",

        "notifications",

        "<all_urls>",

        "unlimitedStorage",

        "debugger"

    ],

    "browser_action":

    {

        "default_icon": "img/icon_128.png",

        "default_popup": "popup.html"

    },

    "icons": 

    {

        "128" : "img/icon_128.png"

    }

}


aluckdog
浏览 162回答 1
1回答

江户川乱折腾

好的,这对我有用。[app.js]var stock;chrome.runtime.onMessage.addListener(function(request){&nbsp; &nbsp; stock = request;&nbsp; &nbsp; $.get('https://myweb.com/GetData.php?THEDATA=' + stock, function(data)&nbsp; &nbsp; {&nbsp; &nbsp; });});$('#tab').click(function(){&nbsp; &nbsp; chrome.tabs.create({ url: 'https://stackoverflow.com/', active: false },&nbsp; &nbsp; function(tab)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; chrome.tabs.executeScript(tab.id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; code : "chrome.runtime.sendMessage(mycommand);"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, () => chrome.runtime.lastError);&nbsp; &nbsp; &nbsp; &nbsp; chrome.tabs.remove(tab.id);&nbsp; &nbsp; });});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript