1+ import { getTableStyle } from "./helpers/predefined_css.js" ;
2+ import {
3+ getCurrentTab ,
4+ openPopupWithHtml ,
5+ showLoading ,
6+ } from "./helpers/utils.js" ;
7+
18export default {
29 icon : "https://shopee.vn/favicon.ico" ,
310 name : {
@@ -8,60 +15,66 @@ export default {
815 en : "See how many times each product variant was purchased" ,
916 vi : "Thống kê xem tùy chọn sản phẩm nào được mọi người mua nhiều nhất" ,
1017 } ,
11- blackList : [ ] ,
12- whiteList : [ ] ,
1318
14- onClickExtension : function ( ) {
19+ onClickExtension : async function ( ) {
1520 // Source: https://gist.github.com/J2TEAM/bc5d9a1f3e648d6a3d679edf8689e5de
1621 // Post: https://www.facebook.com/groups/j2team.community/posts/1730753827256730/
1722
18- var $jscomp = $jscomp || { } ;
19- $jscomp . scope = { } ;
20- $jscomp . createTemplateTagFirstArg = function ( a ) {
21- return ( a . raw = a ) ;
22- } ;
23- $jscomp . createTemplateTagFirstArgWithRaw = function ( a , d ) {
24- a . raw = d ;
25- return a ;
26- } ;
27- $jscomp . arrayIteratorImpl = function ( a ) {
28- var d = 0 ;
29- return function ( ) {
30- return d < a . length ? { done : ! 1 , value : a [ d ++ ] } : { done : ! 0 } ;
23+ let { setLoadingText, closeLoading } = showLoading ( "Đang chuẩn bị..." ) ;
24+ try {
25+ var helpers = {
26+ scope : { } ,
27+ createTemplateTagFirstArg : function ( a ) {
28+ return ( a . raw = a ) ;
29+ } ,
30+ createTemplateTagFirstArgWithRaw : function ( a , d ) {
31+ a . raw = d ;
32+ return a ;
33+ } ,
34+ arrayIteratorImpl : function ( a ) {
35+ var d = 0 ;
36+ return function ( ) {
37+ return d < a . length ? { done : ! 1 , value : a [ d ++ ] } : { done : ! 0 } ;
38+ } ;
39+ } ,
40+ arrayIterator : function ( a ) {
41+ return { next : helpers . arrayIteratorImpl ( a ) } ;
42+ } ,
43+ makeIterator : function ( a ) {
44+ var d =
45+ "undefined" != typeof Symbol &&
46+ Symbol . iterator &&
47+ a [ Symbol . iterator ] ;
48+ return d ? d . call ( a ) : helpers . arrayIterator ( a ) ;
49+ } ,
3150 } ;
32- } ;
33- $jscomp . arrayIterator = function ( a ) {
34- return { next : $jscomp . arrayIteratorImpl ( a ) } ;
35- } ;
36- $jscomp . makeIterator = function ( a ) {
37- var d =
38- "undefined" != typeof Symbol && Symbol . iterator && a [ Symbol . iterator ] ;
39- return d ? d . call ( a ) : $jscomp . arrayIterator ( a ) ;
40- } ;
41- ( function ( ) {
42- function a ( b ) {
43- b . forEach ( function ( c ) {
51+
52+ function a ( arr ) {
53+ arr . forEach ( function ( c ) {
4454 c . product_items &&
45- c . product_items . forEach ( function ( e ) {
46- f [ e . model_name ] ? f [ e . model_name ] ++ : ( f [ e . model_name ] = 1 ) ;
55+ c . product_items . forEach ( function ( i ) {
56+ count [ i . model_name ]
57+ ? count [ i . model_name ] ++
58+ : ( count [ i . model_name ] = 1 ) ;
4759 } ) ;
4860 } ) ;
4961 }
5062 function d ( ) {
5163 var b = Object . fromEntries (
52- Object . entries ( f ) . sort ( function ( c , e ) {
53- var g = $jscomp . makeIterator ( c ) ;
64+ Object . entries ( count ) . sort ( function ( c , e ) {
65+ var g = helpers . makeIterator ( c ) ;
5466 g . next ( ) ;
5567 g = g . next ( ) . value ;
56- var m = $jscomp . makeIterator ( e ) ;
68+ var m = helpers . makeIterator ( e ) ;
5769 m . next ( ) ;
5870 return m . next ( ) . value - g ;
5971 } )
6072 ) ;
6173 console . info ( "Đã hoàn tất. Kết quả:" ) ;
6274 console . table ( b ) ;
75+ showResult ( b ) ;
6376 }
64- function run ( ) {
77+ async function run ( ) {
6578 var url =
6679 "https://shopee.vn/api/v2/item/get_ratings?filter=0&flag=1&itemid=" +
6780 itemId +
@@ -70,8 +83,9 @@ export default {
7083 "&shopid=" +
7184 shopId +
7285 "&type=0" ;
73- console . log ( "Đang quét trang " + page + "..." ) ;
74- fetch ( url , {
86+
87+ setLoadingText ( title + "<br/><br/>Đang quét trang " + page + "..." ) ;
88+ let res = await fetch ( url , {
7589 headers : {
7690 "x-api-source" : "pc" ,
7791 "x-requested-with" : "XMLHttpRequest" ,
@@ -80,44 +94,75 @@ export default {
8094 method : "GET" ,
8195 mode : "cors" ,
8296 credentials : "include" ,
83- } )
84- . then ( function ( res ) {
85- return res . json ( ) ;
97+ } ) ;
98+ let json = await res . json ( ) ;
99+ console . log ( json ) ;
100+
101+ json . error || ! json . data || ! json . data . ratings ?. length
102+ ? 1 < page && d ( )
103+ : ( a ( json . data . ratings ) ,
104+ 50 > page ? ( ( offset += 50 ) , page ++ , await run ( ) ) : d ( ) ) ;
105+ }
106+ function showResult ( data ) {
107+ let html = Object . entries ( data )
108+ . map ( ( [ key , value ] , index ) => {
109+ return `<tr>
110+ <td>${ index } </td>
111+ <td>${ key } </td>
112+ <td>${ value } </td>
113+ </tr>` ;
86114 } )
87- . then ( function ( json ) {
88- json . error || ! json . data || 0 === json . data . ratings . length
89- ? 1 < page && d ( )
90- : ( a ( json . data . ratings ) ,
91- 50 > page ? ( ( offset += 50 ) , page ++ , run ( ) ) : d ( ) ) ;
92- } ) ;
115+ . join ( "" ) ;
116+
117+ openPopupWithHtml (
118+ `<h1>
119+ <a href="${ tab . url } " target="_blank">Link sản phẩm</a>
120+ </h1>
121+ <table>
122+ <tr>
123+ <th>#</th>
124+ <th>Loại</th>
125+ <th>Lượt mua</th>
126+ </tr>
127+ ${ html }
128+ </table>
129+ <style>${ getTableStyle ( ) } </style>
130+ ` ,
131+ 500 ,
132+ window . screen . height
133+ ) ;
93134 }
94- var f = { } ,
135+
136+ var count = { } ,
95137 offset = 0 ,
96138 page = 1 ,
97139 shopId = null ,
98- itemId = null ;
99- console . info (
100- "Chia sẻ bởi Mạnh Tuấn từ nhóm J2TEAM Community - https://www.facebook.com/groups/j2team.community"
101- ) ;
102- ( function ( ) {
103- var b = new URL ( window . top . location . href ) ;
104- if ( ! b . hostname . includes ( "shopee.vn" ) )
105- throw Error ( "Đoạn mã này chỉ có thể sử dụng trên web Shopee.vn" ) ;
106- if ( ( b = / .+ - i \. ( [ 0 - 9 ] + ) .( [ 0 - 9 ] + ) / . exec ( b . pathname ) ) )
107- ( shopId = b [ 1 ] ) , ( itemId = b [ 2 ] ) ;
108- else
109- throw Error (
110- "Trang bạn đang xem không phải là một trang sản phẩm của Shopee."
111- ) ;
112- } ) ( ) ;
113- console . info (
114- "Bắt đầu phân tích với Shop ID " +
115- shopId +
116- " và mã sản phẩm " +
117- itemId +
118- "..."
119- ) ;
120- run ( ) ;
121- } ) ( ) ;
140+ itemId = null ,
141+ title = "" ;
142+
143+ setLoadingText ( "Đang lấy url trang web..." ) ;
144+ let tab = await getCurrentTab ( ) ;
145+ var b = new URL ( tab . url ) ;
146+ if ( ! b . hostname . includes ( "shopee.vn" ) )
147+ throw Error ( "Đoạn mã này chỉ có thể sử dụng trên web Shopee.vn" ) ;
148+ if ( ( b = / .+ - i \. ( [ 0 - 9 ] + ) .( [ 0 - 9 ] + ) / . exec ( b . pathname ) ) )
149+ ( shopId = b [ 1 ] ) , ( itemId = b [ 2 ] ) ;
150+ else
151+ throw Error (
152+ "Trang bạn đang xem không phải là một trang sản phẩm của Shopee."
153+ ) ;
154+
155+ title =
156+ "Bắt đầu phân tích với " +
157+ `<br/>Shop ID: ${ shopId } ` +
158+ `<br/>Mã sản phẩm ${ itemId } ` +
159+ "..." ;
160+
161+ await run ( ) ;
162+ } catch ( e ) {
163+ alert ( "ERROR: " + e ) ;
164+ } finally {
165+ closeLoading ( ) ;
166+ }
122167 } ,
123168} ;
0 commit comments