Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 编写替换背景图像的用户脚本_Javascript_Background_Greasemonkey_Userscripts - Fatal编程技术网

Javascript 编写替换背景图像的用户脚本

Javascript 编写替换背景图像的用户脚本,javascript,background,greasemonkey,userscripts,Javascript,Background,Greasemonkey,Userscripts,这是密码 // ==UserScript== // @name Wood Background // @namespace http://www.nationstates.net/nation=ellorn // @description Changes background to wood finish // @include http:*//w11.zetaboards.com/Allied_Republics/* // ==/UserScri

这是密码

// ==UserScript==
// @name           Wood Background
// @namespace      http://www.nationstates.net/nation=ellorn
// @description    Changes background to wood finish
// @include       http:*//w11.zetaboards.com/Allied_Republics/*
// ==/UserScript==


function addCss(cssString) {
    var head = document.getElementsByTagName('head')[0];
    return unless head;
    var newCss = document.createElement('style');
    newCss.type = "text/css";
    newCss.innerHTML = cssString;
    head.appendChild(newCss);
}  
addCss (
    '* { background: #00ff00                          url('http://awesomewallpapers.files.wordpress.com/2010/01 /wooden_top.jpg') no-repeat 

fixed center;  }'
);

我正在尝试替换网站的背景:

您可以定义自己的用户样式表来覆盖网站的作者样式表。我认为这比编写javascript更适合改变CSS

下面是关于如何实现这一目标的说明

除此之外,如果您正确地移交CSS字符串(固定引号和换行符),您的代码通常应该可以工作:


但是,请注意,
*
将中断,因为您将木制背景应用于每个元素,而不仅仅是背景元素(HTML或body或包装器div)。

对于样式更改,请使用(几乎每个浏览器上都有类似的版本)

Style比Greasemonkey或userscripts更快、更轻、更简单。有很多预先制作的款式可供选择

除此之外,请使用内置函数,如

下面的脚本适用于Firefox和Chrome,可能还有一些其他浏览器。(CSS字符串中存在语法错误):


请注意,最好将
*
替换为
正文

您知道您的报价已被打断,并且您不能像那样使用换行符而不转义,对吗?请尝试
添加CSS(“*{background:#00ff00 url('http://awesomewallpapers.files.wordpress.com/2010/01/wooden_top.jpg')无重复固定中心;}“
我建议将
*
替换为
主体
或其他选择器,
*
将产生不可预测的效果。
var css = "* { background: #00ff00 url('http://awesomewallpapers.files.wordpress.com/2010/01/wooden_top.jpg') no-repeat fixed center;  }";

addCss ( css );
// ==UserScript==
// @name        Wood Background
// @namespace   http://www.nationstates.net/nation=ellorn
// @description Changes background to wood finish
// @include     http:*//w11.zetaboards.com/Allied_Republics/*
// ==/UserScript==

GM_addStyle (
    "* { background: #00ff00 "
    + "url('http://awesomewallpapers.files.wordpress.com/2010/01/wooden_top.jpg')"
    + " no-repeat fixed center; }"
);