11// ==UserScript==
22// @name XMOJ
3- // @version 2.2 .1
3+ // @version 2.3 .1
44// @description XMOJ增强脚本
55// @author @XMOJ -Script-dev, @langningchen and the community
66// @namespace https://github/langningchen
2020// @grant unsafeWindow
2121// @grant GM_setValue
2222// @grant GM_getValue
23+ // @grant GM_cookie
2324// @homepage https://www.xmoj-bbs.me/
2425// @supportURL https://support.xmoj-bbs.me/form/8050213e-c806-4680-b414-0d1c48263677
2526// @connect api.xmoj-bbs.tech
@@ -444,7 +445,7 @@ let UtilityEnabled = (Name) => {
444445let storeCredential = async ( username , password ) => {
445446 if ( 'credentials' in navigator && window . PasswordCredential ) {
446447 try {
447- const credential = new PasswordCredential ( { id : username , password : password } ) ;
448+ const credential = new PasswordCredential ( { id : username , password : password } ) ;
448449 await navigator . credentials . store ( credential ) ;
449450 } catch ( e ) {
450451 console . error ( e ) ;
@@ -454,7 +455,7 @@ let storeCredential = async (username, password) => {
454455let getCredential = async ( ) => {
455456 if ( 'credentials' in navigator && window . PasswordCredential ) {
456457 try {
457- return await navigator . credentials . get ( { password : true , mediation : 'optional' } ) ;
458+ return await navigator . credentials . get ( { password : true , mediation : 'optional' } ) ;
458459 } catch ( e ) {
459460 console . error ( e ) ;
460461 }
@@ -479,6 +480,20 @@ let RequestAPI = (Action, Data, CallBack) => {
479480 Session = Temp [ i ] . split ( "=" ) [ 1 ] ;
480481 }
481482 }
483+ if ( Session === "" ) { //The cookie is httpOnly
484+ GM . cookie . set ( {
485+ name : 'PHPSESSID' ,
486+ value : ( Math . random ( ) . toString ( 36 ) . slice ( 2 ) + Math . random ( ) . toString ( 36 ) . slice ( 2 ) + Math . random ( ) . toString ( 36 ) . slice ( 2 ) ) . substring ( 0 , 28 ) ,
487+ path : "/"
488+ } )
489+ . then ( ( ) => {
490+ console . log ( 'Reset PHPSESSID successfully.' ) ;
491+ location . reload ( ) ; //Refresh the page to auth with the new PHPSESSID
492+ } )
493+ . catch ( ( error ) => {
494+ console . error ( error ) ;
495+ } ) ;
496+ }
482497 let PostData = {
483498 "Authentication" : {
484499 "SessionID" : Session , "Username" : CurrentUsername ,
@@ -1010,7 +1025,17 @@ async function main() {
10101025 } ) ;
10111026 PopupUL . children [ 5 ] . addEventListener ( "click" , ( ) => {
10121027 clearCredential ( ) ;
1013- document . cookie = "PHPSESSID=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/" ; //This is how you remove a cookie?
1028+ GM . cookie . set ( {
1029+ name : 'PHPSESSID' ,
1030+ value : ( Math . random ( ) . toString ( 36 ) . slice ( 2 ) + Math . random ( ) . toString ( 36 ) . slice ( 2 ) + Math . random ( ) . toString ( 36 ) . slice ( 2 ) ) . substring ( 0 , 28 ) ,
1031+ path : "/"
1032+ } )
1033+ . then ( ( ) => {
1034+ console . log ( 'Reset PHPSESSID successfully.' ) ;
1035+ } )
1036+ . catch ( ( error ) => {
1037+ console . error ( error ) ;
1038+ } ) ; //We can no longer rely of the server to set the cookie for us
10141039 location . href = "https://www.xmoj.tech/logout.php" ;
10151040 } ) ;
10161041 Array . from ( PopupUL . children ) . forEach ( item => {
@@ -1627,8 +1652,9 @@ async function main() {
16271652 } , 1000 ) ;
16281653 } else {
16291654 let PID = localStorage . getItem ( "UserScript-Contest-" + SearchParams . get ( "cid" ) + "-Problem-" + SearchParams . get ( "pid" ) + "-PID" ) ;
1630-
1631- document . querySelector ( "body > div > div.mt-3 > center" ) . lastChild . style . marginLeft = "10px" ;
1655+ if ( document . querySelector ( "body > div > div.mt-3 > center" ) . lastElementChild !== null ) {
1656+ document . querySelector ( "body > div > div.mt-3 > center" ) . lastElementChild . style . marginLeft = "10px" ;
1657+ }
16321658 //修复提交按钮
16331659 let SubmitLink = document . querySelector ( '.mt-3 > center:nth-child(1) > a:nth-child(12)' ) ;
16341660 if ( SubmitLink == null ) { //a special type of problem
0 commit comments