简体   繁体   English

如何通过扩展程序获取(未设置)Chrome中选定文本的背景颜色?

[英]How to get (not set) the background color of selected text in Chrome via an extension?

I've seen lots of questions about setting the background color of selected text in Chrome and other browsers using the ::selection selector, but I haven't been able to find anything that discusses getting the color currently being used by the browser on the user's OS. 我已经看到了很多有关使用::selection选择器在Chrome和其他浏览器中设置所选文本的背景色的问题,但是我找不到任何讨论如何获取当前浏览器正在使用的颜色的问题。用户的操作系统。

For a Chrome extension I've created a fake input element, and would like to style the "selected" text in that element with the same foreground and background colors as used in real input elements. 对于一个Chrome扩展程序,我创建了一个伪造的input元素,并希望使用与实际输入元素相同的前景色和背景色来对该元素中的“选定”文本设置样式。 So I'm wondering if there's an extension API in Chrome that lets JavaScript look up the colors currently being used by the browser for selected text. 因此,我想知道Chrome中是否有扩展API,可让JavaScript查找浏览器当前用于所选文本的颜色。 Given different combinations of platform, OS version and desktop theme, those colors may change from user to user, so simply hard-coding a couple of values isn't sufficient (though that's what I'm doing currently). 给定平台,操作系统版本和桌面主题的不同组合,这些颜色可能因用户而异,因此仅硬编码几个值是不够的(尽管这是我目前正在做的事情)。

One approach might be to render an input with selected text into a canvas and sample the colors, though I haven't investigated whether taking what's essentially a screenshot is possible via an extension without user interaction (the desktopCapture API seems to show a dialog to the user). 一种方法可能是将带有选定文本的输入呈现到画布中并对颜色进行采样,尽管我尚未调查是否可以通过扩展而无需用户干预来获取本质上的屏幕快照( desktopCapture API似乎显示了一个对话框,用户)。 So I'm hoping there's an API for looking up the colors used by various UI elements. 因此,我希望有一个API可以查找各种UI元素使用的颜色。 Simply getting access to the browser's default user agent style sheet would suffice. 只需访问浏览器的默认用户代理样式表就足够了。

Rob W points out that the chrome.tabs.captureVisibleTab call can capture the visible area of the current tab into a dataUrl , which could then be rendered into a canvas. Rob W 指出chrome.tabs.captureVisibleTab调用可以将当前选项卡的可见区域捕获到dataUrl ,然后可以将其呈现到画布中。 By inserting an <input> element at a known location, putting some text into it, focusing the element, selecting the text and then doing the screen capture, you could scan the canvas for the element's pixels and pick out the selection's foreground and background colors. 通过在已知位置插入<input>元素,在其中放置一些文本,聚焦该元素,选择文本,然后进行屏幕捕获,您可以在画布上扫描该元素的像素,并选择该选择的前景色和背景色。 Bit kludgy, but it should work. 有点笨拙,但应该可以。

The drawback is that this feature requires the <all_urls> permission, even if your extension only needs to work on one domain. 缺点是此功能需要<all_urls>权限,即使您的扩展名只需要在一个域上运行也是如此。 It would also mean that a focused input field would briefly flash when the extension loads, which could be distracting. 这也意味着扩展加载时,聚焦的输入字段将短暂闪烁,这可能会分散注意力。

You could use this: 您可以使用此:

function elemPropVal(elem, prop){
  var r = getComputedStyle(elem) || elem.currentStyle;
  return r[prop];
}
console.log(elemPropVal(document.getElementById('someId'), 'backgroundColor'));

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

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