index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * WebSocket测试
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. url: 'ws://121.40.165.18:8800',
  8. msg: '',
  9. connBtn: false,
  10. closeBtn: true,
  11. results: [],
  12. websock: null,
  13. },
  14. watch: {},
  15. mounted: function () {
  16. this.$refs.url.focus();
  17. this.loadPatchHotfix();
  18. },
  19. destroyed() {
  20. this.websock.close() //离开之后断开websocket连接
  21. },
  22. methods: {
  23. loadPatchHotfix() {
  24. // 页面加载时自动获取并注入页面的补丁
  25. chrome.runtime.sendMessage({
  26. type: 'fh-dynamic-any-thing',
  27. thing: 'fh-get-tool-patch',
  28. toolName: 'websocket'
  29. }, patch => {
  30. if (patch) {
  31. if (patch.css) {
  32. const style = document.createElement('style');
  33. style.textContent = patch.css;
  34. document.head.appendChild(style);
  35. }
  36. if (patch.js) {
  37. try {
  38. if (window.evalCore && window.evalCore.getEvalInstance) {
  39. window.evalCore.getEvalInstance(window)(patch.js);
  40. }
  41. } catch (e) {
  42. console.error('websocket补丁JS执行失败', e);
  43. }
  44. }
  45. }
  46. });
  47. },
  48. initWebSocket() { //初始化weosocket
  49. this.results.push(this.now() + "连接到:" + this.url);
  50. console.log(this.now() + "连接到:" + this.url)
  51. this.websock = new WebSocket(this.url);
  52. this.websock.onmessage = this.websocketOnMessage;
  53. this.websock.onopen = this.websocketOnOpen;
  54. this.websock.onerror = this.websocketOnError;
  55. this.websock.onclose = this.websocketOnClose;
  56. },
  57. websocketSend() {//数据发送
  58. this.websock.send(this.msg);
  59. this.results.push(this.now() + "发送:" + this.msg);
  60. console.log(this.now() + "发送:" + this.msg)
  61. this.msg = '';
  62. },
  63. websocketClose() {//数据发送
  64. this.results.push(this.now() + "关闭连接");
  65. console.log(this.now() + "关闭连接")
  66. this.websock.close();
  67. },
  68. cleanupMsg() {
  69. this.results = [];
  70. },
  71. websocketOnOpen(e) { //连接建立之后执行send方法发送数据
  72. this.results.push(this.now() + "连接成功");
  73. console.log(this.now() + "连接成功!", e)
  74. this.connBtn = true
  75. this.closeBtn = false
  76. },
  77. websocketOnError(e) {//连接建立失败重连
  78. this.results.push(this.now() + "连接失败,请检查连接地址是否正确或服务器是否正常");
  79. console.log(this.now() + "连接失败,请检查连接地址是否正确或服务器是否正常!", e)
  80. this.connBtn = false
  81. this.closeBtn = true
  82. },
  83. websocketOnMessage(msg) { //数据接收
  84. this.results.push(this.now() + "收到消息:" + msg.data);
  85. console.log(this.now() + "收到消息:", msg)
  86. },
  87. websocketOnClose(e) { //关闭
  88. console.log(this.now() + "连接断开!", e)
  89. this.connBtn = false
  90. this.closeBtn = true
  91. },
  92. now() {
  93. let date = new Date();
  94. return f0(date.getHours()) + ":" + f0(date.getMinutes()) + ":" + f0(date.getSeconds()) + " - ";
  95. },
  96. openDonateModal: function(event) {
  97. event.preventDefault();
  98. event.stopPropagation();
  99. chrome.runtime.sendMessage({
  100. type: 'fh-dynamic-any-thing',
  101. thing: 'open-donate-modal',
  102. params: { toolName: 'websocket' }
  103. });
  104. },
  105. openOptionsPage: function(event) {
  106. event.preventDefault();
  107. event.stopPropagation();
  108. chrome.runtime.openOptionsPage();
  109. }
  110. }
  111. });
  112. function f0(i) {
  113. return (i < 10 ? '0' : '') + i
  114. }