简体   繁体   English

如何确定javascript中文本的实际backgroundColor和颜色(firefox扩展代码)

[英]how to determine actual backgroundColor and color of text in javascript (firefox extension code)

I am writing a firefox extension (in javascript). 我正在写一个Firefox扩展(在javascript中)。

When the browser operator pauses his mouse cursor over a "term" (word/phrase/acronym) in the HTML browser document, I need to highlight the text that displays that term by changing the color of that term (not the entire text node). 当浏览器操作员将鼠标光标悬停在HTML浏览器文档中的“术语”(单词/词组/缩写)上时,我需要通过更改该术语的颜色(而不是整个文本节点)来突出显示该术语的文本。

But I need to know the backgroundColor attribute and text color attribute at that point to compute an appropriate color to achieve a visible and pleasing "highlight" of the term. 但是我需要知道那时的backgroundColor属性和text color属性,以计算适当的颜色,以实现该术语的可见和令人愉悦的“突出显示”。

What I can't figure out is how to get the values of the backgroundColor and color attributes that will be active at random places in arbitrary HTML documents. 我不知道的是如何获取在任意HTML文档中随机位置有效的backgroundColor和color属性的值。 I can't just write code that reads the style.color of the text node (doesn't exist), or the element that contains the text node (because that element usually won't contain any element.style.color or element.style.backgroundColor specification at all). 我不能只编写读取文本节点的style.color(不存在)或包含文本节点的元素的代码(因为该元素通常将不包含任何element.style.color或element)。 style.backgroundColor规范)。

As far as I can tell, the backgroundColor and color attributes of a random element (and the text nodes contained in that element) can come from anywhere up the node hierarchy, or from somewhere in some other block in the document (like "head"), or from other javascript code within the HTML document, or from some "style-sheet" specification either in the HTML document or linked into the HTML document. 据我所知,随机元素(以及该元素中包含的文本节点)的backgroundColor和color属性可以来自节点层次结构中的任何位置,也可以来自文档中其他某些块的位置(例如“ head”) ),或者来自HTML文档中的其他javascript代码,或者来自HTML文档中或链接到HTML文档中的某些“样式表”规范。

So I'm looking for a way for javascript to determine what backgroundColor and color attributes will be actually be applied by the browser to arbitrary HTML elements in arbitrary HTML documents, hopefully without needing to write code to walk all the way up the nested hierarchy, then perhaps look around in other sections of the document (like "head" and maybe others), then perhaps find out what CSS files apply and try to read and parse them too! 因此,我正在寻找一种让javascript确定浏览器将实际将哪些backgroundColor和color属性应用于任意HTML文档中的任意HTML元素的方法,希望该方法无需编写代码就可以遍历嵌套层次结构,然后也许浏览文档的其他部分(例如“ head”以及其他部分),然后找出适用的CSS文件并尝试读取和解析它们! That would be a horrible pain, and extremely wasteful since the browser obviously already figured all this out in order to display the HTML. 这将是一个可怕的痛苦,并且非常浪费,因为浏览器显然已经想出了所有这些来显示HTML。

So, what does the javascript look like that would determine the actual backgroundColor and color attributes that the browser will apply to a given text-node (or the element that surrounds that text node)? 那么,javascript看起来像什么,将决定浏览器将应用于给定文本节点(或包围该文本节点的元素)的实际backgroundColor和color属性?

I assume changing the text color once I compute it will be simple, probably something like: 我假设一旦计算出文字颜色就很简单,大概是这样的:

element.style.color = newcolor;

But the javascript to determine the browser applied backgroundColor and color is another matter. 但是确定浏览器应用的backgroundColor和color的javascript是另一回事。

My searches found the following function, but from what I understand this does not take into account every way these attributes might be modified before they are applied by the browser: 我的搜索找到了以下功能,但据我了解,这并未考虑在浏览器应用这些属性之前对其进行修改的所有方式:

var fgc = window.getComputedStyle(element).getPropertyValue("color");
var bgc = window.getComputedStyle(element).getPropertyValue("backgroundColor");

What's the solution? 有什么解决方案?

Well, I'm not aware of any perfect way to do so... 好吧,我不知道有什么完美的方法...

However, the getComputedStyle() approach should work reasonably well if you traverse parent nodes as well if the actual node does not yield a viable result. 但是,如果遍历父节点,并且实际节点不能产生可行的结果,则getComputedStyle()方法应该也可以正常工作。

Here is my unoptimized implementation: 这是我未优化的实现:

function colorForElement(element, what) {
    var color = null;
    var w = element.ownerDocument.defaultView;
    while (element) {
        color = w.getComputedStyle(element).getPropertyValue(what);
        if (color && color != "transparent") {
            break;
        }
        element = element.parentElement;
    }
    return color;
}

I might have missed some edge-cases, but this is a good baseline function, IMO. 我可能会错过一些极端情况,但这是一个很好的基线函数,IMO。

I tested this with the following on this very page: 我在此页面上使用以下内容对此进行了测试:

addEventListener("click", function(e) {
    console.log("fore", colorForElement(e.originalTarget, "color"));
    console.log("back", colorForElement(e.originalTarget, "background-color"));
}, true);

Mmm, i'd need to see your code because i'm not sure that i understood your problem. 嗯,我需要查看您的代码,因为我不确定我是否理解您的问题。 Anyqay if you want to use Javascript you can use Jquery with .hover() method to put the nowcolor in a variable so you can confront it and change as you need. 无论如何,如果您想使用Javascript,可以将Jquery与.hover()方法结合使用,将nowcolor放入变量中,以便您可以面对它并根据需要进行更改。

I mean: 我的意思是:

var color = $('#content_text').attr('color');
var bgcolor = $('#content_text').attr('backgound-color');

then using jquery text() i think that you can solve your problem. 然后使用jquery text()我认为您可以解决您的问题。 If you post some code i hope i can help you more. 如果您发布一些代码,希望我能为您提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM