Get archive link from the Wayback Machine

Please note: The tweaked version of this extension is available from the extension directory: Wayback Machine — PopClip Extensions

The following extension checks if an archived version of a website is available from the Wayback Machine (Wikipedia: Wayback Machine) which is run by the Internet Archive.

It copies the URL of the most recent archived version to the clipboard. The extension calls the Wayback Machine API, which is faster than other methods.

If the output URL is also to be displayed by PopClip, remove // before popclip.showText.

When holding the option key while envoking the extension, the returned URL is also opened in the browser. This makes it easier to check whether the archive version contains the desired content.

Rarely does the API incorrectly state that there is no archive version. In this case, this should be checked via Wayback Machine website.

Input:

https://www.popclip.app/

Output:

https://web.archive.org/web/20240515192607/https://www.popclip.app/

Extension:

// #popclip
// name: Wayback Machine
// icon: iconify:simple-icons:internetarchive
// language: javascript
// entitlements: [network]
// after: show-status

const axios = require('axios');

// extract and encode URL from popclip.input.text

const targetURL = encodeURIComponent(popclip.input.text);

// Wayback Machine APIs: https://archive.org/help/wayback_api.php
// define the Wayback Machine API URL with the target URL as query parameter

const waybackMachineApiUrl = `https://archive.org/wayback/available?url=${targetURL}`;

// check Wayback Machine for the URL

async function checkWaybackMachine(url) {
  try {
    // GET request to the Wayback Machine API
    const response = await axios.get(url);
    const { archived_snapshots } = response.data;

    // check if a closest is snapshot available
    
    if (archived_snapshots.closest && archived_snapshots.closest.available) {

      // if snapshot available, show success message, copy URL to clipboard
      
      popclip.showSuccess()
      popclip.copyText(archived_snapshots.closest.url);
      
      // if snapshot available, show its details
      // popclip.showText(`Snapshot found: ${archived_snapshots.closest.url}`);

      // open archive URL in browser when option key is pressed
      if (popclip.modifiers.option) {
        popclip.openUrl(archived_snapshots.closest.url)
        }

    } else {
      popclip.showText('No snapshot available for this URL.');
    }
  } catch (error) {
    popclip.showText(`Error: ${error.message}`);
  }
}

// Call the function with the Wayback Machine API URL
checkWaybackMachine(waybackMachineApiUrl);

(The above block is an extension snippet – select it to install the extension with PopClip)

3 Likes

Beautiful!

May I add this to the Extensions Directory?

Of course :smiley:

1 Like

Thank you. Here is is: Wayback Machine — PopClip Extensions

I tweaked it slightly to require a url as input using the requirements key.

1 Like

I used it to find this :smiley:

2 Likes

Hi, this release doesn’t work for me:

1 Like

Whoops! Try downloading again now (version 148)

2 Likes

Yess fantastic, thanks!

1 Like

The extension is currently not working due to a DDOS attack on the Internet Archive.

I hope it wasn’t all the PopClip users :wink:

1 Like