html - 将 <style> CSS 脚本制作成 Greasemonkey 脚本

标签 html css greasemonkey

我有以下 CSS 代码:

<style>
.crumbContTop > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > th:nth-child(1)     {
    position: fixed;
    bottom: 0px;
    width: 98%;
}

#navMenuContainer {
    position: fixed;
    top: 5px;
    z-index: 2;
}

#navBar > div:nth-child(1) {
    position: fixed;
    top: 5px;
    z-index: 1;
    right:0px;
}

#navBar {
    position: fixed;
    top: 0px;
    z-index: 1;
    width:4000px;
    height:15px;
}

td.cell1 {
    position:fixed;
    bottom:0px;
    z-index:1;
    padding:20px;
    right:0px;
    opacity:0.7;
    visibility:visible !important;
}

#main-header {
    position:fixed;
    top:0px !important;
    z-index:1 !important;
    width:100%;
}
</style>

我只是想将它转换成 Greasemonkey 脚本,基本上这修复了屏幕顶部和底部的导航栏的位置。 问题是 greasemonkey 告诉我它需要一个标识符而不是“<”。

有什么帮助吗?

谢谢。

(还有一个问题,如何让位置静止,但是当物体离开屏幕时它就固定了?)

最佳答案

编辑:

正如 Hellion 指出的那样,这由 Greasemonkey 用 GM_addStyle 覆盖.来自维基:

GM_addStyle("body { color: white; } /* CSS etc, etc */");

Greasemonkey 只是 JS,所以你不能直接在 Greasemonkey 脚本中使用 CSS;您需要以某种方式将其注入(inject)页面。

原答案:

http://www.techradar.com/us/news/internet/the-beginner-s-guide-to-greasemonkey-scripting-598247/2 上展示,您可以添加一个函数来向页面添加样式表,然后将您的 CSS 存储在一个字符串中:

function addCss(cssString) {
    var head = document.getElementsByTagName('head')[0];
    var newCss = document.createElement('style');
    newCss.type = "text/css";
    newCss.innerHTML = cssString;
    head.appendChild(newCss);
}

并且在您的脚本中的某个时刻,在您当前的 CSS 上调用 addCss:

addCss("#mainHeader { position:fixed;} /* more CSS here */");

关于html - 将 &lt;style&gt; CSS 脚本制作成 Greasemonkey 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22385673/

相关文章:

html - 网站在移动设备上拉伸(stretch)和收缩

html - 对齐 div 标签内的文本

javascript - Greasemonkey:样式 ="display: none;"更改时如何做?

javascript - 用于选择选择选项和单击按钮的 JQuery 代码不起作用?

jQuery nestedsortable插件默认添加css

javascript - 我有一个带有 onClick 处理程序的 div,还有带有 onClick 事件的按钮。如何防止按钮触发 div onclick?

jquery - 如何将CSS箭头插入html页面并使其可点击

javascript - 使用 Greasemonkey 禁用页面重定向

javascript - 如何使用用户脚本覆盖警报功能?

python - 将 Markdown 渲染为 HTML,同时保留标题、换行符等?