1. --!movie
  2. --!encoding=utf-8
  3.  
  4. global $
  5.  
  6. global gChromium
  7.  
  8. ----------------------------------------
  9. -- encryption key
  10. ----------------------------------------
  11. global pwd -- used to decrypt scripts and PDF files encrypted with AES-256 encryption
  12.  
  13. ----------------------------------------
  14. --
  15. ----------------------------------------
  16. on startMovie
  17.     _player.debugPlaybackEnabled = 1
  18.  
  19.     ----------------------------------------
  20.     -- CONFIG
  21.     ----------------------------------------
  22.     pwd = "(!O0S9QiMfL$_*ltV(0M841JCxm*J6qV"
  23.  
  24.     -- libs
  25.     $.import("aes")
  26.     $.import("base64")
  27.     $.import("file")
  28.     $.import("string")
  29.  
  30.     -- window settings
  31.     _movie.stage.title = "Chromium Xtra Demo: Encrypted PDF"
  32.     _movie.stage.titlebarOptions.visible = 1
  33.     _movie.stage.titlebarOptions.maximizebox = 1
  34.     _movie.stage.rect = rect(0,0,1200,840)
  35.     _movie.stage.bgcolor = rgb(255,255,255)
  36.     _movie.centerStage = 1
  37.     _movie.stage.resizable = 1
  38.     _movie.puppetTempo(30)
  39.     _movie.stage.visible = 1
  40.  
  41.     cefInit(["log_severity":99]) -- disable creation of debug.log
  42.  
  43.     -- create browser window in stage window that shows the PDF
  44.     r = rect(0, 0, _movie.stage.rect.width, _movie.stage.rect.height)
  45.  
  46.     p = $.string.replace("\", "/", $.PATH)
  47.     u = "file:///"&p&"resources/pdf/pdf.htm"
  48.     gChromium = xtra("Chromium").new(u, r, 1)
  49.     gChromium.browserSetAutoResize(TRUE)
  50.  
  51.     -- for debugging only
  52.     --gChromium.browserShowDevTools()
  53. end
  54.  
  55. ----------------------------------------
  56. --
  57. ----------------------------------------
  58. on stopMovie (me)
  59.     if objectP(gChromium) then
  60.         gChromium.browserClose()
  61.         -- give browser some time to shutdown
  62.         Sleep(1000)
  63.         gChromium = VOID
  64.     end if
  65. end
  66.  
  67. ----------------------------------------
  68. --
  69. ----------------------------------------
  70. on loadEncryptedPDF (fn)
  71.     data = $.file.getBytes(fn)
  72.     data = $.aes.decryptByteArray(data, pwd)
  73.     js = "loadPDF(atob('"&$.base64.encode(data)&"'));"
  74.     gChromium.browserExecuteJavaScript(js, "js")
  75. end
  76.  
  77. ----------------------------------------
  78. -- Loads JS script synchronously
  79. ----------------------------------------
  80. on loadEncryptedScriptSync (fn)
  81.     data = $.file.getBytes(fn)
  82.     data = $.aes.decryptByteArray(data, pwd)
  83.     js = ""
  84.     put "var scr = document.createElement('script');" after js
  85.     put "var txt = document.createTextNode(atob('"&$.base64.encode(data)&"'));" after js
  86.     put "scr.appendChild(txt);" after js
  87.     put "document.getElementsByTagName('head')[0].appendChild(scr);" after js
  88.     gChromium.browserExecuteJavaScript(js, "js")
  89. end
  90.  
  91. ----------------------------------------
  92. -- Loads JS script asynchronously.
  93. -- Lingo code <callbackCode> is executed when script was loaded into DOM.
  94. ----------------------------------------
  95. --on loadEncryptedScriptAsync (fn, callbackCode)
  96. --  data = $.file.getBytes(fn)
  97. --  data = $.aes.decryptByteArray(data, pwd)
  98. --  uri = "data:text/javascript;base64," & $.base64.encode(data)
  99. --  js = ""
  100. --  put "var scr = document.createElement('script');" after js
  101. --  put "scr.setAttribute('src', '"&uri&"');" after js
  102. --  put "scr.onload=function(){" after js
  103. --  put "   window.cefQuery({request:'"&callbackCode&"'});" after js
  104. --  put "};" after js
  105. --  put "document.getElementsByTagName('head')[0].appendChild(scr);" after js
  106. --  gChromium.browserExecuteJavaScript(js, "js")
  107. --end
  108.  
  109. --**************************************
  110. -- RAW CALLBACKS - NOT THREAD-SAFE!
  111. --**************************************
  112.  
  113. ----------------------------------------
  114. -- @callback
  115. ----------------------------------------
  116. on OnLoadEnd (aHttpStatusCode)
  117.     -- The dummy page was loaded.
  118.     -- Now completely reset the HTML code, in case a user messed with it
  119.     js = ""
  120.     put "document.write();" after js
  121.     put "var h=document.createElement('html');" after js
  122.     put "h.innerHTML='<head></head><body><canvas id=\'canvas\'></canvas></body>';" after js
  123.     put "document.appendChild(h);" after js
  124.     gChromium.browserExecuteJavaScript(js, "js")
  125.  
  126.     -- Load the encrypted pdf.js code (synchronously)
  127.     loadEncryptedScriptSync($.PATH&"resources\pdf\pdf.min.js.enc")
  128.  
  129.     -- Execute (encrypted) viewer script code
  130.     data = $.file.getBytes($.PATH&"resources\pdf\viewer.js.enc")
  131.     data = $.aes.decryptByteArray(data, pwd)
  132.     js = data.readRawString(data.length)
  133.     gChromium.browserExecuteJavaScript(js, "js")
  134.  
  135.     -- Auto-load an encrypted PDF file
  136.     loadEncryptedPDF($.PATH&"media\test.pdf.enc")
  137. end
  138.  
  139. ----------------------------------------
  140. -- @callback
  141. ----------------------------------------
  142. on OnConsoleMessage (aLevel, aMsg)
  143.     put ["[DEBUG] ","[INFO] ","[WARNING] ","[ERROR] "][aLevel] & aMsg
  144. end
  145.  
  146. ----------------------------------------
  147. -- @callback
  148. ----------------------------------------
  149. on OnBeforeContextMenu
  150.     --return TRUE -- no context menu
  151.  
  152.     -- custom context menu for navigation in PDF
  153.     return ["", "Next Page", "Previous Page", "-", "First Page", "Last Page", "-", "Zoom In", "Zoom Out"]
  154. end
  155.  
  156. ----------------------------------------
  157. -- @callback
  158. ----------------------------------------
  159. on OnContextMenuCommand (aCommandID)
  160.     case (aCommandID-26500) of
  161.     2: gChromium.browserExecuteJavaScript("onNextPage();", "js")
  162.     3: gChromium.browserExecuteJavaScript("onPrevPage();", "js")
  163.     5: gChromium.browserExecuteJavaScript("onGotoPage(1);", "js")
  164.     6: gChromium.browserExecuteJavaScript("onGotoPage(pdfDoc.numPages);", "js")
  165.     8: gChromium.browserExecuteJavaScript("scale=scale*1.1;onGotoPage(pageNum);", "js")
  166.     9: gChromium.browserExecuteJavaScript("scale=scale/1.1;onGotoPage(pageNum);", "js")
  167.     end case
  168. end
  169.  
[raw code]