content-script.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /**
  2. * FeHelper Page Color Picker Tools
  3. */
  4. window.colorpickerContentScript = function () {
  5. let FeHelper = window.FeHelper || {};
  6. FeHelper.elemTool = {
  7. elm: function (nodeType, attributes, addchilds, appnedTo) {
  8. var ne = document.createElement(nodeType), i, l;
  9. if (attributes) {
  10. if (attributes.event || attributes.events) {
  11. var lev = attributes.event || attributes.events;
  12. if (typeof(lev[0]) == 'string') ne.addEventListener(lev[0], lev[1], lev[2]);
  13. else if (lev.length)
  14. for (i = 0, l = lev.length; i < l; i++)
  15. ne.addEventListener(lev[i][0], lev[i][1], lev[i][2]);
  16. }
  17. }
  18. for (i in attributes) {
  19. if (i.substring(0, 5) == 'event') {
  20. //handled earlier
  21. } else if (i == 'checked' || i == 'selected') {
  22. if (attributes[i]) ne.setAttribute(i, i);
  23. } else ne.setAttribute(i, attributes[i]);
  24. }
  25. if (addchilds) {
  26. for (i = 0, l = addchilds.length; i < l; i++) {
  27. if (addchilds[i]) ne.appendChild(addchilds[i]);//you probably forgot a comma when calling the function
  28. }
  29. }
  30. if (appnedTo) {
  31. this.insertNode(ne, appnedTo);
  32. }
  33. return ne;
  34. },
  35. /*elemTool.txt creates text nodes, does not support HTML entiteis */
  36. txt: function (textContent) {
  37. return document.createTextNode(textContent);
  38. },
  39. /*elemTool.ent creates text nodes that may or may not contain HTML entities. From a
  40. single entity to many entities interspersed with text are all supported by this */
  41. ent: function (textContent) {
  42. return document.createTextNode(this.unescapeHtml(textContent));
  43. },
  44. /*elemTool.paragraphs creates an array of nodes that may or may not contain HTML entities.*/
  45. paragraphs: function (textContent) {
  46. var textPieces = textContent.split("\n");
  47. var elmArray = [];
  48. for (var i = 0, l = textPieces.length; i < l; i++) {
  49. elmArray.push(elemTool.elm('p', {}, [elemTool.ent(textPieces[i])]));
  50. }
  51. return elmArray;
  52. },
  53. insertNode: function (newNode, parentElem, optionalInsertBefore) {
  54. if (!parentElem) parentElem = document.body;
  55. if (optionalInsertBefore && optionalInsertBefore.parentNode == parentElem) {
  56. parentElem.insertBefore(newNode, optionalInsertBefore);
  57. } else {
  58. parentElem.appendChild(newNode);
  59. }
  60. },
  61. insertNodes: function (newNodes, parentElem, optionalInsertBefore) {
  62. if (typeof(newNodes) != 'array')
  63. this.insertNode(newNodes, parentElem, optionalInsertBefore);
  64. else {
  65. for (var i = 0, l = newNodes.length; i < l; i++) {
  66. this.insertNode(newNodes[i], parentElem, optionalInsertBefore, true);
  67. }
  68. }
  69. },
  70. empty: function (node) {
  71. while (node.lastChild) node.removeChild(node.lastChild);
  72. },
  73. unescapeHtml: function (str) { //trick used to make HTMLentiites work inside textNodes
  74. if (str.length < 1) return str;
  75. var temp = document.createElement("div");
  76. str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
  77. temp.innerHTML = str;
  78. var result = temp.childNodes[0].nodeValue;
  79. this.empty(temp);
  80. return result;
  81. }
  82. };
  83. /**
  84. * 页面取色器
  85. */
  86. FeHelper.ColorPicker = (function () {
  87. if (!(document.documentElement instanceof HTMLElement)) {
  88. return;
  89. }
  90. var elmid1 = 'fehelper-colorpicker-box', elmid2 = 'fehelper-colorpicker-result';
  91. function _ge(n) {
  92. return document.getElementById(n);
  93. }
  94. var n = false, c = false, hex = 'F00BAF', lasthex = '', rgb = null;
  95. var hsv = null;
  96. var ex = 0, ey = 0, isEnabled = false, isLocked = false, hexIsLowerCase = false, borderValue = '1px solid #666',
  97. blankgif = '';
  98. var isUpdating = false, lastTimeout = 0, lx = 0, ly = 0;
  99. var cvs = document.createElement('canvas');
  100. var ctx = cvs.getContext('2d'), x_cvs_scale = 1, y_cvs_scale = 1;
  101. function RGBtoHex(R, G, B) {
  102. return applyHexCase(toHex(R) + toHex(G) + toHex(B))
  103. }
  104. function applyHexCase(hex) {
  105. return hexIsLowerCase ? hex.toLowerCase() : hex;
  106. }
  107. function toHex(N) {//http://www.javascripter.net/faq/rgbtohex.htm
  108. if (N == null) return "00";
  109. N = parseInt(N);
  110. if (N == 0 || isNaN(N)) return "00";
  111. N = Math.max(0, N);
  112. N = Math.min(N, 255);
  113. N = Math.round(N);
  114. return "0123456789ABCDEF".charAt((N - N % 16) / 16) + "0123456789ABCDEF".charAt(N % 16);
  115. }
  116. function rgb2hsl(r, g, b) {//http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
  117. r /= 255, g /= 255, b /= 255;
  118. var max = Math.max(r, g, b), min = Math.min(r, g, b);
  119. var h, s, l = (max + min) / 2;
  120. if (max == min) {
  121. h = s = 0; // achromatic
  122. } else {
  123. var d = max - min;
  124. s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
  125. switch (max) {
  126. case r:
  127. h = (g - b) / d + (g < b ? 6 : 0);
  128. break;
  129. case g:
  130. h = (b - r) / d + 2;
  131. break;
  132. case b:
  133. h = (r - g) / d + 4;
  134. break;
  135. }
  136. h /= 6;
  137. }
  138. return {
  139. h: Math.round(h * 360),
  140. s: Math.round(s * 100),
  141. v: Math.round(l * 100)
  142. };
  143. }
  144. function emptyNode(node) {
  145. while (node.lastChild) node.removeChild(node.lastChild);
  146. }
  147. function snapshotLoaded() {
  148. c.style.height = 'auto';
  149. c.style.width = (innerWidth) + 'px';
  150. x_cvs_scale = c.naturalWidth / innerWidth;
  151. y_cvs_scale = c.naturalHeight / innerHeight;
  152. cvs.width = c.naturalWidth;
  153. cvs.height = c.naturalHeight;
  154. ctx.drawImage(c, 0, 0);
  155. setTimeout(function () {
  156. isMakingNew = false;
  157. c.style.visibility = "visible";
  158. n.style.visibility = "visible";
  159. document.body.style.cursor = 'url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACEAAAAhCAYAAABX5MJvAAAA8ElEQVRYCe1WAQ6DIAwsm//e9rP9jOU2z0CpyWhlcQkkpoCld15rVcQ5cs5ZRLZrXbuiXVynDj40SVDQkBKfshChZdBe6yaRUkolmF6X934xxxsSGm4lQqjq8CRBQa6cBCwK9Bk4LzMdVO8USizW129k47HwFtX43i0YjiOIkIDGRGV/2/EezKGydxHBZY2btWntbT8mIMRhOe7s7RFo3IvYFSZqonEekQqAIO5KpMJETVRfw+rugIWFd4pXdJI4MtttZXdGn+mgYP+vBBoPnoaWT9Zr3UpoYL3uIeImARA2W9oe4NI3RKIMFJlPElTvBRTxXFcOwvQSAAAAAElFTkSuQmCC) 16 16,crosshair';
  160. updateColorPreview();
  161. }, 255);
  162. }
  163. function setPixelPreview(pix, hxe, lhex) {
  164. if (isLocked) return;
  165. var wid = 75, padr = 32;
  166. wid = 150;
  167. hex = hxe ? hxe : hex;
  168. if (!_ge('fehelper-colorpicker-cpimprev') || (rgb && !_ge('cprgbvl'))) {
  169. emptyNode(n);
  170. FeHelper.elemTool.elm('div', {}, [
  171. FeHelper.elemTool.elm('img', {
  172. id: 'fehelper-colorpicker-cpimprev',
  173. height: wid,
  174. width: wid,
  175. src: pix,
  176. style: 'margin:0px;padding:0px;margin:0px;'
  177. }),
  178. FeHelper.elemTool.elm('br'),
  179. FeHelper.elemTool.elm('input', {
  180. type: 'text',
  181. size: 7,
  182. style: 'width:60px;height:20px;line-height:20px;font-size:10pt;border:' + borderValue,
  183. id: 'fehelper-colorpicker-cphexvl',
  184. value: '#' + hex,
  185. event: ['mouseover', selectTargElm]
  186. })
  187. ], n)
  188. keepOnScreen();
  189. } else {
  190. _ge('fehelper-colorpicker-cpimprev').src = pix;
  191. _ge('fehelper-colorpicker-cpimprev').width = wid;
  192. _ge('fehelper-colorpicker-cpimprev').height = wid;
  193. _ge('fehelper-colorpicker-cphexvl').value = hex;
  194. n.style.backgroundColor = '#' + hex;
  195. }
  196. }
  197. function setCurColor(r) {
  198. if (!n) return;
  199. hex = r.hex ? r.hex : hex;
  200. n.style.backgroundColor = '#' + hex;
  201. if (isLocked) setDisplay();
  202. }
  203. function selectTargElm(ev) {
  204. ev.target.select();
  205. }
  206. function setDisplay() {//FeHelper.elemTool.elm
  207. emptyNode(n);
  208. FeHelper.elemTool.elm('div', {}, [
  209. FeHelper.elemTool.elm('input', {
  210. type: 'text',
  211. size: 7,
  212. style: 'width:80px;height:20px;line-height:20px;font-size:10pt;border:' + borderValue,
  213. id: 'fehelper-colorpicker-cphexvl',
  214. value: '#' + hex,
  215. event: ['mouseover', selectTargElm]
  216. }),
  217. FeHelper.elemTool.elm('img', {
  218. style: 'width:20px;height:20px;position:absolute;top:-10px;right:-10px;cursor:pointer;',
  219. src: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAACwklEQVQ4Ea1US0tbQRg9kwmp8YXBi7hKyMZAcaFuFN26daFEUg1BEF24FQr9D/0BIqhbDe0/0I0LceNCQSIVIcZdLkrER2LUm6+caeZiEmkp7cDlMnfOOfM9zneB/7zUn/REJAEgXsfllVI/fscJvncoIhrAZwArrutGb25uoJRCb28vROQKwBqAr0opr5nfEqGIpGq12s7h4SH29vZQKpUMR2uN9vZ29PX1YWJiAsPDw7zkk1Iq+1a0QVBEkuVy+dvW1hYuLi58HKMLBoPo6uoyb+6j0ShmZmYQDodnlVLfLdgXFJGQ53nVjY0NnJ+fo1arMT2Do0BbWxs6OjoQCASMqOd5iMViSKVS0Fp/UEo9ExywygBWj4+PUSwWTWpDQ0MYHx/3jylE4cHBQSwuLiIUCqFQKODk5ISYVQt8K7jCujE1RtPf349kMomRkRGLRSKRwNTUlMng+dkEBHLYPAsyXRaRj9fX19H7+3sTBQ+Pjo5Miul0Gg8PD+aS6elp831/f9/ycXd3B3KpoZTKWdvEb29vfRBT4yKR0S4vL5uLTk9Psbu7619qCeQ6jkOv5t6mbM/Nm6J8WFfWj4sXsFG2WQ2E+sYK5nt6elrOHcfB/Py8sdDl5SUWFhZokxbROjdPASPI3B3Hueru7vZFCZqbm2N9sL29jc3NTVMv1pQdtpHSm+RSwxesq6yNjY35grQMm5TNZvHy8oKnpyesr6/Tc6bz3L++vmJ0dJQcjqJZLcamAP1F+7B2lUoFj4+PRpBRdXZ2mm9kDwwMYGlp6X1j0+la61lag2PF26vVqpkYToVdtBD38XgcmUyGYhy9X6YE4EdoCfw5eJ63wwk4ODiA67ool8tGhJhIJILJyUmwPFrrlp+D1Wl4c65F5IuIFFzXlbOzM8nlclIsFoXf6mehBlJ90xJhM+hvf7DN/H/e/wRZ4k9klRmUggAAAABJRU5ErkJggg==',
  220. alt: 'Close',
  221. title: '[esc]键可直接关闭',
  222. id: 'fehelper-colorpicker-exitbtn',
  223. event: ['click', dissableColorPickerFromHere, true]
  224. })
  225. ], n);
  226. if (_ge('fehelper-colorpicker-cphexvl')) _ge('fehelper-colorpicker-cphexvl').select();
  227. keepOnScreen();
  228. }
  229. function picked() {
  230. if (isLocked) {
  231. lasthex = hex;
  232. isLocked = false;
  233. emptyNode(n);
  234. } else {
  235. isLocked = true;
  236. setDisplay();
  237. }
  238. }
  239. function dissableColorPickerFromHere() {
  240. setTimeout(disableColorPicker, 500)
  241. }
  242. function disableColorPicker() {
  243. isEnabled = false, isLocked = false;
  244. document.removeEventListener('mousemove', mmf);
  245. removeEventListener('scroll', ssf);
  246. removeEventListener('resize', ssf);
  247. removeEventListener('keyup', wk);
  248. removeExistingNodes();
  249. clearTimeout(lastNewTimeout);
  250. }
  251. function removeExistingNodes() {
  252. if (document.body) {
  253. c = _ge(elmid1), n = _ge(elmid2);
  254. if (c) document.body.removeChild(c);
  255. if (n) document.body.removeChild(n);
  256. c = false, n = false;
  257. document.body.style.cursor = '';
  258. }
  259. }
  260. function wk(ev) {
  261. if (!isEnabled) return;
  262. if (ev.keyCode == 27) {
  263. dissableColorPickerFromHere();
  264. } else if (ev.keyCode == 82 || ev.keyCode == 74) {//r or j refresh
  265. ssf();
  266. } else if (ev.keyCode == 13) {
  267. picked();
  268. }
  269. }
  270. function mmf(ev) {
  271. if (!isEnabled) return;
  272. if (!isLocked) {
  273. lx = (ev.pageX - pageXOffset), ly = (ev.pageY - pageYOffset);
  274. ex = Math.round(lx * x_cvs_scale),
  275. ey = Math.round(ly * y_cvs_scale);
  276. updateColorPreview();
  277. }
  278. }
  279. function ssf(ev) {
  280. if (!isEnabled) return;
  281. n.style.visibility = "hidden";
  282. c.style.visibility = "hidden";//redundent?
  283. clearTimeout(lastNewTimeout);
  284. lastNewTimeout = setTimeout(function () {
  285. newImage()//some delay required OR it won't update
  286. }, 250);
  287. }
  288. function initialInit() {
  289. removeExistingNodes();
  290. c = FeHelper.elemTool.elm('img', {
  291. id: elmid1,
  292. src: blankgif,
  293. style: 'position:fixed;max-width:none!important;max-height:none!important;top:0px;left:0px;margin:0px;padding:0px;overflow:hidden;z-index:2147483646;',
  294. events: [['click', picked, true], ['load', snapshotLoaded]]
  295. }, [], document.body);
  296. n = FeHelper.elemTool.elm('div', {
  297. id: elmid2,
  298. style: 'position:fixed;min-width:30px;max-width:300px;box-shadow:2px 2px 2px #666;border:' + borderValue + ';border-radius:5px;z-index:2147483646;cursor:default;padding:10px;text-align:center;'
  299. }, [], document.body);
  300. document.addEventListener('mousemove', mmf);
  301. addEventListener('keyup', wk);
  302. addEventListener('scroll', ssf);
  303. addEventListener('resize', ssf);
  304. initializeCanvas();
  305. remainingInit();
  306. }
  307. function enableColorPicker() {
  308. disableColorPicker();
  309. if (!n) {
  310. initialInit();
  311. return false;
  312. }
  313. return remainingInit();
  314. }
  315. function remainingInit() {
  316. if (!isEnabled) {
  317. n.style.visibility = "hidden";
  318. c.style.visibility = "hidden";
  319. if (isLocked) picked();//unlocks for next pick
  320. document.body.style.cursor = 'url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACEAAAAhCAYAAABX5MJvAAAA8ElEQVRYCe1WAQ6DIAwsm//e9rP9jOU2z0CpyWhlcQkkpoCld15rVcQ5cs5ZRLZrXbuiXVynDj40SVDQkBKfshChZdBe6yaRUkolmF6X934xxxsSGm4lQqjq8CRBQa6cBCwK9Bk4LzMdVO8USizW129k47HwFtX43i0YjiOIkIDGRGV/2/EezKGydxHBZY2btWntbT8mIMRhOe7s7RFo3IvYFSZqonEekQqAIO5KpMJETVRfw+rugIWFd4pXdJI4MtttZXdGn+mgYP+vBBoPnoaWT9Zr3UpoYL3uIeImARA2W9oe4NI3RKIMFJlPElTvBRTxXFcOwvQSAAAAAElFTkSuQmCC) 16 16,crosshair';
  321. isEnabled = true;
  322. setTimeout(newImage, 1);
  323. return false;
  324. }
  325. return true;
  326. }
  327. function keepOnScreen() {
  328. if (!n) return;
  329. n.style.top = (ly + 8) + "px";
  330. n.style.left = (lx + 8) + "px";
  331. if (n.clientWidth + n.offsetLeft + 24 > innerWidth) {
  332. n.style.left = (lx - 8 - n.clientWidth) + "px";
  333. }
  334. if (n.clientHeight + n.offsetTop + 24 > innerHeight) {
  335. n.style.top = (ly - 8 - n.clientHeight) + "px";
  336. }
  337. }
  338. function updateColorPreview(ev) {
  339. if (!isEnabled) return;
  340. keepOnScreen();
  341. var data = ctx.getImageData(ex, ey, 1, 1).data;
  342. hsv = rgb2hsl(data[0], data[1], data[2]);
  343. rgb = {r: data[0], g: data[1], b: data[2]};
  344. setCurColor({hex: RGBtoHex(data[0], data[1], data[2])});
  345. handleRendering();
  346. }
  347. var isMakingNew = false, lastNewTimeout = 0;
  348. function newImage() {
  349. if (!isEnabled) return;
  350. if (isMakingNew) {
  351. clearTimeout(lastNewTimeout);
  352. lastNewTimeout = setTimeout(function () {
  353. newImage()
  354. }, 255);
  355. return;
  356. }
  357. document.body.style.cursor = 'wait';
  358. isMakingNew = true;
  359. n.style.visibility = "hidden";
  360. c.style.visibility = "hidden";
  361. c.src = blankgif;
  362. var x = innerWidth, y = innerHeight;
  363. c.style.width = x + 'px';
  364. c.style.height = y + 'px';
  365. setTimeout(function () {
  366. try {
  367. chrome.runtime.sendMessage({
  368. type: 'fh-dynamic-any-thing',
  369. thing:'color-picker-capture',
  370. params: {
  371. url: location.href
  372. }
  373. });
  374. } catch (e) {
  375. console.log('有错误发生,可提交此反馈到官网!', e);
  376. }
  377. }, 255);
  378. }
  379. var lastPreviewURI;
  380. var icvs = 0, totalWidth = 150;//750
  381. function handleRendering(quick) {
  382. var x = ex, y = ey;
  383. if (isMakingNew) {
  384. isUpdating = false;
  385. return;
  386. }
  387. var startPoint = Math.floor(totalWidth * 0.5);
  388. var ox = Math.round(x), oy = Math.round(y);
  389. if (quick) {
  390. var ictx = getMain2dContext();
  391. ictx.scale(2, 2);
  392. ictx.drawImage(cvs, -ox + (startPoint * 0.5), -oy + (startPoint * 0.5));
  393. ictx.scale(0.5, 0.5);
  394. ictx.fillStyle = "rgba(0,0,0,0.3)";//croshair
  395. ictx.fillRect(startPoint, 0, 1, totalWidth);
  396. ictx.fillRect(0, startPoint, totalWidth, 1);
  397. } else {
  398. var ictx = getMain2dContext();
  399. ictx.drawImage(cvs, -ox + (startPoint), -oy + (startPoint));
  400. var smi, spi, mp = 15 - 0;
  401. //xx,yy
  402. for (var i = 0; i < startPoint; i += 2) {
  403. smi = startPoint - i;
  404. spi = startPoint + i;
  405. //drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) //CANVAS
  406. ictx.drawImage(icvs, spi, 0, smi, totalWidth,//total width really??
  407. spi + 1, 0, smi, totalWidth);
  408. ictx.drawImage(icvs, 0, 0, smi + 1, totalWidth,
  409. -1, 0, smi + 1, totalWidth);
  410. ictx.drawImage(icvs, 0, spi, totalWidth, smi,
  411. 0, spi + 1, totalWidth, smi);
  412. ictx.drawImage(icvs, 0, 0, totalWidth, smi + 1,
  413. 0, -1, totalWidth, smi + 1);
  414. if (i == 0) {
  415. var dat = ictx.getImageData(startPoint, startPoint, 1, 1).data;//notarget
  416. var d = dat[0] + dat[1] + dat[2];
  417. if (d > 192) ictx.fillStyle = "rgba(30,30,30,0.8)";
  418. else ictx.fillStyle = "rgba(225,225,225,0.8)";
  419. } else ictx.fillStyle = "rgba(255,255,255,0.4)";
  420. for (var c = 0; c < mp; c++) {
  421. if (++i >= startPoint) break;
  422. smi = startPoint - i;
  423. spi = startPoint + i;
  424. ictx.drawImage(icvs, spi, 0, smi, totalWidth,
  425. spi + 1, 0, smi, totalWidth);
  426. ictx.drawImage(icvs, 0, 0, smi + 1, totalWidth,
  427. -1, 0, smi + 1, totalWidth);
  428. ictx.drawImage(icvs, 0, spi, totalWidth, smi,
  429. 0, spi + 1, totalWidth, smi);
  430. ictx.drawImage(icvs, 0, 0, totalWidth, smi + 1,
  431. 0, -1, totalWidth, smi + 1);
  432. }
  433. mp--;
  434. if (mp < 1) mp = 1;
  435. ictx.fillRect(spi + 1, 0, 1, totalWidth);
  436. ictx.fillRect(smi - 1, 0, 1, totalWidth);
  437. ictx.fillRect(0, spi + 1, totalWidth, 1);
  438. ictx.fillRect(0, smi - 1, totalWidth, 1);
  439. }
  440. }
  441. lastPreviewURI = icvs.toDataURL();//the last one, large size, is cached for revisiting the menu
  442. var browseIconWidth = (devicePixelRatio > 1 ? 38 : 19);
  443. var browseIconHalfWidth = Math.floor(browseIconWidth * 0.5);
  444. var tmpCvs = document.createElement('canvas');
  445. tmpCvs.width = browseIconWidth, tmpCvs.height = browseIconWidth;
  446. var tctx = tmpCvs.getContext("2d");
  447. tctx.drawImage(icvs, startPoint - browseIconHalfWidth, startPoint - browseIconHalfWidth, browseIconWidth, browseIconWidth, 0, 0, browseIconWidth, browseIconWidth);
  448. var pathData = {};
  449. pathData[browseIconWidth] = tmpCvs.toDataURL();
  450. setPixelPreview(lastPreviewURI, hex, lasthex);
  451. isUpdating = false;
  452. }
  453. function getMain2dContext() {
  454. var context = icvs.getContext("2d");
  455. if (context) return context;
  456. else {
  457. initializeCanvas();
  458. return icvs.getContext("2d");
  459. }
  460. }
  461. function initializeCanvas() {
  462. icvs = document.createElement('canvas');//icon canvas
  463. icvs.width = totalWidth;
  464. icvs.height = totalWidth;
  465. }
  466. return function (request) {
  467. if (request.setPickerImage) {
  468. c.src = request.pickerImage;
  469. } else {
  470. enableColorPicker();
  471. picked();
  472. }
  473. };
  474. })();
  475. // 给background page直接调用的
  476. window.colorpickerNoPage = function (request) {
  477. FeHelper.ColorPicker(request)
  478. };
  479. };