I am developing a Google Chrome extension. When the popup window was clicked, I would like the input box to be present in the popup.html file to contain the selected text of the current web page.
Example text field:
<input id="searchBox" type="text" />
When text is selected on a web page, the text box should contain the selected word. I tried with chrome.extension.getBackgroundPage().getSelection(), but it does not work.
chrome.extension.getBackgroundPage().getSelection()
There is a stream in google groups: how to get HTML code from a selection?
var selection = window.getSelection(); var range = selection.getRangeAt(0); if (range) { var div = document.createElement('div'); div.appendChild(range.cloneContents()); alert(div.innerHTML); }
You can use this in the console or in the plugin anyway.
, div :
var text = ''; if(window.getSelection){ text = window.getSelection(); }else if(document.getSelection){ text = document.getSelection(); }else if(document.selection){ text = document.selection.createRange().text; } text=text.toString();
, , - Chrome.
chrome.extension.getBackgroundPage().getSelection() , "BackgroundPage" -. - :
chrome.tabs.executeScript(null, {code:"alert(window.getSelection().toString());"}) //alerts the selected text
. , .
Source: https://habr.com/ru/post/1750784/More articles:Games for iPhone - c ++How to apply style for WPF DATAGRID Scroll Bars - wpfdatagridVilla Perl CGI Cloning Request Headers for LWP UserAgent - perlOffer for richtextbox online HTML code generator on web page - javascriptHow to change date format from DD / MM / YYYY to YYYY-MM-DD? - dateВ чем разница между указателем на функцию и указателем на функцию WINAPI? - c++is a decorator in python exactly the same as calling a function on a function? - pythonHow to disable a Menu control from a style in javascript? - asp.netquick search for a small image in a larger image - searchAxAcroPDF swallows the keys, how to make it stop? - c #All Articles