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