You cannot import the code, you’d have to port it to JavaScript and to use Tiled’s API for reading/writing map data. The logic concerning the .map file itself should be similar, the biggest differences will be in interacting with Tiled.
Most of the information you need can be found in Tiled’s scripting documentation: https://doc.mapeditor.org/en/stable/reference/scripting/
Some additional scripts you can learn from (including one export script) can be found in the tiled-extensions repo: https://github.com/mapeditor/tiled-extensions