【问题标题】:Change a color code to another using JavaScript in browser console在浏览器控制台中使用 JavaScript 将颜色代码更改为另一种颜色代码
【发布时间】:2018-08-09 06:39:58
【问题描述】:

我是一名网页设计师。

当我浏览网站时,我喜欢改变一些颜色,看看当我改变原色时网站的显示效果。

我使用“检查元素”来执行此操作。但这是非常耗时的工作,因为我需要添加大量代码才能到处更改。

是否有任何 JavaScript 代码可用于使用浏览器控制台将一种颜色代码更改为另一种颜色代码。

基本上我想要的是下面的......

使用浏览器控制台将本站点中所有位置的 #FFF8DC 颜色更改为 #e6dfc6 颜色。

【问题讨论】:

  • 可能不会。但是我还没有对所有浏览器的功能进行详尽的搜索。
  • 当然...迭代每一个元素 - 每个元素的计算样式...遍历所有这些属性以查找源颜色,您可能需要查找 #fff8dcrgb(255,248,220)rgba(255,248,220, \d+) 中的任何一个或全部(\d+ 表示任何数字) - 然后将其更改为您的目标颜色 - 应该是大约十几行代码
  • ^ 也不要忘记hsla。 (您可能还想考虑#ffd#edc?)
  • 实际上 getComputedStyle 应该总是返回格式为 "rgb(nnn, nnn, nnn)"rgba(nnn, nnn, nnn, n) 的计算值,并且整个事情可以减少到不到十几行:switchColor=(c1, c2) => { [...document.all].forEach(e=>{ let s = getComputedStyle(e); [...s].forEach(k=>{if(s[k]===c1)e.style[k]=c2}) }) }; 在这个页面中可以测试 @987654330 @
  • @IamtheMostStupidPerson 您想在下面对我的回答写下反馈吗?

标签: javascript css browser colors browser-console


【解决方案1】:

原则

正如 Kaiido 评论的那样:.getComputedStyle() 应始终以 rgb(nnn, nnn, nnn)rgba(nnn, nnn, nnn, n) 的格式返回计算值”

因此,在遍历所有元素计算的样式属性并替换适用的颜色值之后,不应该做太多事情。

我的更新

  • 由于您希望能够“将#FFF8DC 颜色更改为#e6dfc6 颜色”,我修改了此solution (RGB to Hex and Hex to RGB) 中的函数,以便能够在我的sn-p,
  • 修改了我的函数,使其适用于包含多种颜色(例如渐变)的属性值,
  • 添加了strict 作为可选参数,以避免在包含多种颜色的值中进行颜色替换。

片段

// Below function modified from solution here: https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
function hexToRgb(hex) {
  // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
  var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  hex = hex.replace(shorthandRegex, function(m, r, g, b) {
    return r + r + g + g + b + b;
  });

  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? "rgb(" + [
    parseInt(result[1], 16),
    parseInt(result[2], 16),
    parseInt(result[3], 16)
  ].join(', ') + ")" : null;
}

// Function to change a color to another one
function colorChange(colorOld, colorNew, strict = false) {
  // If hex notation, convert to rgb
  if (colorOld.includes('#'))
    colorOld = hexToRgb(colorOld);
  // Loop through all elements styles
  [...document.all].forEach(elm => {
    let cStyle = getComputedStyle(elm);
    [...cStyle].forEach(prop => {
      // Escape if not a string
      if (typeof cStyle[prop] !== 'string') return;
      // Check if colorOld is in property
      if (cStyle[prop].includes(colorOld)){
        // If strict, colorOld is replaced only if it's the only value of the property
        if (!strict || cStyle[prop] === colorOld)
          elm.style[prop] = cStyle[prop].replace(colorOld, colorNew); // Replace color
      }
    })
  })
};

// Then, call your function the way you like !
colorChange("rgb(255, 0, 0)", 'orange');
colorChange("#00ff00", '#125689', true); // Note the use of the “strict” parameter here
colorChange("#00f", 'rgb(255, 0, 128)');
<p style="color: rgb(255, 0, 0);">I was red !</p>
<p style="color: #00ff00;">I was green !</p>
<p style="color: #00f;">I was blue !</p>
<div style="background: linear-gradient(to right, #f00, #0000ff);">
  <p>I was a gradient from red to blue</p>
</div>
<div style="background: linear-gradient(to right, #ff0000, #0f0);">
  <p>I was a gradient from red to green (green is not replaced here, because of the use of “strict”)</p>
</div>

当然,您可以在将这些功能复制/粘贴到控制台后尝试此页面上的功能。 (例如colorChange("#eff0f1", "#ffaaaa");

希望对你有帮助。

【讨论】:

  • 啊,当心,你假设你会从cStyle[prop] 检索一个字符串,而它可能不是(它在我的 FF for Android 上未定义,我不知道哪个属性......) .编辑:可以记录它,它是一些 -moz-window... 道具和一些 css 变量,可能是由 sn-p 的控制台插入的。
  • @Kaiido 我这样改正怎么样:let cProp = cStyle[prop] || '' 然后使用这个变量?
  • 既然你不想打任何东西,我宁愿玩安全和可读性。您将使用字符串方法 (includes),因此请检查您所拥有的确实是一个字符串。你永远不知道几年后 CSS 规范会变成什么样子,如果他们开始接受其他类型(比如数字),你会为这个小 if(typeof cStyle[prop] !== 'string') return; 感到高兴。
【解决方案2】:

您写道: 使用浏览器控制台将本站点中的 #FFF8DC 颜色更改为 #e6dfc6

这很容易!在这个网站上,我们在右侧有 #FFF8DCbackgroundColor by "Featured on Meta" div 块。您必须在浏览器控制台中输入:

document.querySelector('.community-bulletin').style.backgroundColor = "#e6dfc6";

或者你可以使用“检查元素”来选择一些元素,就像这个截图一样:

然后要更改此前景色和背景色,您必须将其放入浏览器控制台:

$0.style.color = "orange";
$0.style.backgroundColor = "#e6dfc6";

按下enter key,颜色就会改变。

如果你想一直为控制台使用一些代码,那么你必须使用 sn-ps。转到开发人员工具,然后点击标签"Sources",就像这个截图一样:

然后将您的代码放到一个新的 sn-p 中,然后用鼠标右键单击此 sn-p 中的名称,然后在上下文菜单中单击 "Run"。这个 sn-p 将被执行。不要忘记保存这个 sn-p:用鼠标右键单击这个 sn-p 中的名称,然后在上下文菜单中单击 "Save...",但在显示的 "Save dialog box" 上单击 "Cancel"(这很奇怪)和你的sn-p 将被保存。

【讨论】:

    猜你喜欢
    • 2018-03-15
    • 2017-12-08
    • 2021-06-26
    • 2020-10-03
    • 2021-07-19
    • 2015-04-25
    • 1970-01-01
    • 2022-12-04
    • 2014-05-20
    相关资源
    最近更新 更多