网页去色变黑白+网页黑白恢复为彩色
创始人
2024-03-09 00:54:23
0

前言

  • 特定节日,你会发现网页和app首页都会变成灰色,以此来表达我们的哀思之情。

  • 好奇宝宝想知道各个网站都是使用哪些小技巧来做出这种效果的(由彩变灰,由灰变彩),于是稍微学习了一下…

由灰变彩

在这里插入图片描述
稍微想想,应该是在CSS中设置了灰色的样式。所以,F12大法瞧瞧呗…
不出意外基本都是加了“gray”相关的CSS样式,大部分都直接加在标签中,少部分是加在

中,对于图片可能会替换灰色图片,如百度。
按下F12后,搜索源码中的关键字“gray”,即可找到。如图:
在这里插入图片描述


在这里插入图片描述

在这里插入图片描述


在这里插入图片描述在这里插入图片描述

src="//www.baidu.com/img/flexible/logo/pc/index_gray.png"

  • 接下来就简单了,把对应的代码删除就可以恢复色彩。这个方法有一个不足之处就是,一旦页面刷新之后,就又变成灰色了。

那就借助一下插件脚本的力量:

  • 一个是谷歌的插件“Permanent Inspect Element”,作用是保持修改页面的状态,也就是你对源码做的修改会保存,刷新之后也会保存。
  • 另一个就是使用油猴插件“黑白网页恢复彩色”或者“我要彩色”,两个都可以达到效果。
    第一个脚本源码:
// ==UserScript==
// @name         黑白网页恢复彩色
// @namespace    http://tampermonkey.net/
// @version      1.4.5
// @license      MIT
// @description  黑白网页恢复彩色,匹配所有网页,即装即用。
// @author       https://greasyfork.org/users/574395-frammolz-amanda
// @match        *://*/*
// @grant        none
// ==/UserScript==(function() {var filter = document.createElement('style');filter.type = 'text/css';document.getElementsByTagName('head')[0].appendChild(filter);filter.appendChild(document.createTextNode("*{-webkit-filter:none!important;}"));var windowUrl = window.location.href;var baidudesk = /https:\/\/www.baidu.com/;var baidumobile = /https:\/\/m.baidu.com/;var kafan = /https:\/\/bbs.kafan.cn/;var qpic = /https:\/\/www.58pic.com/;if( windowUrl.match(baidudesk)){document.getElementById("s_lg_img").setAttribute("src","https://www.baidu.com/img/flexible/logo/pc/index.png");document.getElementById("s_lg_img_new").setAttribute("src","https://www.baidu.com/img/flexible/logo/pc/index.png");document.getElementById("su").style.setProperty("background-color","#4e6ef2","important");if (document.getElementsByClassName("index-logo-src").length==1){document.getElementsByClassName("index-logo-src")[0].setAttribute("src","https://www.baidu.com/img/flexible/logo/pc/result.png");document.getElementsByClassName("index-logo-peak")[0].setAttribute("src","https://www.baidu.com/img/flexible/logo/pc/result.png");document.getElementsByClassName("index-logo-srcnew")[0].setAttribute("src","https://www.baidu.com/img/flexible/logo/pc/result.png");}}if( windowUrl.match(baidumobile)){document.getElementById("logo").getElementsByTagName("a")[0].getElementsByTagName("img")[0].setAttribute("src","https://www.baidu.com/img/flexible/logo/logo_web.png");document.getElementById("index-bn").style.setProperty("background-color","#4e6ef2","important");}if( windowUrl.match(kafan)){document.getElementById("nv_forum").style.setProperty("background-blend-mode","normal");}if( windowUrl.match(qpic)){document.documentElement.style.setProperty("-webkit-filter","none","important");}
})();

第二个脚本源码:

