helper.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. export function sendDebugCommand(tabId, method, params = {}) {
  2. return new Promise((resolve) => {
  3. chrome.debugger.sendCommand({ tabId }, method, params, resolve);
  4. });
  5. }
  6. export function attachDebugger(tabId, prevTab) {
  7. return new Promise((resolve) => {
  8. if (prevTab && tabId !== prevTab)
  9. chrome.debugger.detach({ tabId: prevTab });
  10. chrome.debugger.attach({ tabId }, '1.3', () => {
  11. chrome.debugger.sendCommand({ tabId }, 'Page.enable', resolve);
  12. });
  13. });
  14. }
  15. export function waitTabLoaded(tabId) {
  16. return new Promise((resolve, reject) => {
  17. const activeTabStatus = () => {
  18. chrome.tabs.get(tabId, (tab) => {
  19. if (!tab) {
  20. reject(new Error('no-tab'));
  21. return;
  22. }
  23. if (tab.status === 'loading') {
  24. setTimeout(() => {
  25. activeTabStatus();
  26. }, 500);
  27. return;
  28. }
  29. resolve();
  30. });
  31. };
  32. activeTabStatus();
  33. });
  34. }
  35. export function convertData(data, type) {
  36. let result = data;
  37. switch (type) {
  38. case 'integer':
  39. result = typeof data !== 'number' ? +data?.replace(/\D+/g, '') : data;
  40. break;
  41. case 'boolean':
  42. result = Boolean(data);
  43. break;
  44. case 'array':
  45. result = Array.from(data);
  46. break;
  47. default:
  48. }
  49. return result;
  50. }
  51. export function getBlockConnection(block, index = 1) {
  52. const blockId = block.outputs[`output_${index}`]?.connections[0]?.node;
  53. return blockId;
  54. }