1+ const countApi = 'https://greasyfork.org/scripts/by-site.json' ,
2+ adultAPI = 'https://sleazyfork.org/scripts/by-site.json' ,
3+ sleazy = browser . runtime . getURL ( "js/sleazyfork.js" ) ,
4+ greasy = document . location . host ,
5+ s = document . createElement ( "script" ) ;
6+ s . type = "module" ;
7+ s . src = sleazy ;
8+ s . crossOrigin = "anonymous" ;
9+
10+ function getCurrentTabUrl ( callback ) {
11+ // Query filter to be passed to chrome.tabs.query - see
12+ // https://developer.chrome.com/extensions/tabs#method-query
13+ let queryInfo = {
14+ active : true ,
15+ currentWindow : true
16+ } ;
17+
18+ chrome . tabs . query ( queryInfo , ( tabs ) => {
19+ let tab = tabs [ 0 ] ,
20+ url = tab . url ;
21+ console . assert ( typeof url == 'string' , 'tab.url should be a string' ) ;
22+
23+ callback ( url ) ;
24+ } ) ;
25+ }
26+
27+ function getUrlHost ( url ) {
28+ let a = document . createElement ( 'a' ) ;
29+ a . href = url ;
30+ let mainHost = psl . get ( a . hostname ) || a . hostname . split ( '.' ) . splice ( - 2 ) . join ( '.' )
31+ return mainHost ;
32+ }
33+
34+ function changeBadge ( data ) {
35+ getCurrentTabUrl ( function ( url ) {
36+ let host = getUrlHost ( url ) ,
37+ count = data [ host ] ;
38+ count = count > 50 ? 50 : count ;
39+ sessionStorage . setItem ( 'host' , host ) ;
40+ if ( count ) {
41+ chrome . browserAction . setBadgeText ( {
42+ text : count . toString ( )
43+ } ) ;
44+ } else {
45+ chrome . browserAction . setBadgeText ( {
46+ text : ''
47+ } ) ;
48+ }
49+ } ) ;
50+ }
51+ fetch ( adultAPI ) . then ( ( r ) => {
52+ r . json ( ) . then ( ( data ) => {
53+ console . log ( 'count data loaded!' ) ;
54+ chrome . tabs . onUpdated . addListener ( ( ) => {
55+ changeBadge ( data ) ;
56+ } ) ;
57+ chrome . tabs . onActivated . addListener ( ( ) => {
58+ changeBadge ( data ) ;
59+ } ) ;
60+ } ) ;
61+ } ) ;
62+ fetch ( countApi ) . then ( ( r ) => {
63+ r . json ( ) . then ( ( data ) => {
64+ console . log ( 'count data loaded!' ) ;
65+ chrome . tabs . onUpdated . addListener ( ( ) => {
66+ changeBadge ( data ) ;
67+ } ) ;
68+ chrome . tabs . onActivated . addListener ( ( ) => {
69+ changeBadge ( data ) ;
70+ } ) ;
71+ } ) ;
72+ } ) ;
0 commit comments