String.prototype.replaceLast = function (what, replacement) { var pcs = this.split(what); var lastPc = pcs.pop(); return pcs.join(what) + replacement + lastPc; } async function getContent(url) { const res = await fetch(url.concat('/download')); return res.text(); } async function migrateDocument(oldUrl, newUrl) { // get content of old pad var content = await getContent(oldUrl); // replace URLs content = content.replaceAll(new URL(oldUrl).hostname, new URL(newUrl).hostname); // visit new pad url (not possible via post api request because pad may already exists) // Caution: Content of new pad url will be overwritten! // Caution: History will not be moved to new pad url cy.visit(newUrl.concat('?edit')); cy.window().then((win) => { // Write Content cy.get('.CodeMirror-scroll').type('{ctrl}a{backspace}'); cy.get('.CodeMirror-scroll').type(content); // Visit old pad and replace content cy.visit(oldUrl); cy.get('#view-mode-toggle-edit').click({force: true}); cy.get('.CodeMirror-scroll').type('{ctrl}a{backspace}'); cy.get('.CodeMirror-scroll').type(`# 301 Pad moved{enter}=> [${newUrl}](${newUrl})`); }) } describe('Migrate document', () => { it('passes', async () => { // Read list of pads to migrate cy.fixture('pads').then(async(pads) => { if(pads.length === 0) { console.log("Didn't find any pad urls to migrate"); } for (const pad of pads) { await migrateDocument(pad.oldUrl, pad.newUrl); } }) }) })