]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/bookmarklet.js
WordPress 4.2.2-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         if ( href.match( /\/\/(www|m)\.youtube\.com\/watch/ ) ||
70                 href.match( /\/\/vimeo\.com\/(.+\/)?([\d]+)$/ ) ||
71                 href.match( /\/\/(www\.)?dailymotion\.com\/video\/.+$/ ) ||
72                 href.match( /\/\/soundcloud\.com\/.+$/ ) ||
73                 href.match( /\/\/twitter\.com\/[^\/]+\/status\/[\d]+$/ ) ||
74                 href.match( /\/\/vine\.co\/v\/[^\/]+/ ) ) {
75
76                 add( '_embeds[]', href );
77         }
78
79         metas = head.getElementsByTagName( 'meta' ) || [];
80
81         for ( var m = 0; m < metas.length; m++ ) {
82                 if ( m > 200 ) {
83                         break;
84                 }
85
86                 var q = metas[ m ],
87                         q_name = q.getAttribute( 'name' ),
88                         q_prop = q.getAttribute( 'property' ),
89                         q_cont = q.getAttribute( 'content' );
90
91                 if ( q_cont ) {
92                         if ( q_name ) {
93                                 add( '_meta[' + q_name + ']', q_cont );
94                         } else if ( q_prop ) {
95                                 add( '_meta[' + q_prop + ']', q_cont );
96                         }
97                 }
98         }
99
100         links = head.getElementsByTagName( 'link' ) || [];
101
102         for ( var y = 0; y < links.length; y++ ) {
103                 if ( y >= 50 ) {
104                         break;
105                 }
106
107                 var g = links[ y ],
108                         g_rel = g.getAttribute( 'rel' );
109
110                 if ( g_rel === 'canonical' || g_rel === 'icon' || g_rel === 'shortlink' ) {
111                         add( '_links[' + g_rel + ']', g.getAttribute( 'href' ) );
112                 }
113         }
114
115         if ( document.body.getElementsByClassName ) {
116                 content = document.body.getElementsByClassName( 'hfeed' )[0];
117         }
118
119         content = document.getElementById( 'content' ) || content || document.body;
120         images = content.getElementsByTagName( 'img' ) || [];
121
122         for ( var n = 0; n < images.length; n++ ) {
123                 if ( n >= 100 ) {
124                         break;
125                 }
126
127                 img = images[ n ];
128
129                 // If we know the image width and/or height, check them now and drop the "uninteresting" images.
130                 if ( img.src.indexOf( 'avatar' ) > -1 || img.className.indexOf( 'avatar' ) > -1 ||
131                         ( img.width && img.width < 256 ) || ( img.height && img.height < 128 ) ) {
132
133                         continue;
134                 }
135
136                 add( '_images[]', img.src );
137         }
138
139         iframes = document.body.getElementsByTagName( 'iframe' ) || [];
140
141         for ( var p = 0; p < iframes.length; p++ ) {
142                 if ( p >= 50 ) {
143                         break;
144                 }
145
146                 add( '_embeds[]', iframes[ p ].src );
147         }
148
149         if ( document.title ) {
150                 add( 't', document.title );
151         }
152
153         if ( selection ) {
154                 add( 's', selection );
155         }
156
157         form.setAttribute( 'method', 'POST' );
158         form.setAttribute( 'action', pt_url );
159         form.setAttribute( 'target', target );
160         form.setAttribute( 'style', 'display: none;' );
161
162         window.open( 'about:blank', target, 'location,resizable,scrollbars,width=' + windowWidth + ',height=' + windowHeight );
163
164         document.body.appendChild( form );
165         form.submit();
166 } )( window, document, top.location.href, window.pt_url );