You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

157 lines
3.9 KiB

  1. ( function( window, document, href, pt_url ) {
  2. var encURI = window.encodeURIComponent,
  3. form = document.createElement( 'form' ),
  4. head = document.getElementsByTagName( 'head' )[0],
  5. target = '_press_this_app',
  6. canPost = true,
  7. windowWidth, windowHeight, selection,
  8. metas, links, content, images, iframes, img;
  9. if ( ! pt_url ) {
  10. return;
  11. }
  12. if ( href.match( /^https?:/ ) ) {
  13. pt_url += '&u=' + encURI( href );
  14. if ( href.match( /^https:/ ) && pt_url.match( /^http:/ ) ) {
  15. canPost = false;
  16. }
  17. } else {
  18. top.location.href = pt_url;
  19. return;
  20. }
  21. if ( window.getSelection ) {
  22. selection = window.getSelection() + '';
  23. } else if ( document.getSelection ) {
  24. selection = document.getSelection() + '';
  25. } else if ( document.selection ) {
  26. selection = document.selection.createRange().text || '';
  27. }
  28. pt_url += '&buster=' + ( new Date().getTime() );
  29. if ( ! canPost ) {
  30. if ( document.title ) {
  31. pt_url += '&t=' + encURI( document.title.substr( 0, 256 ) );
  32. }
  33. if ( selection ) {
  34. pt_url += '&s=' + encURI( selection.substr( 0, 512 ) );
  35. }
  36. }
  37. windowWidth = window.outerWidth || document.documentElement.clientWidth || 600;
  38. windowHeight = window.outerHeight || document.documentElement.clientHeight || 700;
  39. windowWidth = ( windowWidth < 800 || windowWidth > 5000 ) ? 600 : ( windowWidth * 0.7 );
  40. windowHeight = ( windowHeight < 800 || windowHeight > 3000 ) ? 700 : ( windowHeight * 0.9 );
  41. if ( ! canPost ) {
  42. window.open( pt_url, target, 'location,resizable,scrollbars,width=' + windowWidth + ',height=' + windowHeight );
  43. return;
  44. }
  45. function add( name, value ) {
  46. if ( typeof value === 'undefined' ) {
  47. return;
  48. }
  49. var input = document.createElement( 'input' );
  50. input.name = name;
  51. input.value = value;
  52. input.type = 'hidden';
  53. form.appendChild( input );
  54. }
  55. metas = head.getElementsByTagName( 'meta' ) || [];
  56. for ( var m = 0; m < metas.length; m++ ) {
  57. if ( m > 200 ) {
  58. break;
  59. }
  60. var q = metas[ m ],
  61. q_name = q.getAttribute( 'name' ),
  62. q_prop = q.getAttribute( 'property' ),
  63. q_cont = q.getAttribute( 'content' );
  64. if ( q_cont ) {
  65. if ( q_name ) {
  66. add( '_meta[' + q_name + ']', q_cont );
  67. } else if ( q_prop ) {
  68. add( '_meta[' + q_prop + ']', q_cont );
  69. }
  70. }
  71. }
  72. links = head.getElementsByTagName( 'link' ) || [];
  73. for ( var y = 0; y < links.length; y++ ) {
  74. if ( y >= 50 ) {
  75. break;
  76. }
  77. var g = links[ y ],
  78. g_rel = g.getAttribute( 'rel' );
  79. if ( g_rel === 'canonical' || g_rel === 'icon' || g_rel === 'shortlink' ) {
  80. add( '_links[' + g_rel + ']', g.getAttribute( 'href' ) );
  81. }
  82. }
  83. if ( document.body.getElementsByClassName ) {
  84. content = document.body.getElementsByClassName( 'hfeed' )[0];
  85. }
  86. content = document.getElementById( 'content' ) || content || document.body;
  87. images = content.getElementsByTagName( 'img' ) || [];
  88. for ( var n = 0; n < images.length; n++ ) {
  89. if ( n >= 100 ) {
  90. break;
  91. }
  92. img = images[ n ];
  93. // If we know the image width and/or height, check them now and drop the "uninteresting" images.
  94. if ( img.src.indexOf( 'avatar' ) > -1 || img.className.indexOf( 'avatar' ) > -1 ||
  95. ( img.width && img.width < 256 ) || ( img.height && img.height < 128 ) ) {
  96. continue;
  97. }
  98. add( '_images[]', img.src );
  99. }
  100. iframes = document.body.getElementsByTagName( 'iframe' ) || [];
  101. for ( var p = 0; p < iframes.length; p++ ) {
  102. if ( p >= 50 ) {
  103. break;
  104. }
  105. add( '_embeds[]', iframes[ p ].src );
  106. }
  107. if ( document.title ) {
  108. add( 't', document.title );
  109. }
  110. if ( selection ) {
  111. add( 's', selection );
  112. }
  113. form.setAttribute( 'method', 'POST' );
  114. form.setAttribute( 'action', pt_url );
  115. form.setAttribute( 'target', target );
  116. form.setAttribute( 'style', 'display: none;' );
  117. window.open( 'about:blank', target, 'location,resizable,scrollbars,width=' + windowWidth + ',height=' + windowHeight );
  118. document.body.appendChild( form );
  119. form.submit();
  120. } )( window, document, top.location.href, window.pt_url );