Skip to content

Commit 2ac9cd7

Browse files
committed
ok
1 parent 7f3ccad commit 2ac9cd7

5 files changed

Lines changed: 61 additions & 43 deletions

File tree

crx/extension/background.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
const countApi = 'https://greasyfork.org/scripts/by-site.json'
3+
14
function getCurrentTabUrl(callback) {
25
// Query filter to be passed to chrome.tabs.query - see
36
// https://developer.chrome.com/extensions/tabs#method-query
@@ -13,7 +16,7 @@ function getCurrentTabUrl(callback) {
1316
// A window can only have one active tab at a time, so the array consists of
1417
// exactly one tab.
1518
var tab = tabs[0];
16-
19+
1720
// A tab is a plain object that provides information about the tab.
1821
// See https://developer.chrome.com/extensions/tabs#type-Tab
1922
var url = tab.url;
@@ -37,8 +40,38 @@ function getCurrentTabUrl(callback) {
3740
// alert(url); // Shows "undefined", because chrome.tabs.query is async.
3841
}
3942

40-
chrome.tabs.onUpdated.addListener(function(){
43+
function getUrlHostname(url) {
44+
var a = document.createElement('a');
45+
a.href = url;
46+
return a.hostname
47+
}
48+
49+
function changeBadge(data) {
4150
getCurrentTabUrl(function(url){
42-
alert(url)
43-
})
51+
let host = getUrlHostname(url).split('.').splice(-2).join('.')
52+
let count = data[host]
53+
count = count > 50 ? 50 : count
54+
chrome.storage.local.set({'host':host})
55+
if(count) {
56+
chrome.browserAction.setBadgeText({
57+
text: count.toString()
58+
})
59+
}else {
60+
chrome.browserAction.setBadgeText({
61+
text: ''
62+
})
63+
}
64+
})
65+
}
66+
67+
fetch(countApi).then((r) => {
68+
r.json().then((data) => {
69+
alert('data loaded')
70+
chrome.tabs.onUpdated.addListener(() => {
71+
changeBadge(data)
72+
})
73+
chrome.tabs.onActivated.addListener(() => {
74+
changeBadge(data)
75+
})
76+
})
4477
})

crx/extension/popup.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crx/src/common/js/tools.js

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import timeago from 'timeago.js'
44

55
let config = {
6-
cacheKey: 'jae_fetch_userjs_cache',
7-
countKey: 'jae_fetch_userjs_count',
8-
host: window.location.hostname.split('.').splice(-2).join('.'),
96
api: 'https://greasyfork.org/en/scripts/by-site/{host}.json'
107
}
118

@@ -33,35 +30,27 @@ export default {
3330
for (let i = 0, l = keys.length; i < l; i++) v = v[keys[i]]
3431
return (typeof v !== 'undefined' && v !== null) ? v : ''
3532
})
36-
},
37-
getJSON (url, callback) {
38-
parent.window.GmAjax({
39-
method: 'GET',
40-
url: url,
41-
onload: (res) => {
42-
let json = JSON.parse(res.responseText)
43-
callback(json)
44-
}
45-
})
4633
},
4734
// 获取油猴缓存好的脚本数据
4835
getData (callback) {
49-
let data = sessionStorage.getItem(config.cacheKey)
50-
if (data) {
51-
data = JSON.parse(data)
52-
callback(data)
53-
} else {
54-
let api = this.nano(config.api, {
55-
host: config.host
56-
})
57-
this.getJSON(api, (json) => {
58-
sessionStorage.setItem(config.cacheKey, JSON.stringify(json))
59-
callback(json)
60-
})
61-
}
62-
},
63-
64-
getCount () {
65-
return 12
36+
chrome.storage.local.get(['host'], (rt) => {
37+
let host = rt.host
38+
let data = sessionStorage.getItem(host)
39+
if (data) {
40+
data = JSON.parse(data)
41+
callback(data)
42+
} else {
43+
let api = this.nano(config.api, {
44+
host: host
45+
})
46+
fetch(api)
47+
.then((r) => {
48+
r.json().then((json) => {
49+
sessionStorage.setItem(host, JSON.stringify(json))
50+
callback(json)
51+
})
52+
})
53+
}
54+
})
6655
}
6756
}

crx/src/components/Table.vue

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,16 @@
9999
export default {
100100
components: { Info, Indicator },
101101
mounted: function () {
102-
103-
this.count = Tools.getCount()
104-
105102
this.$Spin.show()
106-
this.getData((json) => {
103+
Tools.getData((json) => {
107104
this.data = json
105+
this.count = this.data.length
108106
this.$Spin.hide()
109107
this.showBody = !this.showBody
110108
setTimeout(() => {
111109
this.showTitle = this.showBody
112110
}, 500)
113111
})
114-
115112
},
116113
data: function () {
117114
return {
@@ -222,7 +219,7 @@
222219
}
223220
},
224221
watch: {
225-
222+
226223
},
227224
methods: {
228225
close () {

crx/src/main.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Vue.use(VueI18n)
1515
Vue.use(iView)
1616

1717
let nlang = navigator.language.toLowerCase()
18-
if(nlang === 'zh') {
18+
if (nlang === 'zh') {
1919
nlang = 'zh-cn'
2020
}
2121
let lang = localeMessage[nlang] ? nlang : 'en-us'
@@ -32,4 +32,3 @@ new Vue({ // eslint-disable-line no-new
3232
el: appEl,
3333
render: h => h(App)
3434
})
35-

0 commit comments

Comments
 (0)