@@ -11,67 +11,87 @@ export default {
1111 whiteList : [ "https://*.facebook.com/*" , "https://*.messenger.com/*" ] ,
1212
1313 onDocumentStart : ( ) => {
14- function notifyTypingEvent ( ) {
15- // TODO add notification UI
16- }
17- function saveTyingEvent ( uid , typing ) {
18- let key = "ufs-fb_whoIsTyping" ;
19- let old = JSON . parse ( localStorage . getItem ( key ) ?? "[]" ) ;
20- old . push ( {
21- time : new Date ( ) . getTime ( ) ,
22- uid : uid ,
23- typing : typing ,
24- } ) ;
25- localStorage . setItem ( key , JSON . stringify ( old ) ) ;
26- }
14+ window . ufs_whoIsTyping_Cached = { } ;
2715
2816 const WebSocketOrig = window . WebSocket ;
2917 window . WebSocket = function fakeConstructor ( dt , config ) {
3018 const websocket_instant = new WebSocketOrig ( dt , config ) ;
3119 websocket_instant . addEventListener ( "message" , async function ( achunk ) {
3220 let utf8_str = new TextDecoder ( "utf-8" ) . decode ( achunk . data ) ;
3321
34- if (
35- utf8_str . includes ( "updateTypingIndicator" ) ||
36- utf8_str . includes ( "mid.$" )
37- ) {
38- let isStartTyping = utf8_str . includes ( ",true)" ) ;
39- let isStopTyping = utf8_str . includes ( ",false)" ) ;
22+ if ( utf8_str . includes ( "updateTypingIndicator" ) ) {
23+ console . log ( "abc" ) ;
24+ try {
25+ let isStartTyping = utf8_str . includes ( ",true)" ) ;
26+ let isStopTyping = utf8_str . includes ( ",false)" ) ;
4027
41- let arr = utf8_str . match ( / ( \[ ) ( .* ?) ( \] ) / g) ;
42- let uid = UsefulScriptGlobalPageContext . Facebook . decodeArrId (
43- JSON . parse ( arr [ arr . length - 2 ] )
44- ) ;
28+ let arr = utf8_str . match ( / ( \[ ) ( .* ?) ( \] ) / g) ;
29+ let uid = UsefulScriptGlobalPageContext . Facebook . decodeArrId (
30+ JSON . parse ( arr [ arr . length - 2 ] )
31+ ) ;
4532
46- console . log ( uid , isStartTyping , utf8_str ) ;
47- saveTyingEvent ( uid , isStartTyping ) ;
33+ if ( ! ( uid in window . ufs_whoIsTyping_Cached ) ) {
34+ let userData =
35+ await UsefulScriptGlobalPageContext . Facebook . getUserProfileDataFromUid (
36+ uid
37+ ) ;
38+ window . ufs_whoIsTyping_Cached [ uid ] = userData ;
39+
40+ console . log ( userData ) ;
41+ }
42+
43+ let { name, profiePicLarge } = window . ufs_whoIsTyping_Cached [ uid ] ;
44+ notifyTypingEvent ( uid , name , profiePicLarge , isStartTyping ) ;
45+ } catch ( e ) {
46+ console . log ( "ERROR: " , e ) ;
47+ }
4848 }
4949 } ) ;
5050 return websocket_instant ;
5151 } ;
5252 window . WebSocket . prototype = WebSocketOrig . prototype ;
5353 window . WebSocket . prototype . constructor = window . WebSocket ;
5454
55- requireLazy (
56- [
57- "LSUpdateTypingIndicator" ,
58- "LSTypingUpdateTypingIndicatorStoredProcedure_Optimized" ,
59- ] ,
60- ( L1 , L2 ) => {
61- const L1Orig = L1 . prototype . constructor ;
62- L1 . prototype . constructor = function ( ) {
63- console . log ( arguments ) ;
64- return L1Orig . apply ( this , arguments ) ;
55+ function notifyTypingEvent ( uid , name , avatar , isTyping ) {
56+ let divId = "ufs-who-is-typing" ;
57+ let exist = document . querySelector ( "#" + divId ) ;
58+ if ( ! exist ) {
59+ exist = document . createElement ( "div" ) ;
60+ exist . id = divId ;
61+ exist . innerHTML = `<div class="ufs-header clearfix">
62+ <button>+</button>
63+ </div>` ;
64+ exist . querySelector ( ".ufs-header button" ) . onclick = ( e ) => {
65+ exist . classList . toggle ( "collapsed" ) ;
6566 } ;
6667
67- const L2Orig = L2 . prototype . constructor ;
68- L2 . prototype . constructor = function ( ) {
69- console . log ( "L2" , arguments ) ;
70- return L2Orig . apply ( this , arguments ) ;
71- } ;
68+ document . body . appendChild ( exist ) ;
69+
70+ UsefulScriptGlobalPageContext . Extension . getURL (
71+ "scripts/fb_whoIsTyping.css"
72+ ) . then ( UsefulScriptGlobalPageContext . DOM . injectCssFile ) ;
7273 }
73- ) ;
74- } ,
7574
76- onDocumentIdle : ( ) => { } ,
75+ let time = new Date ( ) . toLocaleTimeString ( ) ;
76+ let text =
77+ `<b><a target="_blank" href="https://fb.com/${ uid } ">${ name } </a></b>` +
78+ ` ${ isTyping ? "đang" : "ngưng" } nhắn cho bạn.` ;
79+
80+ let newNoti = document . createElement ( "div" ) ;
81+ newNoti . className = "ufs-noti-item clearfix" ;
82+ newNoti . innerHTML = `
83+ <button class="ufs-close-btn">X</button>
84+ <img src="${ avatar } " class="ufs-avatar" />
85+ <div class="ufs-content">
86+ <p class="ufs-time">${ time } </p>
87+ <p class="ufs-text">${ text } </p>
88+ </div>
89+ ` ;
90+ newNoti . querySelector ( ".ufs-close-btn" ) . onclick = ( ) => {
91+ newNoti . remove ( ) ;
92+ if ( ! exist . querySelector ( "ufs-noti-item" ) ) exist . remove ( ) ;
93+ } ;
94+ exist . appendChild ( newNoti ) ;
95+ }
96+ } ,
7797} ;
0 commit comments