Answer the question
In order to leave comments, you need to log in
Not displaying alert(js) in chrome extension?
Good afternoon. I am creating a chrome extension. It is necessary that when you click on the "output" button. An alert was displayed with the text that was selected. But when clicked, the extension window is hidden. What could be the problem?
<html>
<title>Пример#1: получаем выделенный фрагмент страницы с помощью JS</title>
<head>
</head>
<body>
<span id="text">
world face qwerty
</span>
<button id="321">ZAMENA</button>
<button id="123">ВЫВОД</button>
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
function swapSelection(swapText) {
var sel = window.getSelection ? window.getSelection() : document.selection.createRange();
if (sel != "") {
if (sel.getRangeAt) {
var range = sel.getRangeAt(0);
var newNode = document.createElement("span");
newNode.setAttribute('class', 'swapclass');
range.surroundContents(newNode);
} else {
sel.pasteHTML('<span class="swapclass">' + sel.htmlText + '</span>');
}
$('.swapclass').replaceWith(swapText);
}
}
$(document.getElementById('321')).click(function () {
swapSelection("night");
});
document.getElementById('123').addEventListener('click',function(){getSelectionFragment()});
function getSelectionFragment(){
var ie = false;
if ( window.getSelection ) {
var selectedText = window.getSelection();
} else if ( document.getSelection ) {
var selectedText = document.getSelection();
} else if ( document.selection ) {
ie = true;
var selectedText = document.selection.createRange();
}
if(!ie){
alert(selectedText)// просто выведет выделенный текст без тегов
var theParent = selectedText.getRangeAt(0).cloneContents();
// собственно выделенный кусок кода
}else{
alert(selectedText.text) // текст
alert(selectedText.htmlText) // выделенный кусок кода
}
}
function toHTML(docFragment){
var d = document.createElement('div');
d.appendChild(docFragment);
return d.innerHTML;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question