Skip to content

Instantly share code, notes, and snippets.

@rcaloras
Created January 6, 2023 20:42
Show Gist options
  • Save rcaloras/95bcd4e83cb17ff40d28e8d51fc2e0df to your computer and use it in GitHub Desktop.
Save rcaloras/95bcd4e83cb17ff40d28e8d51fc2e0df to your computer and use it in GitHub Desktop.
Opensea metadata refresh script. For chains that aren't supported via API.
// Paste this script into the console on a specfic NFT detail page for your collection.
// e.g. https://opensea.io/assets/ethereum/0x1485297e942ce64e0870ece60179dfda34b4c625/3723
// Adjust i on the for loop for the range of nfts you wish to refresh
// Should work for all chains including Polygon and testnets.
// Returns a Promise that resolves after "ms" Milliseconds
const timer = ms => new Promise(res => setTimeout(res, ms))
async function refresh_metadata() { // We need to wrap the loop into an async function for this to work
for (var i = 1; i <= 1326; i++) {
await timer(1000); // then the created Promise can be awaited
var collectionUrl = window.location.href.split('/').slice(0, -1).join('/') + '/'
var nextUrl = collectionUrl + i
var nextWindow = window.open(nextUrl)
nextWindow.addEventListener('load', function () {
// Interact with the dom on this new page
kabob = nextWindow.document.getElementsByClassName('sc-a143597d-0 sc-f0199734-0 RovoC gdcSkC material-icons')[2]
kabob.click()
refresh = nextWindow.document.getElementsByClassName('sc-29427738-0 sc-bdnxRM sc-a8df1259-2 erpyI iPAlIP')[0]
refresh.click()
console.log("Refreshed for " + i)
}, false);
nextWindow.blur();
await timer(3000);
nextWindow.close();
}
}
refresh_metadata()
@Dercont
Copy link

Dercont commented Apr 20, 2023

Thanks bro! It helped me to refresh the metadata of a collection of 500 NFTs deployed in Polygon.

@TomiOhl
Copy link

TomiOhl commented May 19, 2023

Thanks! Just updated a bunch of tokens using this method.
I had to change a couple of things to make it work:

// ...
        nextWindow.addEventListener('load', async function () {
            // Interact with the dom on this new page
            kabob = nextWindow.document.getElementsByClassName('sc-29427738-0 sc-a1432d88-0 hbvxtu eUEzSV sc-9a637e94-1 epiQPq sc-ca43447a-1 bhQhMR')[1] // Changed this
            kabob.click()
            await timer(1000) // Added this additional wait to ensure the button is loaded
            refresh = nextWindow.document.getElementsByClassName('sc-b267fe84-0 cEtajt sc-29427738-0 sc-630fc9ab-0 sc-3c78d994-0 fMUbeR bNkKFC cFwPqI sc-fb098485-0 jGwQjm')[0] // Changed this
            refresh.click()
            console.log("Refreshed for " + i)
        }, false);
        nextWindow.blur();
        await timer(2000); // reduced this to 2 sec
// ...

@figs999
Copy link

figs999 commented Jun 12, 2023

This is what worked for me. I made it slightly less dependent on waiting arbitrary amounts of time and added a min/max range.

const timer = ms => new Promise(res => setTimeout(res, ms))

async function refresh_metadata(min, max) { // We need to wrap the loop into an async function for this to work
    var collectionUrl = window.location.href.split('/').slice(0, -1).join('/') + '/'
    var nextUrl = collectionUrl
    var nextWindow = null
    for (var i = min; i <= max; i++) {
        nextUrl = collectionUrl + i
        var initialTime = performance.now();
        nextWindow = window.open(nextUrl)
        nextWindow.addEventListener('load', async function () {
            // Interact with the dom on this new page
            await timer(100)
            kabob = nextWindow.document.getElementsByClassName('sc-29427738-0 sc-9ebaebf4-0 hbvxtu jdHdtm sc-dc8c4ccf-1 BmtmO sc-95996c10-1 kMXejK')[1]
            kabob.click()
            refresh = nextWindow.document.getElementsByClassName('sc-29427738-0 sc-bgqQcB sc-edb2493b-2 sc-97a347fc-2 bJHWdf kMdDzv')[0]
            while(!refresh) {
                await timer(100)
                refresh = nextWindow.document.getElementsByClassName('sc-29427738-0 sc-bgqQcB sc-edb2493b-2 sc-97a347fc-2 bJHWdf kMdDzv')[0]
            }
            refresh.click()
            nextWindow.close();
        }, false);
        nextWindow.blur();
        await new Promise(resolve => {
          nextWindow.addEventListener('unload', ev => {
              const delta = performance.now() - initialTime;
              const thresholdMs = 1000;
              if (delta < thresholdMs) return;
              resolve();
            });
        });
        var refreshed = "Refreshed for " + i
        console.log(refreshed )
    }
}

refresh_metadata(1, 10000)

@infinitelost
Copy link

Hello. this looks like just what I need but I can’t understand what it means to “Paste this script into the console on a specific NFT detail page for your collection.”

is this the CONSOLE when accessing developer-tools / web-inspector built in the browser??.. like when right-clicking any webpage in Google Chrome??

sorry, it sounds confusing, I’m not sure is that or to paste in Terminal in XCode.
anyway thanks and good work.

@TomiOhl
Copy link

TomiOhl commented Jul 27, 2023

is this the CONSOLE when accessing developer-tools / web-inspector built in the browser??.. like when right-clicking any webpage in Google Chrome??

Exactly. Just do that while an nft detail page is open since the script works with that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment