content-script.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * 栅格相关处理
  3. * @author zhaoxianlie
  4. */
  5. window.gridrulerContentScript = function () {
  6. // 创建栅格
  7. let _createGrid = function () {
  8. let box = jQuery('#fe-helper-box');
  9. if (box[0]) { //已经有栅格,则移除
  10. box.remove();
  11. }
  12. //没有栅格,则创建
  13. let $win = jQuery(window);
  14. let $body = jQuery('body');
  15. jQuery('<div id="fe-helper-box"></div>').appendTo('body').css({
  16. position: 'static'
  17. });
  18. jQuery('<div id="fe-helper-grid"></div>').appendTo('#fe-helper-box').css({
  19. width: $body.width(),
  20. height: Math.max($win.height(), $body.height())
  21. }).mousemove(function (e) {
  22. let pos = {};
  23. try {
  24. pos = document.getElementsByTagName('body')[0].getBoundingClientRect();
  25. } catch (err) {
  26. pos = {left: 0, top: 0};
  27. }
  28. //虚线框
  29. jQuery('#fe-helper-g-pos').show().css({
  30. width: e.pageX - pos.left,
  31. height: e.pageY
  32. });
  33. let _t = Math.min(e.pageY, jQuery(window).height() + $body.scrollTop() - 40);
  34. let _l = Math.min(e.pageX, jQuery(window).width() + $body.scrollLeft() - 200) + 5 - pos.left;
  35. //坐标tooltip
  36. jQuery('#fe-helper-gp-info').show().css({
  37. top: _t,
  38. left: _l
  39. }).html('top = ' + e.pageY + ' px ,left = ' + e.pageX + ' px');
  40. }).mouseout(function (e) {
  41. jQuery('#fe-helper-g-pos,#fe-helper-gp-info').hide();
  42. });
  43. jQuery('<div id="fe-helper-g-pos"></div><div id="fe-helper-gp-info"></div>').appendTo('#fe-helper-box');
  44. jQuery('<span id="fe-helper-btn-close-grid">关闭栅格层</span>')
  45. .appendTo('#fe-helper-box').click(function () {
  46. jQuery('#fe-helper-box').remove();
  47. });
  48. };
  49. // 绘制Ruler
  50. let _drawRuler = function () {
  51. let _t = 0, _h = 30, _w = 30;
  52. let $win = jQuery(window);
  53. let $page = jQuery('html');
  54. let elScroll = jQuery(document.scrollingElement || 'html');
  55. let $width = Math.max($win.width(), $page.width(), elScroll[0].scrollWidth);
  56. let $height = Math.max($win.height(), $page.height(), elScroll[0].scrollHeight);
  57. let rulerTop = jQuery('#fe-helper-ruler-top').width($width);
  58. let rulerLeft = jQuery('#fe-helper-ruler-left').height($height);
  59. if (!rulerTop.children().length || rulerTop.children().last().position().left < $width - 50) {
  60. rulerTop.html('');
  61. for (let i = 30; i <= $width; i += 10) {
  62. _t = (i % 50) ? 10 : 0;
  63. jQuery('<div class="h-line"></div>').appendTo('#fe-helper-ruler-top').css({
  64. left: i - 1,
  65. top: _t + 15,
  66. height: _h - _t - 5
  67. });
  68. if (_t === 0 && i !== 0) {
  69. jQuery('<div class="h-text">' + i + '</div>').appendTo('#fe-helper-ruler-top').css({
  70. left: i - (String(i).length * 4)
  71. });
  72. }
  73. }
  74. }
  75. if (!rulerLeft.children().length || rulerLeft.children().last().position().top < $height - 50) {
  76. rulerLeft.html('');
  77. for (let i = 0; i <= $height; i += 10) {
  78. _l = (i % 50) ? 10 : 0;
  79. jQuery('<div class="v-line"></div>').appendTo('#fe-helper-ruler-left').css({
  80. left: _l + 15,
  81. top: i - 1,
  82. width: _w - _l - 5
  83. });
  84. if (_l === 0) {
  85. jQuery('<div class="v-text">' + i + '</div>').appendTo('#fe-helper-ruler-left').css({
  86. top: i - (String(i).length * 4),
  87. left: i === 0 ? 5 : 0
  88. });
  89. }
  90. }
  91. }
  92. };
  93. // 创建页面标尺
  94. let _createPageRuler = function () {
  95. if (!jQuery('#fe-helper-box')[0]) {
  96. jQuery('<div id="fe-helper-box"></div>').appendTo('body');
  97. }
  98. jQuery('<div id="fe-helper-ruler-top"></div><div id="fe-helper-ruler-left"></div>').appendTo('#fe-helper-box');
  99. _drawRuler();
  100. };
  101. // 全局事件绑定
  102. let _bindEvent = function () {
  103. //为页面注册按键监听
  104. jQuery('body').keydown(function (e) {
  105. if (jQuery('#fe-helper-box')[0]) {
  106. if (e.which === 27) { //ESC
  107. jQuery('#fe-helper-box').remove();
  108. }
  109. }
  110. });
  111. //window.onresize
  112. jQuery(window).resize(function () {
  113. if (jQuery('#fe-helper-box')[0]) {
  114. let $win = jQuery(window);
  115. let $body = jQuery('body');
  116. jQuery('#fe-helper-grid').css({
  117. width: Math.max($win.width(), $body.width()),
  118. height: Math.max($win.height(), $body.height())
  119. });
  120. _drawRuler();
  121. }
  122. });
  123. //处理scroll的时候,标尺跟着移动
  124. jQuery(window).scroll(function (e) {
  125. if (jQuery('#fe-helper-box')[0]) {
  126. let elScroll = jQuery(document.scrollingElement || 'html');
  127. //水平标尺定位
  128. jQuery('#fe-helper-ruler-top').css('left', 0 - elScroll.scrollLeft());
  129. //垂直标尺
  130. jQuery('#fe-helper-ruler-left').css('top', 0 - elScroll.scrollTop());
  131. }
  132. });
  133. };
  134. let cssInjected = false;
  135. /**
  136. * 执行栅格系统检测
  137. */
  138. window.gridrulerNoPage = function (tabInfo) {
  139. // 提前注入css
  140. if(!cssInjected) {
  141. chrome.runtime.sendMessage({
  142. type: 'fh-dynamic-any-thing',
  143. thing:'inject-content-css',
  144. tool: 'grid-ruler'
  145. });
  146. }
  147. //创建栅格
  148. _createGrid();
  149. //创建页面标尺
  150. _createPageRuler();
  151. // 事件绑定
  152. _bindEvent();
  153. };
  154. };