【问题标题】:How to insert sidebar inside the current page to show output?如何在当前页面内插入侧边栏以显示输出?
【发布时间】:2022-08-09 19:18:46
【问题描述】:

我的 Chrome 扩展程序从当前页面复制文本并在当前页面左侧的框内显示,即使我滚动当前页面,该文本也将保持固定。代码是:

内容.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;    
}


                                               
                                               

清单.json

{
  \"name\": \"\",
  \"description\": \"\",
  \"version\": \"1.0\",
  \"manifest_version\": 3,
  \"content_scripts\": [
    {
      \"matches\": [\"<all_urls>\"],
      \"js\": [\"content.js\"]
    }
  ],
  \"permissions\": [\"tabs\"]
}   

现在我有一个弹出的窗口,但我想在当前页面中有那个窗口。例如看下图:

看到 \"B\" 是当前页面,而 \"A\" 是另一部分,即使滚动 \"B\" 也将保持不变。我想在“A”之类的框中显示。

这些东西叫什么名字?我怎样才能做到这一点?

PS:侧边栏不应该隐藏当前页面,两者都应该完全可见。

评论 1:我们需要一个动态的解决方案,科学:

document.querySelector(\'body\').style.position = \"absolute\"; 修复 维基百科的问题,但正如我所说,有很多方法 可以实施不同的网站 - 可以有各种各样的 古怪的绝对/固定/粘性定位元素,一些网站可能是 使用,这可能会超出您的侧边栏甚至影响您的侧边栏 造型。使其工作的唯一方法是在多个 不同的页面,看看什么中断并迭代地应用越来越多 修复直到您满意为止。 - 斯威夫

标签: javascript html google-chrome dom sidebar


【解决方案1】:

这是一种方法,尽管实际的解决方案取决于您要注入侧边栏的网站是如何构建的。在这里,我们只是使用 margin-left 将网站内容移出侧边栏。其他方法可能包括将网页内容以及侧边栏浮动到左侧。 div 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>

内容脚本

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);

显现

{
  "name": "asd",
  "description": "asd",
  "version": "1.0",
  "manifest_version": 3,
  "content_scripts": [ 
  {
    "js": ["index.js" ],
    "matches": ["*://*.stackoverflow.com/*"]
  }]
}

【讨论】:

  • 你的代码应该根据我的content.js,你只是模仿了我提供的链接中的代码......我如何在我的扩展中为任何给定页面实现这个......细节缺失......
  • 可以说你的问题是“如何在当前页面中插入侧边栏”,“这样的东西的名称是什么”,并且有一个限制“侧边栏不应该隐藏当前页面”。正如我所说的“div some_webpage 代表,例如某个网页的正文元素。”,这意味着您只需将 div 附加到某个网页的正文中。我尝试将我的解决方案作为内容脚本和it works
  • 我不确定您需要什么详细信息,但我能做的最好的就是将我的清单以及第二次脚本添加到我的答案中。
  • 它不能按我的要求工作,它会覆盖当前页面(请参阅帖子脚本),因此隐藏了当前页面左侧的某些部分,请参阅:i.stack.imgur.com/rl08V.jpg
  • document.querySelector('body').style.position = "absolute"; 解决了维基百科的问题,但正如我所说,可以通过多种方式实现不同的网站 - 有些网站可能会使用各种古怪的绝对/固定/粘性定位元素,这可能会过去您的侧边栏甚至影响侧边栏的样式。让它工作的唯一方法是在多个不同的页面上测试它,看看有什么问题,然后迭代地应用越来越多的修复,直到你满意为止。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-13
  • 2021-06-14
  • 2017-06-07
  • 2012-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多