Home.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div class="bg-accent rounded-b-2xl absolute top-0 left-0 h-32 w-full"></div>
  3. <div
  4. class="flex dark placeholder-black text-white px-5 pt-8 mb-6 items-center"
  5. >
  6. <ui-input
  7. v-model="query"
  8. autofocus
  9. prepend-icon="riSearch2Line"
  10. class="flex-1 search-input"
  11. placeholder="Search..."
  12. ></ui-input>
  13. <ui-button
  14. icon
  15. title="Element selector"
  16. class="ml-3"
  17. @click="selectElement"
  18. >
  19. <v-remixicon name="riFocus3Line" />
  20. </ui-button>
  21. <ui-button icon title="Dashboard" class="ml-3" @click="openDashboard">
  22. <v-remixicon name="riHome5Line" />
  23. </ui-button>
  24. </div>
  25. <div class="px-5 pb-5 space-y-2">
  26. <ui-card v-if="Workflow.all().length === 0" class="text-center">
  27. <img src="@/assets/svg/alien.svg" />
  28. <p class="font-semibold">It looks like you don't have any workflows</p>
  29. <ui-button variant="accent" class="mt-6" @click="openDashboard">
  30. New workflow
  31. </ui-button>
  32. </ui-card>
  33. <home-workflow-card
  34. v-for="workflow in workflows"
  35. :key="workflow.id"
  36. :workflow="workflow"
  37. @details="openDashboard(`/workflows/${$event.id}`)"
  38. @execute="executeWorkflow"
  39. @rename="renameWorkflow"
  40. @delete="deleteWorkflow"
  41. />
  42. </div>
  43. </template>
  44. <script setup>
  45. import { ref, computed } from 'vue';
  46. import browser from 'webextension-polyfill';
  47. import { useDialog } from '@/composable/dialog';
  48. import { sendMessage } from '@/utils/message';
  49. import Workflow from '@/models/workflow';
  50. import HomeWorkflowCard from '@/components/popup/home/HomeWorkflowCard.vue';
  51. const dialog = useDialog();
  52. const query = ref('');
  53. const workflows = computed(() =>
  54. Workflow.query()
  55. .where(({ name }) =>
  56. name.toLocaleLowerCase().includes(query.value.toLocaleLowerCase())
  57. )
  58. .orderBy('createdAt', 'asc')
  59. .get()
  60. );
  61. function executeWorkflow(workflow) {
  62. sendMessage('workflow:execute', workflow, 'background');
  63. }
  64. function renameWorkflow({ id, name }) {
  65. dialog.prompt({
  66. title: 'Rename workflow',
  67. placeholder: 'Workflow name',
  68. okText: 'Rename',
  69. inputValue: name,
  70. onConfirm: (newName) => {
  71. Workflow.update({
  72. where: id,
  73. data: {
  74. name: newName,
  75. },
  76. });
  77. },
  78. });
  79. }
  80. function deleteWorkflow({ id, name }) {
  81. dialog.confirm({
  82. title: 'Delete workflow',
  83. okVariant: 'danger',
  84. body: `Are you sure you want to delete "${name}" workflow?`,
  85. onConfirm: () => {
  86. Workflow.delete(id);
  87. },
  88. });
  89. }
  90. function openDashboard(url) {
  91. const tabOptions = {
  92. active: true,
  93. url: browser.runtime.getURL(
  94. `/newtab.html#${typeof url === 'string' ? url : ''}`
  95. ),
  96. };
  97. browser.tabs
  98. .query({ url: browser.runtime.getURL('/newtab.html') })
  99. .then(([tab]) => {
  100. if (tab) {
  101. browser.tabs.update(tab.id, tabOptions).then(() => {
  102. browser.tabs.reload(tab.id);
  103. });
  104. } else {
  105. browser.tabs.create(tabOptions);
  106. }
  107. });
  108. }
  109. async function selectElement() {
  110. const [tab] = await browser.tabs.query({ active: true });
  111. try {
  112. await browser.tabs.sendMessage(tab.id, {
  113. type: 'content-script-exists',
  114. });
  115. browser.tabs.sendMessage(tab.id, {
  116. type: 'select-element',
  117. });
  118. } catch (error) {
  119. if (error.message.includes('Could not establish connection.')) {
  120. await browser.tabs.executeScript(tab.id, {
  121. file: './contentScript.bundle.js',
  122. });
  123. selectElement();
  124. }
  125. console.error(error);
  126. }
  127. }
  128. </script>