index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * FeHelper Timestamp Tools
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. txtNowS: Math.round((new Date()).getTime() / 1000),
  8. txtNowMs: (new Date()).getTime(),
  9. txtNowDate: (new Date()).toLocaleString(),
  10. txtSrcStamp: '',
  11. txtDesDate: '',
  12. txtLocale: '',
  13. txtDesStamp: '',
  14. secFrom: 's',
  15. secTo: 's',
  16. worldTime: {},
  17. curGMT: (new Date()).getTimezoneOffset() / 60 * -1
  18. },
  19. mounted: function () {
  20. this.startTimestamp();
  21. this.loadPatchHotfix();
  22. },
  23. methods: {
  24. startTimestamp: function () {
  25. let formatter = 'yyyy-MM-dd HH:mm:ss';
  26. window.intervalId = window.setInterval(() => {
  27. let localDate = new Date();
  28. let gmtTime = new Date(localDate.getTime() + localDate.getTimezoneOffset() * 60000);
  29. let nowDate = new Date(gmtTime.getTime() + this.curGMT * 60 * 60000);
  30. this.txtNowDate = nowDate.format(formatter);
  31. this.txtNowS = Math.round(nowDate.getTime() / 1000);
  32. this.txtNowMs = nowDate.getTime();
  33. this.worldTime['local'] = this.txtNowDate;
  34. this.worldTime['gmt'] = gmtTime.format(formatter);
  35. for (let offset = -12; offset <= 12; offset++) {
  36. this.worldTime[offset > 0 ? ('+' + offset) : offset] = new Date(gmtTime.getTime() + offset * 60 * 60000).format(formatter);
  37. }
  38. }, 1000);
  39. },
  40. unixToggle: function () {
  41. window.toggleModel = window.toggleModel || 0;
  42. if (window.toggleModel) {
  43. this.$refs.btnToggle.value = '暂停';
  44. window.toggleModel = 0;
  45. this.startTimestamp();
  46. } else {
  47. this.$refs.btnToggle.value = '开始';
  48. window.toggleModel = 1;
  49. window.clearInterval(window.intervalId);
  50. }
  51. },
  52. stampToLocale: function () {
  53. if (this.txtSrcStamp.length === 0) {
  54. alert('请先填写你需要转换的Unix时间戳');
  55. return;
  56. }
  57. if (!parseInt(this.txtSrcStamp, 10)) {
  58. alert('请输入合法的Unix时间戳');
  59. return;
  60. }
  61. let base = this.secFrom === 's' ? 1000 : 1;
  62. let format = 'yyyy-MM-dd HH:mm:ss' + (this.secFrom === 's' ? '' : '.SSS');
  63. this.txtDesDate = (new Date(parseInt(this.txtSrcStamp, 10) * base + ((new Date()).getTimezoneOffset() + this.curGMT * 60) * 60000)).format(format);
  64. },
  65. localeToStamp: function () {
  66. if (this.txtLocale && !/\s\d+:\d+:\d+/.test(this.txtLocale)) {
  67. this.txtLocale += ' 00:00:00';
  68. }
  69. let locale = (new Date(Date.parse(this.txtLocale) - ((new Date()).getTimezoneOffset() + this.curGMT * 60) * 60000)).getTime();
  70. if (isNaN(locale)) {
  71. alert('请输入合法的时间格式,如:2014-04-01 10:01:01,或:2014-01-01');
  72. }
  73. let base = this.secTo === 's' ? 1000 : 1;
  74. this.txtDesStamp = Math.round(locale / base);
  75. },
  76. copyToClipboard(text) {
  77. if (!text || !(text || '').trim().length) return;
  78. let input = document.createElement('textarea');
  79. input.style.position = 'fixed';
  80. input.style.opacity = 0;
  81. input.value = text;
  82. document.body.appendChild(input);
  83. input.select();
  84. document.execCommand('Copy');
  85. document.body.removeChild(input);
  86. this.toast('已复制到剪贴板,随处粘贴可用:[ ' + text + ' ]');
  87. },
  88. toast(content) {
  89. window.clearTimeout(window.feHelperAlertMsgTid);
  90. let elAlertMsg = document.querySelector("#fehelper_alertmsg");
  91. if (!elAlertMsg) {
  92. let elWrapper = document.createElement('div');
  93. elWrapper.innerHTML = '<div id="fehelper_alertmsg">' + content + '</div>';
  94. elAlertMsg = elWrapper.childNodes[0];
  95. document.body.appendChild(elAlertMsg);
  96. } else {
  97. elAlertMsg.innerHTML = content;
  98. elAlertMsg.style.display = 'block';
  99. }
  100. window.feHelperAlertMsgTid = window.setTimeout(function () {
  101. elAlertMsg.style.display = 'none';
  102. }, 3000);
  103. },
  104. loadPatchHotfix() {
  105. // 页面加载时自动获取并注入页面的补丁
  106. chrome.runtime.sendMessage({
  107. type: 'fh-dynamic-any-thing',
  108. thing: 'fh-get-tool-patch',
  109. toolName: 'timestamp'
  110. }, patch => {
  111. if (patch) {
  112. if (patch.css) {
  113. const style = document.createElement('style');
  114. style.textContent = patch.css;
  115. document.head.appendChild(style);
  116. }
  117. if (patch.js) {
  118. try {
  119. if (window.evalCore && window.evalCore.getEvalInstance) {
  120. window.evalCore.getEvalInstance(window)(patch.js);
  121. }
  122. } catch (e) {
  123. console.error('timestamp补丁JS执行失败', e);
  124. }
  125. }
  126. }
  127. });
  128. },
  129. }
  130. });