JavaScript:反转页面所有元素的颜色
注意:我在我的问题中保留了最新版本的书签,它运行良好并且基于雅各布的答案。如果您正在寻找要使用的书签,请使用该书签。如果您只想在 chrome 上运行一些令人惊奇的东西,请参阅 leosok 的精彩答案。
我希望能够使用 JavaScript 书签反转页面上所有元素的颜色。我知道要反转颜色,您需要从 255(xFF) 中减去每个 RGB 十六进制值,但除此之外,我不确定如何继续。
我怎样才能做到这一点?
使用 jQuery
是可以接受的,它只需要在 Chrome 上运行,但如果它在 Firefox 中运行那就更好了。
这不包括图像 - 背景、文本和链接颜色都应该反转。基本上任何从 CSS 获取颜色的东西。
更新 这是一个更新的书签,它修复了嵌套元素问题,并且适用于许多不同的网站(包括这个)
UPDATE2 添加了对透明度的一些支持,处理具有默认背景颜色 rgba(0, 0, 0, 0) 的元素。更多网站现在应该可以使用更新后的网站。
javascript: (function ($) {
function load_script(src, callback) {
var s = document.createElement('script');
s.src = src;
s.onload = callback;
document.getElementsByTagName('head')[0].appendChild(s);
}
function invertElement() {
var colorProperties = ['color', 'background-color'];
var color = null;
for (var prop in colorProperties) {
prop = colorProperties[prop];
if (!$(this).css(prop)) continue;
if ($(this).data(prop) != $(this).css(prop)) continue;
if (($(this).css(prop) === 'rgba(0, 0, 0, 0)') || ($(this).css(prop) === 'transparent')) {
if ($(this).is('body')) {
$(this).css(prop, 'black');
continue;
} else {
continue;
}
}
color = new RGBColor($(this).css(prop));
if (color.ok) {
$(this).css(prop, 'rgb(' + (255 - color.r) + ',' + (255 - color.g) + ',' + (255 - color.b) + ')');
}
color = null;
}
}
function setColorData() {
var colorProperties = ['color', 'background-color'];
for (var prop in colorProperties) {
prop = colorProperties[prop];
$(this).data(prop, $(this).css(prop));
}
}
function invertColors() {
$(document).live('DOMNodeInserted', function(e) {
var $toInvert = $(e.target).find('*').andSelf();
$toInvert.each(setColorData);
$toInvert.each(invertElement);
});
$('*').each(setColorData);
$('*').each(invertElement);
$('iframe').each(function () {
$(this).contents().find('*').each(setColorData);
$(this).contents().find('*').each(invertElement);
});
}
load_script('http://www.phpied.com/files/rgbcolor/rgbcolor.js', function () {
if (!window.jQuery) load_script('https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', invertColors);
else invertColors();
});
})(jQuery);
现在适用于我尝试过的大多数网站。然而,背景图像可能会带来问题。
Note: I am keeping an up-to-date version of the bookmarklet in my question which works well and is based on Jacob's answer. If you are looking for a bookmarklet to use, use that one. See leosok's fantastic answer if you just want something amazing that works on chrome.
I want to be able to invert the color of all the elements on a page with a JavaScript bookmarklet. I know that to invert a color you subtract each of the RGB hex values from 255(xFF), but beyond that I am unsure of how to proceed.
How can I accomplish this?
Using jQuery
is acceptable, and it only needs to work on Chrome, although if it worked in Firefox that'd be a plus.
This is excluding images - background, text and links colors should all be inverted. Basically anything that gets its color from CSS.
UPDATE
Here is an updated bookmarklet that fixes the nested element issue and will work on a lot of different sites (including this one)
UPDATE2
Added some support for transparency, handling elements that have default background-color rgba(0, 0, 0, 0). More sites should be working now with the updated one.
javascript: (function ($) {
function load_script(src, callback) {
var s = document.createElement('script');
s.src = src;
s.onload = callback;
document.getElementsByTagName('head')[0].appendChild(s);
}
function invertElement() {
var colorProperties = ['color', 'background-color'];
var color = null;
for (var prop in colorProperties) {
prop = colorProperties[prop];
if (!$(this).css(prop)) continue;
if ($(this).data(prop) != $(this).css(prop)) continue;
if (($(this).css(prop) === 'rgba(0, 0, 0, 0)') || ($(this).css(prop) === 'transparent')) {
if ($(this).is('body')) {
$(this).css(prop, 'black');
continue;
} else {
continue;
}
}
color = new RGBColor($(this).css(prop));
if (color.ok) {
$(this).css(prop, 'rgb(' + (255 - color.r) + ',' + (255 - color.g) + ',' + (255 - color.b) + ')');
}
color = null;
}
}
function setColorData() {
var colorProperties = ['color', 'background-color'];
for (var prop in colorProperties) {
prop = colorProperties[prop];
$(this).data(prop, $(this).css(prop));
}
}
function invertColors() {
$(document).live('DOMNodeInserted', function(e) {
var $toInvert = $(e.target).find('*').andSelf();
$toInvert.each(setColorData);
$toInvert.each(invertElement);
});
$('*').each(setColorData);
$('*').each(invertElement);
$('iframe').each(function () {
$(this).contents().find('*').each(setColorData);
$(this).contents().find('*').each(invertElement);
});
}
load_script('http://www.phpied.com/files/rgbcolor/rgbcolor.js', function () {
if (!window.jQuery) load_script('https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', invertColors);
else invertColors();
});
})(jQuery);
Now works with most sites I've tried. Background images can pose a problem, however.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,在这里获取很棒的
RGBColor
类< /a>.这里是:
jsFiddle 示例
屏幕截图:
编辑:这是一个您现在可以使用的书签复制粘贴到您的浏览器中 (http://jsfiddle.net/F7HqS/1/)
First things first, grab the awesome
RGBColor
class here.Here goes:
jsFiddle example
Screenshot:
EDIT: Here's a bookmarklet you can now copy-paste into your browser (http://jsfiddle.net/F7HqS/1/)
我的解决方案目前似乎仅适用于 Chrome,但它会反转所有内容(包括图像和 iframe),如下所示:
,它不使用外部库,而且非常简单:将
-webkit-filter: invert(100%)
添加到html
选择器。这是小提琴:http://jsfiddle.net/nikita_turing/jVKw6/3/包括小书签。如果有人知道如何使其适用于 Firefox(SVG 过滤器?),请继续!
My solution seems to work only for Chrome right now, but it inverts everything (including images and iframes) as seen here:
Also it does not make use of external libraries and is very simple: adding a
-webkit-filter: invert(100%)
to thehtml
-selector.Here's the fiddle: http://jsfiddle.net/nikita_turing/jVKw6/3/ with the bookmarklet included. If someone has an idea of how to make it work for Firefox (SVG-Filters?), go ahead!
我清理了上面一个答案(由 leosok 提供)中的评论,因此它将作为 Chrome 中的书签使用。请注意,此解决方案比此处当前的最高点更有效,而且即使在运行脚本后 html 发生更改,它也能正常工作。
一行书签。创建一个书签,然后将 url 编辑为:
javascript:(function () { var css = 'html {-webkit-filter: invert(100%);' + '-moz-filter: invert(100%);' + '-o-filter: invert (100%);' + '-ms-filter: invert(100%); var head = document.getElementsByTagName('head')[0]; (!window.counter) { window.counter = 1; } else { window.counter++; if (window.counter % 2 == 0) { var css = 'html {-webkit-filter: invert(0%); moz-filter: 反转(0%); -o-filter: 反转(0%); -ms-filter: 反转(0%); }' } } style.type = 'text/css'; styleSheet) { style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); } head.appendChild(style);
I cleaned up the comments from one of the answers (by leosok) above, so it will work as a bookmarklet in chrome. Note that this solution is more efficient than the current highest-point here, plus it works even if the html changes after you run the script.
One line for bookmarklet. create a bookmark, then edit the url to this:
javascript:(function () { var css = 'html {-webkit-filter: invert(100%);' + '-moz-filter: invert(100%);' + '-o-filter: invert(100%);' + '-ms-filter: invert(100%); }'; var head = document.getElementsByTagName('head')[0]; var style = document.createElement('style'); if (!window.counter) { window.counter = 1; } else { window.counter++; if (window.counter % 2 == 0) { var css = 'html {-webkit-filter: invert(0%); -moz-filter: invert(0%); -o-filter: invert(0%); -ms-filter: invert(0%); }' } } style.type = 'text/css'; if (style.styleSheet) { style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); } head.appendChild(style); }());
接受的答案是完全正确的,只有一个小缺陷。每次切换反转时,它都会在头部添加另一个样式标签。这样做
如果您想撤消反转,只需删除标签即可。效果很好!
The accepted answer is totally correct, with one minor flaw. Each time you toggle the invert it adds ANOTHER style tag to the head. Do this instead
That way you simply remove the tag if you want to undo your invert. Works great!
我认为尝试反转图像会很有趣。没花很长时间就找到了合适的用于图像编辑的 Javascript 库: http://www.pixastic.com/lib /
您可能无法将整个库加载到书签中,但是如果您自己托管它,您可以将类似的内容添加到书签的末尾(在 invertColors 之后):
我认为值得注意的是,如果您的目标就是将一个白色背景的页面变成黑色(反之亦然),可能会更简单。
我刚刚尝试了 Jacob 的书签,并将其与我在 Google 支持论坛上找到的更简单的版本进行了比较:
http://www.google.com/support /forum/p/Chrome/thread?tid=26affebdd0da12d9&hl=en
Jacob 的反转似乎工作频率较低,并且在大页面上需要更长的时间。我想我最终会更频繁地使用天真的版本。
I figured it would be fun to try inverting images. Didn't take long to find an appropriate Javascript library for image editing: http://www.pixastic.com/lib/
You probably can't load that whole library into a bookmarklet, but if you host it yourself you can add something like this to the end of the bookmarklet (after invertColors):
I think it's worth noting that if your goal is to take a page with a white background and make it black (or vice versa), something simpler might be in order.
I just tried the bookmarklet from Jacob and compared it to a more naive version I found on the Google support forums:
http://www.google.com/support/forum/p/Chrome/thread?tid=26affebdd0da12d9&hl=en
Jacob's invert seems to work less frequently and takes quite a bit longer on large pages. I think I'll end up using the naive version more frequently.