惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
A
About on SuperTechFans
J
Java Code Geeks
量子位
博客园 - 三生石上(FineUI控件)
博客园 - Franky
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
Martin Fowler
Martin Fowler
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

Fooleap's Blog

渴望理想 | Fooleap's Blog 19 年底一些事 | Fooleap's Blog 藉秋风,跑起来 | Fooleap's Blog 一个人去跑步 | Fooleap's Blog 8 月的跑量仿佛是那暑假的作业 | Fooleap's Blog 三伏天跑步那么难受,为何还要跑? | Fooleap's Blog 解决小程序开发“当前系统代理不是安全代理” | Fooleap's Blog 忘却配速的夏日跑步 | Fooleap's Blog 六月天时“带水”跑步更爽 | Fooleap's Blog 出来混迟早要还的 | Fooleap's Blog 跑步不能当饭吃 | Fooleap's Blog 将京东移动端详情页链接转为 PC 端 | Fooleap's Blog 不是热就是雨 | Fooleap's Blog 这半月,我跑了 11 个 520 | Fooleap's Blog 跑完流汗一时爽,一直流汗一直爽 | Fooleap's Blog 初夏夜跑 | Fooleap's Blog Electron 中打开 QQ 临时会话 | Fooleap's Blog 春节期间的培隆角 | Fooleap's Blog 从春天跑到夏天 | Fooleap's Blog 晨雾中奔跑 | Fooleap's Blog 漫步春雨中 | Fooleap's Blog 跑在木棉花下 | Fooleap's Blog 我在春节依然坚持跑步 | Fooleap's Blog 伴随着日出跑步 | Fooleap's Blog 没有最好,只有更好 | Fooleap's Blog 电子气温计 | Fooleap's Blog 渡亭小学的金凤花 | Fooleap's Blog 随心而跑 | Fooleap's Blog 2018 跑步小结 | Fooleap's Blog 在 gVim 中使用“非等宽字体” | Fooleap's Blog
像 Disqus 一样获取链接颜色 | Fooleap's Blog
fooleap · 2017-10-05 · via Fooleap's Blog

平常接触 Disqus 比较多的朋友不难发现,Disqus 原生评论列表的链接颜色,似乎是直接采用所应用页面的主链接颜色,也就是出现次数最多的链接颜色。

那么,怎样获取到页面的主链接颜色呢?思路是遍历所有的 <a> 标签,并获取到相应的颜色值,存起来再比较,以找出出现次数最多的那个颜色值。

获取 CSS 属性值,采用 window 的 getComputedStyle 方法[1]没跑。因实现起来不麻烦,就不多话,具体代码可如下:

function getColor(){
    var links = document.getElementsByTagName('a');
    var colors = {};
    var max = 0;
    var linkColor;
    for (var i = 0; i < links.length; i++) {
        var color = getComputedStyle(links[i]).color;
        colors[color] = (colors[color] || 0) + 1;
    }
    for (var color in colors) {
        var count = colors[color];
        if (count > max) {
            max = count;
            linkColor = color;
        }
    }
    return linkColor || rgb(46, 159, 255);
}

经 @xcatliu 提醒,Disqus 使用的不是出现次数最多的颜色值,而是 id 为 disqus_thread 中的 a 标签继承的颜色值,也就是说,若想改变 Disqus 评论框的链接颜色,只需用 CSS 指定一下就可以了,如:

#disqus_thread a{
    color: #ff0;
}

关于 Disqus 是怎么获取到颜色值,又是怎么传到 iframe 里的, @xcatliu 解释得很清楚,就不再废话。

参考资料

本文历史

  • 2017 年 10 月 05 日 完成初稿
  • 2017 年 10 月 14 日 完善

最近更新

猜你喜欢