helper.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import browser from 'webextension-polyfill';
  2. export async function getFrames(tabId) {
  3. try {
  4. const frames = await browser.webNavigation.getAllFrames({ tabId });
  5. const framesObj = frames.reduce((acc, { frameId, url }) => {
  6. const key = url === 'about:blank' ? '' : url;
  7. acc[key] = frameId;
  8. return acc;
  9. }, {});
  10. return framesObj;
  11. } catch (error) {
  12. console.error(error);
  13. return {};
  14. }
  15. }
  16. export function sendDebugCommand(tabId, method, params = {}) {
  17. return new Promise((resolve) => {
  18. chrome.debugger.sendCommand({ tabId }, method, params, resolve);
  19. });
  20. }
  21. export function attachDebugger(tabId, prevTab) {
  22. return new Promise((resolve) => {
  23. if (prevTab && tabId !== prevTab)
  24. chrome.debugger.detach({ tabId: prevTab });
  25. chrome.debugger.attach({ tabId }, '1.3', () => {
  26. chrome.debugger.sendCommand({ tabId }, 'Page.enable', resolve);
  27. });
  28. });
  29. }
  30. export function waitTabLoaded(tabId, ms = 10000) {
  31. return new Promise((resolve, reject) => {
  32. let timeout = null;
  33. const excludeErrors = ['net::ERR_BLOCKED_BY_CLIENT', 'net::ERR_ABORTED'];
  34. const onErrorOccurred = (details) => {
  35. if (
  36. details.tabId !== tabId ||
  37. details.frameId !== 0 ||
  38. !excludeErrors.includes(details.error)
  39. )
  40. return;
  41. browser.webNavigation.onErrorOccurred.removeListener(onErrorOccurred);
  42. reject(new Error(details.error));
  43. };
  44. if (ms > 0) {
  45. timeout = setTimeout(() => {
  46. browser.webNavigation.onErrorOccurred.removeListener(onErrorOccurred);
  47. reject(new Error('Timeout'));
  48. }, ms);
  49. }
  50. browser.webNavigation.onErrorOccurred.addListener(onErrorOccurred);
  51. const activeTabStatus = () => {
  52. browser.tabs.get(tabId).then((tab) => {
  53. if (!tab) {
  54. reject(new Error('no-tab'));
  55. return;
  56. }
  57. if (tab.status === 'loading') {
  58. setTimeout(() => {
  59. activeTabStatus();
  60. }, 1000);
  61. return;
  62. }
  63. clearTimeout(timeout);
  64. browser.webNavigation.onErrorOccurred.removeListener(onErrorOccurred);
  65. resolve();
  66. });
  67. };
  68. activeTabStatus();
  69. });
  70. }
  71. export function convertData(data, type) {
  72. if (type === 'any') return data;
  73. let result = data;
  74. switch (type) {
  75. case 'integer':
  76. result = typeof data !== 'number' ? +data?.replace(/\D+/g, '') : data;
  77. break;
  78. case 'boolean':
  79. result = Boolean(data);
  80. break;
  81. case 'array':
  82. result = Array.from(data);
  83. break;
  84. case 'string':
  85. result = String(data);
  86. break;
  87. default:
  88. }
  89. return result;
  90. }
  91. export function getBlockConnection(block, index = 1) {
  92. const blockId = block.outputs[`output_${index}`];
  93. return blockId;
  94. }