【发布时间】: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\";修复 维基百科的问题,但正如我所说,有很多方法 可以实施不同的网站 - 可以有各种各样的 古怪的绝对/固定/粘性定位元素,一些网站可能是 使用,这可能会超出您的侧边栏甚至影响您的侧边栏 造型。使其工作的唯一方法是在多个 不同的页面,看看什么中断并迭代地应用越来越多 修复直到您满意为止。 - 斯威夫
-
@Yogi 它没有,但以下帖子接近我想要的,唯一的问题是,它覆盖了现有页面,我需要查看完整的当前页面:stackoverflow.com/questions/39610205/…
-
侧边栏有
position: stickycss-rule,无论滚动如何,它都会保持在那里。要使侧边栏也不会越过页面内容,您需要将侧边栏注入网页左侧,并将页面的其余部分向左“移动”侧边栏的宽度。 -
@Swiffy 喜欢
document.body.style.width = window.innerWidth - 100 + \"px\";?试过但没用,在这里找到:stackoverflow.com/q/56605652/11974735
标签: javascript html google-chrome dom sidebar