packages.flutter icon indicating copy to clipboard operation
packages.flutter copied to clipboard

web script loading fix

Open abdelaziz-mahdy opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe. fixing the pdf.js not loading for some people due to The https://cdn.jsdelivr.net/ domain is blocked in a number of countries including Egypt and China. It has been blocked in China since about May 2022 and in Egypt since about June 2023. You have to either use your files locally or search other alternatives like UNPKG or cdnjs

Describe the solution you'd like i am using the script to try to load pdf.js from two places if one fails

Describe alternatives you've considered

  <!-- pdf -->
  <script>
    function loadScript(url, fallbackUrl, onSuccess, onError) {
      var script = document.createElement('script');
      script.src = url;
      script.onload = onSuccess;
      script.onerror = function () {
        console.error('Failed to load script: ' + url);
        if (fallbackUrl) {
          console.log('Trying fallback URL: ' + fallbackUrl);
          loadScript(fallbackUrl, null, onSuccess, onError);
        } else {
          onError();
        }
      };
      document.head.appendChild(script);
    }

    function loadPDFJS() {
      pdfjsLib.GlobalWorkerOptions.workerSrc = "https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.worker.min.js";
      pdfRenderOptions = {
        cMapUrl: 'https://cdn.jsdelivr.net/npm/[email protected]/cmaps/',
        cMapPacked: true,
      };
      // console.log('Loaded PDF.js from ');

    }

    // Try the primary URL first
    loadScript('https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.js', 'https://unpkg.com/[email protected]/build/pdf.js', loadPDFJS, function () {
      console.error('Failed to load PDF.js');
      // Handle error condition, e.g., show an error message to the user
    });
  </script>

Additional context hope it helps, and if any thing that needs to be updated let me know (the script was created by chatgpt since i dont understand much in javascript) but it worked in my case

abdelaziz-mahdy avatar Jul 17 '23 08:07 abdelaziz-mahdy