content-script.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * 对整个页面增加多种滤镜选择,比如:增强对比度、反色等
  3. * @example PageGrayTool.init('0/1/2/3/4/5')
  4. */
  5. let PageGrayTool = (function () {
  6. let mode;
  7. let scheme = '4';
  8. let svgContent = `
  9. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  10. <defs>
  11. <filter x="0" y="0" width="99999" height="99999" id="_fh_page_modifier_off">
  12. <feComponentTransfer>
  13. <feFuncR type="table" tableValues="0 1"/>
  14. <feFuncG type="table" tableValues="0 1"/>
  15. <feFuncB type="table" tableValues="0 1"/>
  16. </feComponentTransfer>
  17. </filter>
  18. <filter x="0" y="0" width="99999" height="99999" id="_fh_page_modifier_highlight">
  19. <feComponentTransfer>
  20. <feFuncR type="gamma" exponent="3.0"/>
  21. <feFuncG type="gamma" exponent="3.0"/>
  22. <feFuncB type="gamma" exponent="3.0"/>
  23. </feComponentTransfer>
  24. </filter>
  25. <filter x="0" y="0" width="99999" height="99999" id="_fh_page_modifier_highlight_back">
  26. <feComponentTransfer>
  27. <feFuncR type="gamma" exponent="0.33"/>
  28. <feFuncG type="gamma" exponent="0.33"/>
  29. <feFuncB type="gamma" exponent="0.33"/>
  30. </feComponentTransfer>
  31. </filter>
  32. <filter x="0" y="0" width="99999" height="99999" id="_fh_page_modifier_grayscale">
  33. <feColorMatrix type="matrix" values="0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0 0 0 1 0"/>
  34. <feComponentTransfer>
  35. <feFuncR type="gamma" exponent="3"/>
  36. <feFuncG type="gamma" exponent="3"/>
  37. <feFuncB type="gamma" exponent="3"/>
  38. </feComponentTransfer>
  39. </filter>
  40. <filter x="0" y="0" width="99999" height="99999" id="_fh_page_modifier_grayscale_back">
  41. <feComponentTransfer>
  42. <feFuncR type="gamma" exponent="0.33"/>
  43. <feFuncG type="gamma" exponent="0.33"/>
  44. <feFuncB type="gamma" exponent="0.33"/>
  45. </feComponentTransfer>
  46. </filter>
  47. <filter x="0" y="0" width="99999" height="99999" id="_fh_page_modifier_invert">
  48. <feComponentTransfer>
  49. <feFuncR type="gamma" amplitude="-1" exponent="3" offset="1"/>
  50. <feFuncG type="gamma" amplitude="-1" exponent="3" offset="1"/>
  51. <feFuncB type="gamma" amplitude="-1" exponent="3" offset="1"/>
  52. </feComponentTransfer>
  53. </filter>
  54. <filter x="0" y="0" width="99999" height="99999" id="_fh_page_modifier_invert_back">
  55. <feComponentTransfer>
  56. <feFuncR type="table" tableValues="1 0"/>
  57. <feFuncG type="table" tableValues="1 0"/>
  58. <feFuncB type="table" tableValues="1 0"/>
  59. </feComponentTransfer>
  60. <feComponentTransfer>
  61. <feFuncR type="gamma" exponent="1.7"/>
  62. <feFuncG type="gamma" exponent="1.7"/>
  63. <feFuncB type="gamma" exponent="1.7"/>
  64. </feComponentTransfer>
  65. </filter>
  66. <filter x="0" y="0" width="99999" height="99999" id="_fh_page_modifier_invert_grayscale">
  67. <feColorMatrix type="matrix" values="0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0 0 0 1 0"/>
  68. <feComponentTransfer>
  69. <feFuncR type="gamma" amplitude="-1" exponent="3" offset="1"/>
  70. <feFuncG type="gamma" amplitude="-1" exponent="3" offset="1"/>
  71. <feFuncB type="gamma" amplitude="-1" exponent="3" offset="1"/>
  72. </feComponentTransfer>
  73. </filter>
  74. <filter x="0" y="0" width="99999" height="99999" id="_fh_page_modifier_yellow_on_black">
  75. <feComponentTransfer>
  76. <feFuncR type="gamma" amplitude="-1" exponent="3" offset="1"/>
  77. <feFuncG type="gamma" amplitude="-1" exponent="3" offset="1"/>
  78. <feFuncB type="gamma" amplitude="-1" exponent="3" offset="1"/>
  79. </feComponentTransfer>
  80. <feColorMatrix type="matrix" values="0.3 0.5 0.2 0 0 0.3 0.5 0.2 0 0 0 0 0 0 0 0 0 0 1 0"/>
  81. </filter>
  82. <filter x="0" y="0" width="99999" height="99999" id="_fh_page_modifier_yellow_on_black_back">
  83. <feComponentTransfer>
  84. <feFuncR type="table" tableValues="1 0"/>
  85. <feFuncG type="table" tableValues="1 0"/>
  86. <feFuncB type="table" tableValues="1 0"/>
  87. </feComponentTransfer>
  88. <feComponentTransfer>
  89. <feFuncR type="gamma" exponent="0.33"/>
  90. <feFuncG type="gamma" exponent="0.33"/>
  91. <feFuncB type="gamma" exponent="0.33"/>
  92. </feComponentTransfer>
  93. </filter>
  94. </defs>
  95. </svg>`;
  96. let cssTemplate = `
  97. html[hc="a0"] {
  98. -webkit-filter: url("#_fh_page_modifier_off");
  99. }
  100. html[hcx="0"] img[src*="jpg"],
  101. html[hcx="0"] img[src*="jpeg"],
  102. html[hcx="0"] svg image,
  103. html[hcx="0"] img.rg_i,
  104. html[hcx="0"] embed,
  105. html[hcx="0"] object,
  106. html[hcx="0"] video {
  107. -webkit-filter: url("#_fh_page_modifier_off");
  108. }
  109. html[hc="a1"] {
  110. -webkit-filter: url("#_fh_page_modifier_highlight");
  111. }
  112. html[hcx="1"] img[src*="jpg"],
  113. html[hcx="1"] img[src*="jpeg"],
  114. html[hcx="1"] img.rg_i,
  115. html[hcx="1"] svg image,
  116. html[hcx="1"] embed,
  117. html[hcx="1"] object,
  118. html[hcx="1"] video {
  119. -webkit-filter: url("#_fh_page_modifier_highlight_back");
  120. }
  121. html[hc="a2"] {
  122. -webkit-filter: url("#_fh_page_modifier_grayscale");
  123. }
  124. html[hcx="2"] img[src*="jpg"],
  125. html[hcx="2"] img[src*="jpeg"],
  126. html[hcx="2"] img.rg_i,
  127. html[hcx="2"] svg image,
  128. html[hcx="2"] embed,
  129. html[hcx="2"] object,
  130. html[hcx="2"] video {
  131. -webkit-filter: url("#_fh_page_modifier_grayscale_back");
  132. }
  133. html[hc="a3"] {
  134. -webkit-filter: url("#_fh_page_modifier_invert");
  135. }
  136. html[hcx="3"] img[src*="jpg"],
  137. html[hcx="3"] img[src*="jpeg"],
  138. html[hcx="3"] img.rg_i,
  139. html[hcx="3"] svg image,
  140. html[hcx="3"] embed,
  141. html[hcx="3"] object,
  142. html[hcx="3"] video {
  143. -webkit-filter: url("#_fh_page_modifier_invert_back");
  144. }
  145. html[hc="a4"] {
  146. -webkit-filter: url("#_fh_page_modifier_invert_grayscale");
  147. }
  148. html[hcx="4"] img[src*="jpg"],
  149. html[hcx="4"] img[src*="jpeg"],
  150. html[hcx="4"] img.rg_i,
  151. html[hcx="4"] svg image,
  152. html[hcx="4"] embed,
  153. html[hcx="4"] object,
  154. html[hcx="4"] video {
  155. -webkit-filter: url("#_fh_page_modifier_invert_back");
  156. }
  157. html[hc="a5"] {
  158. -webkit-filter: url("#_fh_page_modifier_yellow_on_black");
  159. }
  160. html[hcx="5"] img[src*="jpg"],
  161. html[hcx="5"] img[src*="jpeg"],
  162. html[hcx="5"] img.rg_i,
  163. html[hcx="5"] svg image,
  164. html[hcx="5"] embed,
  165. html[hcx="5"] object,
  166. html[hcx="5"] video {
  167. -webkit-filter: url("#_fh_page_modifier_yellow_on_black_back");
  168. }`;
  169. /**
  170. * Add the elements to the pgae that make high-contrast adjustments possible.
  171. */
  172. function addOrUpdateExtraElements() {
  173. let style = document.getElementById('hc_style');
  174. if (!style) {
  175. let baseUrl = window.location.href.replace(window.location.hash, '');
  176. let css = cssTemplate.replace(/#/g, baseUrl + '#');
  177. style = document.createElement('style');
  178. style.id = 'hc_style';
  179. style.setAttribute('type', 'text/css');
  180. style.innerHTML = css;
  181. document.head.appendChild(style);
  182. }
  183. let bg = document.getElementById('_fh_page_modifier_bkgnd');
  184. if (!bg) {
  185. bg = document.createElement('div');
  186. bg.id = '_fh_page_modifier_bkgnd';
  187. bg.style.position = 'fixed';
  188. bg.style.left = '0px';
  189. bg.style.top = '0px';
  190. bg.style.right = '0px';
  191. bg.style.bottom = '0px';
  192. bg.style.zIndex = -1999999999;
  193. document.body.appendChild(bg);
  194. }
  195. bg.style.display = 'block';
  196. bg.style.background = window.getComputedStyle(document.body).background;
  197. let c = (bg.style.backgroundColor || '').replace(/\s\s*/g, '');
  198. let match = /^rgba\(([\d]+),([\d]+),([\d]+),([\d]+|[\d]*.[\d]+)\)/i.exec(c);
  199. if (match && match[4] === '0') {
  200. bg.style.backgroundColor = '#fff';
  201. }
  202. let wrap = document.getElementById('_fh_page_modifier_svg_filters');
  203. if (wrap)
  204. return;
  205. wrap = document.createElement('span');
  206. wrap.id = '_fh_page_modifier_svg_filters';
  207. wrap.setAttribute('hidden', '');
  208. wrap.innerHTML = svgContent;
  209. document.body.appendChild(wrap);
  210. }
  211. function init(grayLevel) {
  212. if (window === window.top) {
  213. mode = 'a';
  214. } else {
  215. mode = 'b';
  216. }
  217. scheme = grayLevel || scheme;
  218. let html = document.documentElement;
  219. html.setAttribute('hc', mode + scheme);
  220. html.setAttribute('hcx', scheme);
  221. if (window === window.top) {
  222. window.scrollBy(0, 1);
  223. window.scrollBy(0, -1);
  224. }
  225. // Update again after a few seconds and again after load so that
  226. // the background isn't wrong for long.
  227. window.setTimeout(addOrUpdateExtraElements, 2000);
  228. addOrUpdateExtraElements();
  229. // Also update when the document body attributes change.
  230. let config = {attributes: true, childList: true, characterData: true};
  231. let observer = new MutationObserver(function (mutations) {
  232. addOrUpdateExtraElements();
  233. });
  234. observer.observe(document.body, config);
  235. }
  236. return {
  237. init: init
  238. };
  239. })();
  240. /**
  241. * 页面修改器
  242. * @param pageConfig
  243. * @constructor
  244. */
  245. let PageModify = function (pageConfig) {
  246. if (pageConfig && pageConfig.id && pageConfig.mPattern && !pageConfig.mDisabled) {
  247. let m = pageConfig.mPattern.match(/\/(.*)\/(.*)?/);
  248. // 如果正则匹配的话才生效
  249. if ((new RegExp(m[1], m[2] || '')).test(location.href)) {
  250. let el = document.createElement('script');
  251. el.type = 'text/javascript';
  252. el.textContent = pageConfig.mScript;
  253. document.body.appendChild(el);
  254. // 如果需要进行页面filter处理的话,直接处理
  255. pageConfig.mFilter && PageGrayTool.init(pageConfig.mFilter);
  256. // 自动刷新
  257. parseInt(pageConfig.mRefresh) && setTimeout(() => {
  258. location.reload(true);
  259. }, parseInt(pageConfig.mRefresh) * 1000);
  260. }
  261. }
  262. };
  263. /**
  264. * 获取当前页面modifier配置
  265. */
  266. let ModifyCurrentPage = function () {
  267. chrome.runtime.sendMessage({
  268. type: 'get_page_modifier_config',
  269. params: {
  270. url: location.href
  271. }
  272. }, function (pageConfig) {
  273. pageConfig && PageModify(pageConfig);
  274. });
  275. };
  276. ModifyCurrentPage();