Chrome扩展无法访问用户所做的选择

时间:2019-07-12 09:55:01

标签: javascript google-chrome-extension

我正在尝试获得用户在网站上所做的选择。我使用了this window.getSelection()函数,它返回用户突出显示(选择)的部分。使用chrome扩展程序时,我认为弹出窗口是它自己的窗口,并且弄清楚了如何访问主窗口而不是弹出窗口。 (在代码中看到)我遇到的问题是,一旦我访问活动标签,功能window.getSelection()就无法使用。

chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
    //console.log(tabs[0])
    let tabIndex = tabs[0].index
    currentURL = tabs[0].url

    chrome.windows.getCurrent({populate: true, windowTypes:["normal"]}, (window) => {
      console.log(window.getSelection())
      console.log(window.tabs[tabIndex])
      console.log(window instanceof Window)
      highlightSelection = window.getSelection();
      if(highlightSelection === undefined) {
        alert("You have not highlighted anything!");
      }
    })
  })

我不断获得变量undefined的{​​{1}}。该函数应该将窗口对象作为参数放置在回调中,但事实并非如此。您可以找到getCurrent函数文档here。这是关于无法访问getSelection()的错误: highlightSelection

0 个答案:

没有答案
相关问题