简体   繁体   English

Google Chrome扩展程序+ chrome.windows.create + window.getSelection()。toString()

[英]Google Chrome Extension + chrome.windows.create + window.getSelection().toString()

I'm working on a chrome extension which copies the selected/highlighted text into a textarea. 我正在开发一个chrome扩展程序,它将所选/突出显示的文本复制到textarea中。 This is what I used so far: 这是我到目前为止使用的:

chrome.tabs.executeScript( {
    code: "window.getSelection().toString();",
}, function(selection) {
    document.getElementById("output").value = selection[0];
});

But now I've switched from the popup.html to a window which I created like this 但是现在我已经从popup.html切换到我这样创建的窗口

background.js : background.js

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.windows.create({
        url: chrome.runtime.getURL("window.html"),
        type: "panel", height: 590, width:850, focused: false
        }, function(win) {
    });
});

And I can't get the selected text into this window anymore. 而且我无法将所选文本输入此窗口。 I also copied the current URL of the activetab doing like so: 我还复制了activetab的当前URL,如下所示:

chrome.tabs.getSelected(windowId, function(tab) {
    document.getElementById('url').innerHTML = tab.url;
    var windowId = tab.id
});

and I could make this work for the new window using: 我可以使用以下方法在新窗口中进行这项工作:

chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
    document.getElementById('url').innerHTML = tabs[0].url;
});

So my question is: How can I get the selected/highlighted text into the textarea inside my newly created window? 所以我的问题是: 如何将选定/突出显示的文本放入新创建的窗口内的文本区域? Is there something similiar to the 是否有类似的东西

chrome.tabs.query() 

just for highlighted text? 仅用于突出显示的文本?

Use executeScript's tabId parameter to inject in the clicked tab, then pass the text using any of these: 使用executeScript的tabId参数注入单击的选项卡,然后使用以下任意一项传递文本:

  • messaging 讯息传递
  • chrome.storage.local chrome.storage.local
  • encodeURIComponent in the URL: 'window.html?url=' + encodeURIComponent(text) URL中的encodeURIComponent'window.html?url=' + encodeURIComponent(text)
    then in window.js use decodeURIComponent. 然后在window.js中使用解码URIComponent。
  • localStorage, the classic synchronous storage shared between all internal extension pages, in case you need the result appear on the first page render without delay/flicker and don't want to pass the text in URL. localStorage,所有内部扩展页面之间共享的经典同步存储,以防万一您需要结果显示在首页上而没有延迟/闪烁且不想在URL中传递文本。

Here's an example for localStorage. 这是localStorage的示例。

  • background.js: background.js:

     chrome.browserAction.onClicked.addListener(function(tab) { getSelectedText(tab.id, function(text) { localStorage.selectedText = text; chrome.windows.create({ url: "/window.html", type: "panel", height: 590, width:850, focused: false }); }); }); function getSelectedText(tabId, cb) { chrome.tabs.executeScript(tabId, { code: "window.getSelection().toString();", }, function(selection) { cb(selection[0]); }); } 
  • window.html: window.html:

      ................. <script src="window.js"></script> </body> </html> 
  • window.js: window.js:

     document.getElementById('url').textContent = localStorage.selectedText || ''; delete localStorage.selectedText; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM