Skip to content

Commit

Permalink
Parse more on the frontend with JS
Browse files Browse the repository at this point in the history
  • Loading branch information
kraftbj committed Nov 27, 2017
1 parent 54ab3d4 commit 0f7e59d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 230 deletions.
139 changes: 6 additions & 133 deletions assets/bookmarklet.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
( function( window, document, href, pt_url ) {
var encURI = window.encodeURIComponent,
form = document.createElement( 'form' ),
head = document.getElementsByTagName( 'head' )[0],
target = '_press_this_app',
canPost = true,
windowWidth, windowHeight, selection,
metas, links, content, images, iframes, img;

selection;
if ( ! pt_url ) {
return;
}

if ( href.match( /^https?:/ ) ) {
if ( /^https?:/.test( href ) ) {
pt_url += '&u=' + encURI( href );
if ( href.match( /^https:/ ) && pt_url.match( /^http:/ ) ) {
canPost = false;
}
} else {
top.location.href = pt_url;
return;
}

if ( window.getSelection ) {
Expand All @@ -29,128 +17,13 @@
selection = document.selection.createRange().text || '';
}

pt_url += '&buster=' + ( new Date().getTime() );

if ( ! canPost ) {
if ( document.title ) {
pt_url += '&t=' + encURI( document.title.substr( 0, 256 ) );
}

if ( selection ) {
pt_url += '&s=' + encURI( selection.substr( 0, 512 ) );
}
}

windowWidth = window.outerWidth || document.documentElement.clientWidth || 600;
windowHeight = window.outerHeight || document.documentElement.clientHeight || 700;

windowWidth = ( windowWidth < 800 || windowWidth > 5000 ) ? 600 : ( windowWidth * 0.7 );
windowHeight = ( windowHeight < 800 || windowHeight > 3000 ) ? 700 : ( windowHeight * 0.9 );

if ( ! canPost ) {
window.open( pt_url, target, 'location,resizable,scrollbars,width=' + windowWidth + ',height=' + windowHeight );
return;
}

function add( name, value ) {
if ( typeof value === 'undefined' ) {
return;
}

var input = document.createElement( 'input' );

input.name = name;
input.value = value;
input.type = 'hidden';

form.appendChild( input );
}

metas = head.getElementsByTagName( 'meta' ) || [];

for ( var m = 0; m < metas.length; m++ ) {
if ( m > 200 ) {
break;
}

var q = metas[ m ],
q_name = q.getAttribute( 'name' ),
q_prop = q.getAttribute( 'property' ),
q_cont = q.getAttribute( 'content' );

if ( q_cont ) {
if ( q_name ) {
add( '_meta[' + q_name + ']', q_cont );
} else if ( q_prop ) {
add( '_meta[' + q_prop + ']', q_cont );
}
}
}

links = head.getElementsByTagName( 'link' ) || [];

for ( var y = 0; y < links.length; y++ ) {
if ( y >= 50 ) {
break;
}

var g = links[ y ],
g_rel = g.getAttribute( 'rel' );

if ( g_rel === 'canonical' || g_rel === 'icon' || g_rel === 'shortlink' ) {
add( '_links[' + g_rel + ']', g.getAttribute( 'href' ) );
}
}

if ( document.body.getElementsByClassName ) {
content = document.body.getElementsByClassName( 'hfeed' )[0];
}

content = document.getElementById( 'content' ) || content || document.body;
images = content.getElementsByTagName( 'img' ) || [];

for ( var n = 0; n < images.length; n++ ) {
if ( n >= 100 ) {
break;
}

img = images[ n ];

// If we know the image width and/or height, check them now and drop the "uninteresting" images.
if ( img.src.indexOf( 'avatar' ) > -1 || img.className.indexOf( 'avatar' ) > -1 ||
( img.width && img.width < 256 ) || ( img.height && img.height < 128 ) ) {

continue;
}

add( '_images[]', img.src );
}

iframes = document.body.getElementsByTagName( 'iframe' ) || [];

for ( var p = 0; p < iframes.length; p++ ) {
if ( p >= 50 ) {
break;
}

add( '_embeds[]', iframes[ p ].src );
}

if ( document.title ) {
add( 't', document.title );
pt_url += '&t=' + encURI( document.title.substr( 0, 256 ) );
}

if ( selection ) {
add( 's', selection );
pt_url += '&s=' + encURI( selection.substr( 0, 512 ) );
}

form.setAttribute( 'method', 'POST' );
form.setAttribute( 'action', pt_url );
form.setAttribute( 'target', target );
form.setAttribute( 'style', 'display: none;' );

window.open( 'about:blank', target, 'location,resizable,scrollbars,width=' + windowWidth + ',height=' + windowHeight );

document.body.appendChild( form );
form.submit();
} )( window, document, top.location.href, window.pt_url );
top.location.href = pt_url + '&' + ( new Date().getTime() );
} )( window, document, top.location.href, url );
1 change: 0 additions & 1 deletion assets/bookmarklet.min.js

This file was deleted.

78 changes: 17 additions & 61 deletions class-wp-press-this-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/
class WP_Press_This_Plugin {
// Used to trigger the bookmarklet update notice.
const VERSION = 8;
public $version = 8;
const VERSION = 9;
public $version = 9;

private $images = array();

Expand Down Expand Up @@ -671,9 +671,7 @@ public function merge_or_fetch_data() {

// Only instantiate the keys we want. Sanity check and sanitize each one.
foreach ( array( 'u', 's', 't', 'v' ) as $key ) {
if ( ! empty( $_POST[ $key ] ) ) {
$value = wp_unslash( $_POST[ $key ] );
} else if ( ! empty( $_GET[ $key ] ) ) {
if ( ! empty( $_GET[ $key ] ) ) {
$value = wp_unslash( $_GET[ $key ] );
} else {
continue;
Expand Down Expand Up @@ -706,61 +704,8 @@ public function merge_or_fetch_data() {
* If no title, _images, _embed, and _meta was passed via $_POST, fetch data from source as fallback,
* making PT fully backward compatible with the older bookmarklet.
*/
if ( empty( $_POST ) && ! empty( $data['u'] ) ) {
if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'scan-site' ) ) {
$data = $this->source_data_fetch_fallback( $data['u'], $data );
} else {
$data['errors'] = 'missing nonce';
}
} else {
foreach ( array( '_images', '_embeds' ) as $type ) {
if ( empty( $_POST[ $type ] ) ) {
continue;
}

$data[ $type ] = array();
$items = $this->_limit_array( $_POST[ $type ] );

foreach ( $items as $key => $value ) {
if ( $type === '_images' ) {
$value = $this->_limit_img( wp_unslash( $value ) );
} else {
$value = $this->_limit_embed( wp_unslash( $value ) );
}

if ( ! empty( $value ) ) {
$data[ $type ][] = $value;
}
}
}

foreach ( array( '_meta', '_links' ) as $type ) {
if ( empty( $_POST[ $type ] ) ) {
continue;
}

$data[ $type ] = array();
$items = $this->_limit_array( $_POST[ $type ] );

foreach ( $items as $key => $value ) {
// Sanity check. These are associative arrays, $key is usually things like 'title', 'description', 'keywords', etc.
if ( empty( $key ) || strlen( $key ) > 100 ) {
continue;
}

if ( $type === '_meta' ) {
$value = $this->_limit_string( wp_unslash( $value ) );

if ( ! empty( $value ) ) {
$data = $this->_process_meta_entry( $key, $value, $data );
}
} else {
if ( in_array( $key, array( 'canonical', 'shortlink', 'icon' ), true ) ) {
$data[ $type ][ $key ] = $this->_limit_url( wp_unslash( $value ) );
}
}
}
}
if ( ! empty( $data['u'] ) ) {
$data = $this->source_data_fetch_fallback( $data['u'], $data );
}

// Support passing a single image src as `i`
Expand Down Expand Up @@ -1214,7 +1159,7 @@ public function html() {

$wp_version = get_bloginfo( 'version' );

// Get data, new (POST) and old (GET).
// Get data
$data = $this->merge_or_fetch_data();

$post_title = $this->get_suggested_title( $data );
Expand Down Expand Up @@ -1399,6 +1344,17 @@ public function html() {

<div class="wrapper">
<div class="editor-wrapper">
<div class="alerts" role="alert" aria-live="assertive" aria-relevant="all" aria-atomic="true">
<?php
if ( isset( $data['v'] ) && $this->version > $data['v'] ) {
?>
<p class="alert is-notice">
<?php printf( __( 'You should upgrade <a href="%s" target="_blank">your bookmarklet</a> to the latest version!' ), admin_url( 'tools.php' ) ); ?>
</p>
<?php
}
?>
</div>
<div id="app-container" class="editor">
<span id="title-container-label" class="post-title-placeholder" aria-hidden="true"><?php _e( 'Post title', 'press-this' ); ?></span>
<h2 id="title-container" class="post-title" contenteditable="true" spellcheck="true" aria-label="<?php esc_attr_e( 'Post title', 'press-this' ); ?>" tabindex="0"><?php echo esc_html( $post_title ); ?></h2>
Expand Down
45 changes: 10 additions & 35 deletions press-this-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function press_this_tool_box() {
$this.attr( 'aria-expanded', $this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
});

// Select Press This code when focusing (tabbing) or clicking the textarea.
// Select Press This code when focusing (tabbing) or clicking the textarea.
$pressthisCode.on( 'click focus', function() {
var self = this;
setTimeout( function() { self.select(); }, 50 );
Expand All @@ -114,45 +114,20 @@ function press_this_tool_box() {
* Retrieves the Press This bookmarklet link.
*
* @since Core/2.6.0
* @since 1.1.0 Bookmarklet code directly in this function.
*
* @global bool $is_IE Whether the browser matches an Internet Explorer user agent.
*/
function press_this_get_shortcut_link() {
global $is_IE;

include( plugin_dir_path( __FILE__ ) . 'class-wp-press-this-plugin.php' );

$link = '';

if ( $is_IE ) {
/*
* Return the old/shorter bookmarklet code for MSIE 8 and lower,
* since they only support a max length of ~2000 characters for
* bookmark[let] URLs, which is way to small for our smarter one.
* Do update the version number so users do not get the "upgrade your
* bookmarklet" notice when using PT in those browsers.
*/
$ua = $_SERVER['HTTP_USER_AGENT'];

if ( ! empty( $ua ) && preg_match( '/\bMSIE (\d)/', $ua, $matches ) && (int) $matches[1] <= 8 ) {
$url = wp_json_encode( admin_url( 'press-this.php' ) );

$link = 'javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,' .
's=(e?e():(k)?k():(x?x.createRange().text:0)),f=' . $url . ',l=d.location,e=encodeURIComponent,' .
'u=f+"?u="+e(l.href)+"&t="+e(d.title)+"&s="+e(s)+"&v=' . WP_Press_This_Plugin::VERSION . '";' .
'a=function(){if(!w.open(u,"t","toolbar=0,resizable=1,scrollbars=1,status=1,width=600,height=700"))l.href=u;};' .
'if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else a();void(0)';
}
}

if ( empty( $link ) ) {
$src = @file_get_contents( plugin_dir_path( __FILE__ ) . 'assets/bookmarklet.min.js' );

if ( $src ) {
$url = wp_json_encode( admin_url( 'press-this.php' ) . '?v=' . WP_Press_This_Plugin::VERSION );
$link = 'javascript:' . str_replace( 'window.pt_url', $url, $src );
}
}
$url = wp_json_encode( admin_url( 'press-this.php' ) . '?v=' . WP_Press_This_Plugin::VERSION );

// Source can be found in assets/bookmarket.js
$link = 'javascript:(function(a,b,c,d){var e,f=a.encodeURIComponent;d&&(/^https?:/.test(c)&&
(d+="&u="+f(c)),a.getSelection?e=a.getSelection()+"":b.getSelection?e=b.getSelection()+"":
b.selection&&(e=b.selection.createRange().text||""),b.title&&(d+="&t="+f(b.title.substr(0,256))),
e&&(d+="&s="+f(e.substr(0,512))),top.location.href=d+"&"+(new Date).getTime())})
(window,document,top.location.href,' . $url . ');';

$link = str_replace( array( "\r", "\n", "\t" ), '', $link );

Expand Down

0 comments on commit 0f7e59d

Please sign in to comment.