Skip to content

Commit f911465

Browse files
committed
chore: refactor app
1 parent 9b1fda3 commit f911465

File tree

5 files changed

+72
-89
lines changed

5 files changed

+72
-89
lines changed

src/background.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import axios from 'axios'
22
import store from './store'
33

4-
// array to save domain for checking if allready post
5-
const domainsVisited = []
64
const browser = require('webextension-polyfill')
75

8-
const map = store.getters.dataInfo
96
// send url to analyzer
10-
async function sendUrl (url, domain, tabId) {
11-
// loading
12-
store.commit('SET_ISLOADING', true)
13-
7+
async function analyze (tabId, url) {
148
await axios({
159
method: 'GET',
1610
url: `https://vuetelemetry.com/api/analyze?url=${url}&src=extension`,
@@ -23,16 +17,11 @@ async function sendUrl (url, domain, tabId) {
2317
tabId,
2418
path: 'icons/icon-128.png'
2519
})
26-
setMapData(domain, data.body)
20+
21+
store.commit('SET_SHOWCASE', data.body)
2722
}).catch((e) => {
28-
browser.browserAction.setIcon({
29-
tabId,
30-
path: 'icons/icon-grey-128.png'
31-
})
32-
setMapData(domain, e.response.data.statusCode === 400 ? 'noVue' : 'error')
23+
store.commit('SET_SHOWCASE', e.response.data.statusCode === 400 ? 'noVue' : 'error')
3324
})
34-
35-
store.commit('SET_ISLOADING', false)
3625
}
3726

