My Chrome extension copy text from the current page and show that inside a box on the left side of the current page, which will remain fixed even if I scroll the current page. The code is:
content.js
string=[];i=0;
function doc_keyUp(e) {
if (getSelectionText() != "") {
if (e.key === '1') {
string[i]=string+getSelectionText();
view = window.open("","Viewer", "width=400,height=600"); //"_parent",
edit = document.createElement("div");
edit.textContent = string;
view.document.body.appendChild(edit);
}
}
} // doc_keyUp(e)
console.log('The program has started!!!');
// register the handler
document.addEventListener('keyup', doc_keyUp, false);
function getSelectionText() {
var text = "";
text = window.getSelection().toString();
if (window.getSelection) {
text = window.getSelection().toString();
}
else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
manifest.json
{
"name": "",
"description": "",
"version": "1.0",
"manifest_version": 3,
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
"permissions": ["tabs"]
}
Now I have a window which pops up, but I want to have that window inside the current page. For example see the below image:
See the "B" is the current page, and "A" is another part that will remain same even if you scroll "B". I want to show in box like "A".
What is the name of such things? How can I do that?
PS: The side bar should not hide the current page, both should be completly visible.
COMMENT 1: We reqiure a dunamic solution, since:
document.querySelector('body').style.position = "absolute";
fixes the problem for wikipedia, but as I said, there are numerous ways different websites could've been implemented - there can be all kinds of wacky absolute/fixed/sticky positioned elements some sites could be using, that could go over your sidebar or even affect your sidebar's styling. The only way to make it work is to test it on multiple different pages, see what breaks and iteratively apply more and more fixes until you are satisfied. – Swiffy