sdk.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import Deferred from './Deferred'
  2. let __sdk = null
  3. let __doc = null
  4. let __mount_list = []
  5. const deferred = Deferred()
  6. const unloadHandler = function() {
  7. __sdk = null
  8. __doc = null
  9. }
  10. export const sdk = deferred.promise()
  11. export const sdk_mounted = (cb) => {
  12. if (__sdk) {
  13. cb(__sdk)
  14. } else {
  15. __mount_list.push(cb)
  16. }
  17. }
  18. export const sdk_unmounted = ()=>[
  19. unloadHandler()
  20. ]
  21. export function setupSDK(iframe) {
  22. __sdk = iframe.contentWindow.__sdk
  23. __doc = iframe.contentDocument
  24. iframe.contentWindow.removeEventListener("unload", unloadHandler);
  25. iframe.contentWindow.addEventListener("unload", unloadHandler);
  26. let cb = null
  27. while((cb = __mount_list.shift()) && cb) {
  28. cb(__sdk)
  29. }
  30. deferred.resolveValue(__sdk)
  31. }
  32. export function insertStyle(style) {
  33. __doc.body.insertAdjacentHTML('beforeend', style)
  34. }
  35. export function clearScreen(clear,retry=0) {
  36. if (__doc) {
  37. let button = __doc.body.querySelector('.clear-screen-btn')
  38. if (button == void 0) {
  39. if(retry > 50) {
  40. return
  41. }
  42. return setTimeout(() => {
  43. clearScreen(clear,++retry)
  44. }, 200);
  45. }
  46. if (clear) {
  47. if (button.querySelector('i').classList.contains('icon-hide_y')) {
  48. button.style.visibility = 'hidden'
  49. button.click()
  50. }
  51. } else {
  52. button.style.visibility = 'visible'
  53. if (button.querySelector('i').classList.contains('icon-hide_n')) {
  54. button.click()
  55. }
  56. }
  57. }
  58. }
  59. // sdk_mounted(() => {
  60. // insertStyle(
  61. // `<style>
  62. // .kankan-app__split .player[name='main'] {
  63. // width:100% !important;
  64. // }
  65. // .kankan-app__split .player[name='copy'] {
  66. // display:none !important;
  67. // }
  68. // </style>`
  69. // )
  70. // })