[wp-trac] [WordPress Trac] #54568: WordPress admin overwrite JS history.state

WordPress Trac noreply at wordpress.org
Wed Apr 10 08:41:43 UTC 2024


#54568: WordPress admin overwrite JS history.state
---------------------------+------------------------------
 Reporter:  maximeschoeni  |       Owner:  (none)
     Type:  defect (bug)   |      Status:  new
 Priority:  normal         |   Milestone:  Awaiting Review
Component:  General        |     Version:
 Severity:  normal         |  Resolution:
 Keywords:  has-patch      |     Focuses:  javascript
---------------------------+------------------------------

Comment (by findkit):

 Found a workaround with a monkey patch. Inject this JS early in the
 admin_head action:


 {{{#!javascript
 const original = history.replaceState;
 history.replaceState = function findkitReplaceStatePatch(state, unused,
 url) {
     const canonical = document.getElementById('wp-admin-canonical').href +
 window.location.hash;
     if (state === null && url === canonical) {
         original.call(history, history.state, unused, url);
         history.replaceState = original;
     } else {
         original.call(history, state, unused, url);
     }
 }
 }}}

 it overrides the existing replaceState function and detects the invalid
 call and recalls it the original function with history.state as the first
 param. Afterwards it restores the original function.

 Our full implementation is here:
 https://github.com/findkit/wp-
 findkit/blob/b6b2bead0a29664fefb14a6088eab197be73aa06/src/BugFixWpAdminHistoryState.php

 --
 Esa-Matti Suuronen

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/54568#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list