0

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:

enter image description here

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.

enter image description here

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

  • Does this answer your question? [How to create sidebar in Google Chrome Extension?](https://stackoverflow.com/questions/35602254/how-to-create-sidebar-in-google-chrome-extension) – Yogi Jun 16 '22 at 17:35
  • @Yogi it does not but the following post is close to what I want, the only problem is, it overwrites the existing page, I need to see full current page:https://stackoverflow.com/questions/39610205/how-to-make-side-panel-in-chrome-extension – Consider Non-Trivial Cases Jun 19 '22 at 09:40
  • Other related post: 1. https://stackoverflow.com/q/65115299/11974735 2. https://stackoverflow.com/q/56605652/11974735 – Consider Non-Trivial Cases Jun 19 '22 at 11:10
  • The sidebar has `position: sticky` css-rule, which makes it stay there regardless of scrolling. To have the sidebar also not go over the page content, you need to inject the sidebar to the left of the webpage and kind of "shift" the rest of the page to the left by the width of your sidebar. – Swiffy Jun 19 '22 at 14:33
  • @Swiffy like `document.body.style.width = window.innerWidth - 100 + "px";`? tried but didn't worked, found here: https://stackoverflow.com/q/56605652/11974735 – Consider Non-Trivial Cases Jun 19 '22 at 14:34

1 Answers1

1

Here's one way to do it, although the actual solution depends on how the website you want to inject the sidebar in is constructed. Here we just use margin-left to move the website content out of the way of the sidebar. Other methods could include floating the webpage contents as well as the sidebar to left. The div some_webpage represents, e.g. the body element of some webpage.

function addSideBar() {
  let barWidth = 100;

  // Make sidebar
  let div = document.createElement("div");
  div.style.position = "fixed";
  div.style.left = "0";  
  div.style.top = "0";
  div.style.width = `${barWidth}px`;
  div.style.height = "100vh";
  div.style.zIndex = "9999";
  div.style.backgroundColor = "white";
  div.style.color = "black";

  div.innerHTML = "<p>This is a sidebar</p>";

  // Float the current page to right
  document.querySelector('#some_webpage').style.marginLeft = `${barWidth}px`;
  document.querySelector('#some_webpage').style.maxWidth = `calc(100vw - ${barWidth}px)`;
  document.querySelector('body').style.position = "absolute";


  // Inject to current page
  document.querySelector('#some_webpage').prepend(div);
}
<div id = "some_webpage">
  <h1> Some webpage header </h1>
  <p> Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content textSome webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text  Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text Some webpage content text </p>
  <button onClick="addSideBar()">Add sidebar</button>
</div>

Content script

let barWidth = 100;

// Make sidebar
let div = document.createElement("div");
div.style.position = "fixed";
div.style.left = "0";  
div.style.top = "0";
div.style.width = `${barWidth}px`;
div.style.height = "100vh";
div.style.zIndex = "9999";
div.style.backgroundColor = "white";
div.style.color = "black";

div.innerHTML = "<p>This is a sidebar</p>";

// Float the current page to right
document.querySelector('body').style.marginLeft = `${barWidth}px`;
document.querySelector('body').style.maxWidth = `calc(100vw - ${barWidth}px)`;
document.querySelector('body').style.position = "absolute";


// Inject to current page
document.querySelector('body').prepend(div);

manifest

{
  "name": "asd",
  "description": "asd",
  "version": "1.0",
  "manifest_version": 3,
  "content_scripts": [ 
  {
    "js": ["index.js" ],
    "matches": ["*://*.stackoverflow.com/*"]
  }]
}
Swiffy
  • 4,401
  • 2
  • 23
  • 49
  • Your code should be according to my `content.js`, you just mimicked the code from the link I provided...how do I implement this in my extension for any given page... details are missing.. – Consider Non-Trivial Cases Jun 19 '22 at 15:09
  • Arguably your questions were "How to insert sidebar inside the current page", "What is the name of such thing" and there was a restriction "The side bar should not hide the current page". As I said "The div some_webpage represents, e.g. the body element of some webpage.", which means that you'd just append the div into the body of some webpage. I tried my solution as a content script and [it works](https://i.imgur.com/g5AcC1B.png) – Swiffy Jun 19 '22 at 15:36
  • I'm not sure what details you need, but the best I can do is to add my manifest, as well as the script for the second time to my answer. – Swiffy Jun 19 '22 at 15:39
  • It does not work as I asked, it overwrites the current page (see the post script), thus hides some part of the left side of the current page, see: https://i.stack.imgur.com/rl08V.jpg – Consider Non-Trivial Cases Jun 19 '22 at 15:57
  • 2
    `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 Jun 19 '22 at 17:07
  • I have awarded you the bounty, but your solution is not what was asked, if possible provide a complete solution, thanks. – Consider Non-Trivial Cases Jun 26 '22 at 17:04