I made a bookmarklet to migrate an old reddit wiki page to the new wiki
It can transfer the current wiki page you have opened from old to new, but not the opposite. Most formatting will work, including images. The spoiler tag doesn't work. Here's the bookmarklet:
javascript:(async()=>{try{await (async()=>{const pingURL=url=>{return new Promise(resolve=>{const img=new Image;img.onload=()=>resolve();img.onerror=()=>resolve();img.src=url%2B(url.includes("?")?"&":"?")%2B"v="%2BDate.now()})};const getCookie=async name=>{if(typeof window.cookieStore?.get==="function"){return(await cookieStore.get(name))?.value??null}const cookie="; "%2Bdocument.cookie%2B";";const searchPattern="; "%2Bname%2B"=";const patternStartIndex=cookie.indexOf(searchPattern);if(patternStartIndex===-1)return null;const valueStartIndex=patternStartIndex%2BsearchPattern.length;return decodeURIComponent(cookie.substring(valueStartIndex,cookie.indexOf(";",valueStartIndex)))};const getCookiePing=async(name,pingSrc)=>{let cookieData=await getCookie(name);if(!cookieData){await pingURL(pingSrc);cookieData=await getCookie(name);if(!cookieData)throw new Error("Failed getting cookie '"%2Bname%2B"' by fetching "%2BpingSrc)}return cookieData};async function getREST(endpoint){const response=await fetch(endpoint,{method:"GET",headers:{Accept:"application/json"}});if(!response.ok)throw new Error(`Failed to fetch ${endpoint}: ${response.status}`);return await response.json()}async function svcGqlFetch(operation,variables){const csrfToken=await getCookiePing("csrf_token","https://sh.reddit.com/404");const response=await fetch("/svc/shreddit/graphql",{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify({operation:operation,variables:variables,csrf_token:csrfToken})});if(!response.ok)throw new Error(`GraphQL request failed for ${operation}: ${response.status}`);const responseData=await response.json();if(responseData.errors?.length)throw new Error(`GraphQL request for ${operation} returned errors: ${JSON.stringify(responseData.errors)}`);return responseData.data}const wikiUrlMatch=location.pathname.match(/^/r/([^/]%2B)/wiki/(?:(?:edit|settings)/)?(.%2B)$/);if(!wikiUrlMatch)throw new Error("Not a wiki page URL");const subredditName=wikiUrlMatch[1];const pageName=wikiUrlMatch[2];const wikiPageData=await getREST(`/r/${subredditName}/wiki/${pageName}.json?raw_json=1`);if(!wikiPageData||wikiPageData.kind!=="wikipage")throw new Error("Failed to fetch wiki page data");const{content_html,reason}=wikiPageData.data;const contentDoc=(new DOMParser).parseFromString(content_html,"text/html");contentDoc.querySelectorAll("td, th").forEach(col=>{const align=col.getAttribute("align");if(align){col.style.textAlign=align;col.removeAttribute("align")}});const{upsertWikiPage}=await svcGqlFetch("UpsertWikiPage",{input:{page:pageName,subredditName:subredditName,reason:reason||"Sync wiki page with old reddit",content:{markdown:contentDoc.body.innerHTML},postToCommunity:false,pageTitle:pageName}});if(!upsertWikiPage.ok){if(upsertWikiPage.isRateLimited){throw new Error(`Rate limited. Try again after: ${upsertWikiPage.rateLimitEndsAt}`)}const{errors,fieldErrors}=upsertWikiPage;throw new Error(`Failed to upsert wiki page: ${JSON.stringify({errors:errors,fieldErrors:fieldErrors},null,2)}`)}alert(`SUCCESS: Wiki page '${pageName}' in subreddit '${subredditName}' has been successfully upserted.`)})();}catch(e){console.error("Bookmarklet failed:", e.message);alert("Bookmarklet failed: " %2B e.message)}})();On a desktop browser, you can right click on the top bar and add a bookmark. Call it something like "Wiki Old => New". Paste the code above as the URL. Now, go to the wiki page you want to transfer. Then click on the bookmark to run it.
On a mobile browser, go to any page and bookmark it with the star button. Then go to all bookmarks and find the newly created bookmark. Edit it and change the title to your liking and enter the code as URL. Now, open the wiki page you want to transfer, and click on the address bar. Enter whatever you entered as the title of the bookmark on the address bar. On the bottom of the search suggestions, you should see the bookmark. Just tap on it to run it
The script is fairly basic. The new wiki uses raw HTML for formatting instead of markdown. The script fetches the wiki page rendered HTML and revision reason from the API, and just creates the new wiki page with the old reddit rendered HTML and the same revision reason. It does some modifications to the old reddit HTML so that table column alignment options work (converts align='left' to style="text-align: left;" etc)