3827
// when tab clicked
@@ -57,34 +46,54 @@ async function handleUpdated (tabId, changeInfo, tabInfo) {
5746
browser.tabs.onActivated.addListener(handleActivated)
5847
browser.tabs.onUpdated.addListener(handleUpdated)
5948

60-
// detect vue by calling detector and sendUrl
49+
// detect vue by calling detector and analyze
6150
async function detectVue (tabId, url) {
62-
store.commit('SET_CURRENTDOMAIN', url)
51+
browser.browserAction.setIcon({
52+
tabId,
53+
path: 'icons/icon-grey-128.png'
54+
})
55+
56+
store.commit('SET_DOMAIN', null)
6357

6458
if (!url) {
6559
return
6660
}
6761
if (/^chrome/.test(url) || /^about/.test(url)) {
68-
return setMapData(url, 'noVue')
62+
return
6963
}
7064

71-
await hasVue(tabId).then(({ response }) => {
72-
store.commit('SET_CURRENTDOMAIN', response.vueInfo.domain)
65+
store.commit('SET_ISLOADING', true)
66+
67+
try {
68+
const { response } = await resolveVue(tabId)
69+
const { domain, hasVue } = response.vueInfo
70+
71+
store.commit('SET_DOMAIN', domain)
7372

74-
if (!domainsVisited.includes(response.vueInfo.domain)) {
75-
domainsVisited.push(response.vueInfo.domain)
73+
const showcase = store.getters.showcase
7674

77-
if (response.vueInfo.hasVue) {
78-
sendUrl(url, response.vueInfo.domain, tabId)
75+
if (showcase) {
76+
if (showcase.id) {
77+
browser.browserAction.setIcon({
78+
tabId,
79+
path: 'icons/icon-128.png'
80+
})
81+
}
82+
} else {
83+
if (hasVue) {
84+
await analyze(tabId, url)
7985
} else {
80-
setMapData(response.vueInfo.domain, 'noVue')
86+
store.commit('SET_SHOWCASE', 'noVue')
8187
}
8288
}
83-
})
89+
} catch (e) {
90+
}
91+
92+
store.commit('SET_ISLOADING', false)
8493
}
8594

8695
// check vue in content.js and get response
87-
function hasVue (tabId) {
96+
function resolveVue (tabId) {
8897
return new Promise((resolve) => {
8998
browser.tabs.sendMessage(
9099
tabId,
@@ -94,8 +103,3 @@ function hasVue (tabId) {
94103
})
95104
})
96105
}
97-
98-
function setMapData (domain, data) {
99-
map[domain] = data
100-
store.commit('SET_DATAINFO', map)
101-
}

src/popup/App.vue

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<div class="flex items-center">
1010
<a
1111
v-if="state === 'data'"
12-
:href="`https://vuetelemetry.com/explore/${website.slug}`"
12+
:href="`https://vuetelemetry.com/explore/${showcase.slug}`"
1313
target="_blank"
1414
class="mr-3"
1515
>
@@ -39,33 +39,33 @@
3939
<div class="grid grid-cols-3 gap-4">
4040
<ListBlock label="Vue version" href="https://vuejs.org/">
4141
<img :src="getURL('/vue.svg')" alt class="w-6 h-6 mr-2" />
42-
<div class="font-semibold">{{ website.vueVersion }}</div>
42+
<div class="font-semibold">{{ showcase.vueVersion }}</div>
4343
</ListBlock>
4444

45-
<ListBlock v-if="website.framework" label="Framework" :href="website.framework.url">
46-
<img :src="getURL(website.framework.imgPath)" alt class="w-6 h-6 mr-2" />
47-
<div class="font-semibold">{{ website.framework.name }}</div>
45+
<ListBlock v-if="showcase.framework" label="Framework" :href="showcase.framework.url">
46+
<img :src="getURL(showcase.framework.imgPath)" alt class="w-6 h-6 mr-2" />
47+
<div class="font-semibold">{{ showcase.framework.name }}</div>
4848
</ListBlock>
4949

50-
<ListBlock v-if="website.ui" label="UI Framework" :href="website.ui.url">
51-
<img :src="getURL(website.ui.imgPath)" alt class="w-6 h-6 mr-2" />
52-
<div class="font-semibold">{{ website.ui.name }}</div>
50+
<ListBlock v-if="showcase.ui" label="UI Framework" :href="showcase.ui.url">
51+
<img :src="getURL(showcase.ui.imgPath)" alt class="w-6 h-6 mr-2" />
52+
<div class="font-semibold">{{ showcase.ui.name }}</div>
5353
</ListBlock>
5454

5555
<ListBlock label="Rendering">
56-
<div class="font-semibold">{{ website.hasSSR ? 'Universal' : 'Client-side' }}</div>
56+
<div class="font-semibold">{{ showcase.hasSSR ? 'Universal' : 'Client-side' }}</div>
5757
</ListBlock>
5858

5959
<ListBlock
60-
v-if="website.framework && website.framework.slug === 'nuxtjs'"
60+
v-if="showcase.framework && showcase.framework.slug === 'nuxtjs'"
6161
label="Deployment"
6262
>
63-
<div class="font-semibold">{{ website.isStatic ? 'Static' : 'Server' }}</div>
63+
<div class="font-semibold">{{ showcase.isStatic ? 'Static' : 'Server' }}</div>
6464
</ListBlock>
6565
</div>
6666
</div>
6767

68-
<div v-if="website.plugins.length" class="mb-4">
68+
<div v-if="showcase.plugins.length" class="mb-4">
6969
<div class="mb-4">
7070
<h3 class="flex items-center font-bold pl-2 text-primary-500 uppercase">
7171
<PluginsIcon class="h-6 mr-2 text-primary-500 opacity-50" />Plugins
@@ -74,7 +74,7 @@
7474

7575
<div class="flex flex-wrap">
7676
<a
77-
v-for="plugin in website.plugins"
77+
v-for="plugin in showcase.plugins"
7878
:key="plugin.id"
7979
:href="plugin.url"
8080
target="_blank"
@@ -87,7 +87,7 @@
8787
</div>
8888
</div>
8989

90-
<div v-if="website.modules.length">
90+
<div v-if="showcase.modules.length">
9191
<div class="mb-4">
9292
<h3 class="flex items-center font-bold pl-2 text-primary-500 uppercase">
9393
<ModulesIcon class="h-6 mr-2 text-primary-500 opacity-50" />Nuxt Modules
@@ -96,7 +96,7 @@
9696

9797
<div class="flex flex-wrap">
9898
<a
99-
v-for="module in website.modules"
99+
v-for="module in showcase.modules"
100100
:key="module.id"
101101
:href="module.url"
102102
target="_blank"
@@ -117,7 +117,7 @@
117117
</template>
118118

119119
<script>
120-
import { mapState } from 'vuex'
120+
import { mapGetters } from 'vuex'
121121
122122
import LogoIcon from '../images/logo.svg?inline'
123123
import ExternalLinkIcon from '../images/external-link.svg?inline'
@@ -141,20 +141,14 @@ export default {
141141
ListBlock
142142
},
143143
computed: {
144-
...mapState([
145-
'dataInfo',
144+
...mapGetters([
146145
'isLoading',
147-
'currentDomain'
146+
'showcase'
148147
]),
149-
website () {
150-
return this.dataInfo[this.currentDomain]
151-
},
152148
state () {
153-
const website = this.dataInfo[this.currentDomain]
154-
155-
if (website && website === 'error') {
149+
if (this.showcase && this.showcase === 'error') {
156150
return 'error'
157-
} else if (website && website !== 'noVue') {
151+
} else if (this.showcase && this.showcase !== 'noVue') {
158152
return 'data'
159153
}
160154

src/store/actions.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/store/index.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11
import Vue from 'vue'
22
import Vuex from 'vuex'
33
import VuexWebExtensions from 'vuex-webextensions'
4-
import * as types from './mutation-types'
5-
import * as actions from './actions'
64

75
Vue.use(Vuex)
86

97
export default new Vuex.Store({
108
state: {
11-
dataInfo: {},
9+
showcases: {},
1210
isLoading: false,
13-
currentDomain: ''
11+
domain: null
1412
},
1513
getters: {
16-
dataInfo (state) { return state.dataInfo },
1714
isLoading (state) { return state.isLoading },
18-
currentDomain (state) { return state.currentDomain }
15+
showcase (state) {
16+
return state.domain ? state.showcases[state.domain] : null
17+
}
1918
},
2019
mutations: {
21-
[types.SET_DATAINFO] (state, payload) {
22-
state.dataInfo = payload
20+
SET_ISLOADING (state, isLoading) {
21+
state.isLoading = isLoading
2322
},
24-
[types.SET_ISLOADING] (state, payload) {
25-
state.isLoading = payload
23+
SET_SHOWCASE (state, showcase) {
24+
if (state.domain) {
25+
state.showcases = {
26+
...state.showcases,
27+
[state.domain]: showcase
28+
}
29+
}
2630
},
27-
[types.SET_CURRENTDOMAIN] (state, payload) {
28-
state.currentDomain = payload
31+
SET_DOMAIN (state, domain) {
32+
state.domain = domain
2933
}
3034
},
31-
actions,
3235
plugins: [
3336
VuexWebExtensions({
34-
persistentStates: ['dataInfo', 'isLoading', 'currentDomain'],
3537
loggerLevel: 'info'
3638
})
3739
]

src/store/mutation-types.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)