chrome.tabs.executeScript里面的chrome.tabs.create

时间:2018-02-17 11:56:41

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

我的Chrome扩展程序最近工作很好,我认为它可能与新的Chrome扩展政策或其他相关。 我有一个在网站上运行的脚本,在新选项卡中打开一些特定链接,并应在这些新创建的选项卡中运行脚本。 它现在已经完美无缺,现在却没有。

清单:

  {
  "name": "Extension ",
  "version": "1.6.2",
  "manifest_version": 2,
  "options_page": "options.html",
  "description": "Extension ",
  "permissions": [
    "<all_urls>", "tabs", "activeTab", "storage"
  ],
  "background": {
  "matches": [ "*://*.website.com/*" ],
  "scripts" : [ "thirdParty/jquery.js", "event.js" ],
  "persistent": false
  },
    "browser_action": {
      "default_icon": "icon.png"
  },
  "content_scripts": [
    {
      "matches": [ "*://*.ebay.com/usr/*" ],
      "css": [ "ebayButton.css" ],
      "js": [ "ebayButton.js" ],
      "run_at": "document_end"
    }
  ]
}

该脚本首先单击“:// .website.com / *”中的扩展图标,后台脚本为“event.js”:

chrome.browserAction.onClicked.addListener(firstFunction);

scanOnLoad = true;

// This gets me the required URLS inside "://.website.com/*" - works
function firstFunction(tab) {
    chrome.tabs.executeScript(tab.id, {file: 'firstFunction.js'}, results => {
        results[0].forEach(secondFunction);
    });
}

// This opens a new tab for each returned URL - works
function secondFunction(url) {
    chrome.tabs.create({url, pinned: true, active: false}, thirdFunction);
}

// This used to work, but now it doesn't. This function should execute 'thirdFunction.js'
// inside every opened tab. for some reason - now it doesn't work.
function thirdFunction(tab) {
    chrome.tabs.executeScript(tab.id,{file: 'thirdFunction.js'}, results => {
        chrome.tabs.remove(tab.id);
        console.log(results[0]);
    });
}

'thirdFunction.js'确实有效并在尝试在网站上运行脚本时返回一个列表:

[...document.getElementsByClassName("mem_loc")]
    .filter(a => !(a.textContent.trim().endsWith('China') || a.textContent.trim().endsWith('Korea')))
    .map(a=> document.getElementsByClassName("analyze")[0].href.replace('search=true&',''))

它会创建选项卡,它会关闭它们,但它不会像以前那样从'thirdFunction.js'获得任何结果。创建的选项卡中没有错误,但后台脚本中有两个错误('event.js') 为每个创建的选项卡。第一个错误:

Error in response to tabs.executeScript: TypeError: Cannot read property '0' of undefined
    at Object.chrome.tabs.executeScript.results [as callback] (chrome-extension://biknnfhbimcbgaefmocdaeaioabnbpkm/event.js:43:28)
    at Object.thirdFunction [as callback] (chrome-extension://biknnfhbimcbgaefmocdaeaioabnbpkm/event.js:41:17)

第二次错误:

Unchecked runtime.lastError while running tabs.executeScript: The tab was closed.
    at Object.thirdFunction [as callback] (chrome-extension://biknnfhbimcbgaefmocdaeaioabnbpkm/event.js:41:17)

我认为我已经对所有事情都做了许可,因为正如我所说的那样,它现在一直在工作。我不知道它为什么停止工作。试图在网上搜索答案,没有答案解决了我的问题。任何帮助都是appriciated:)

0 个答案:

没有答案