// ==UserScript==
// @name 我要彩色
// @version 0.1
// @description Color back
// @author voltachan(https://github.com/voltachan)
// @match http*://*/*
// @grant GM_addStyle
// @run-at document-start
// @namespace https://greasyfork.org/users/438767
// ==/UserScript==(function() {
GM_addStyle("* {filter: none !important}");
//GM_addStyle("* {-webkit-filter:grayscale(0)! important;-moz-filter:grayscale(0) !important;-ms-filter:grayscale(0) !important;-o-filter:grayscale(0) !important;filter:grayscale(0) !important;filter:none !important;}");
})();

由彩变灰

通过对上述网站的学习,发现都是修改了css样式,只要加上样式或者修改灰色图片就可以达到全站变灰的效果。所以上样式:

样式1

css文件

.grayscale {--tw-grayscale: grayscale(100%);-webkit-filter: grayscale(100%); /* webkit */-moz-filter: grayscale(100%); /*firefox*/-ms-filter: grayscale(100%); /*ie9*/-o-filter: grayscale(100%); /*opera*/
filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1);
filter:grayscale(100%);/* W3C */ 
filter:gray /* IE6-9 */
}

使用


样式2,网页图片 变灰

js文件(代码来自Ajax Blender)

varimgObj = document.getElementById('js-image');
functiongray(imgObj) {
varcanvas = document.createElement('canvas');
varcanvasContext = canvas.getContext('2d');varimgW = imgObj.width;
varimgH = imgObj.height;
canvas.width = imgW;
canvas.height = imgH;canvasContext.drawImage(imgObj, 0, 0);
varimgPixels = canvasContext.getImageData(0, 0, imgW, imgH);for(vary = 0; y < imgPixels.height; y++){
for(varx = 0; x < imgPixels.width; x++){
vari = (y * 4) * imgPixels.width + x * 4;
varavg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
}
}
canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
returncanvas.toDataURL();
}
imgObj.src = gray(imgObj);

样式3
来自SVG Filter.

创建一个SVG文件,并将以下代码写在里面,保存命名为***.svg






然后利用过滤器的属性,我们可以通过SVG文件中的元素的ID连接SVG文件

img {
filter:url('img/gray.svg#grayscale');
}

也可以把它放到CSS文件中,例如:

img {
filter:url('url("data:image/svg+xml;utf8,#grayscale");')
}

测试代码

网页黑白图

参考链接

网页彩色图片全部变灰色(黑白)
让网页图片变灰色的三种方法

相关内容

热门资讯

同台竞技展风采 2025年吉林... 央广网长春12月19日消息(记者舒震)12月18日,由吉林金融工会、吉林省商业金融服务业工会主办,吉...
示范文本,助力诉讼体验再升级 人民法院会同司法行政部门、律师协会推动诉辩状示范文本全面推广、扩容升级,便利群众高效实质解决纠纷、促...
普京回应BBC记者:特朗普起诉... 在12月19日举行的俄罗斯总统“直播连线”活动上,英国广播公司(BBC)驻莫斯科记者史蒂夫·罗森伯格...
因发生交通事故构成犯罪,14人... 近期,北京公安交管部门对14名驾驶人依法作出终生禁驾处罚,其中年龄最大的67岁,年龄最小的32岁。 ...
长裕集团主板IPO过会,公司内... 北京商报讯(记者 马换换 王蔓蕾)12月19日,上交所官网显示,长裕控股集团股份有限公司(以下简称“...
明星电力(600101)披露拟... 截至2025年12月19日收盘,明星电力(600101)报收于9.22元,较前一交易日上涨1.65%...
布朗大学枪击案与MIT教授遭枪... 据央视新闻,当地时间18日,美国波士顿联邦官员表示,13日布朗大学枪击案的嫌疑人与15日麻省理工学院...
应星控股(01440.HK)委... 应星控股(01440.HK)公布,徐静女士已不再担任该公司公司秘书、授权代表及法律程序代理人,自20...
首都机场公安侦破一起18年命案... 据@公安部新闻传媒 12月19日消息,首都机场公安侦破一起18年命案积案。 近日深夜,首都机场公安...
“摸摸党”4买4退华为Mate... 央视新闻今日(12 月 19 日)报道了一起“摸摸党 4 买 4 退同款手机被拒、起诉退货结果被法院...