WordPressのカスタマイズ記事ばかりになってしまっているが、そこそこの使い勝手を確保しなければ、その先に進めないと言い訳しつつ・・・
懸案だったmoreの先読み(onLoad)対応は、すでに完了しているが、そのハック方法をまとめる。
—
2007.05.04
タグをサニタイズしていなかったので、正しく表示されていなかった箇所を修正しました。
—
◆ポイント
http_requestの戻り(取得したmoreコンテンツ)にはIDが存在しないため、複数コネクションの交通整理が必要。
→ 配列化する。
101 http_request = new Array();
対象記事がhttp_request中なら、多重に処理しない。(無意味かつサーバ負荷)
154 if( ! http_request[id] ) {
onLoadでhttp_request中に「続きを読む」クリックされたら、コンテンツ取得後に表示する。
191 if( openStat[postID] != true ) {
※この判定を入れないと、コンテンツ取得後に続きがクローズされてしまう。
記事ページ以外(?p=以外のURL)ではonLoad時にhttp_requestする。
19 if ( isset( $_GET[‘p’] ) ) {
20 echo ‘<body>’;
21 } else {
22 echo ‘<body onLoad=”getOpenClose();”>’;
23 }
※記事ページでは当該Ajaxコードが存在しない(作りの)ため、getOpenClose() が存在しないエラーとなる。
◆wp-content/plugins/ajaxified-expand-post-now.php
101行目付近
すでにmoreのキャッシュ化が適用済みだが、フラグ管理とhttp_request配列化で全体的に再ハックしている。
101 http_request = new Array();
102 function makeRequest(url, loadingFunc, doneFunc) {
103 http_request[url] = false;
104
105 if (window.XMLHttpRequest) {
106 http_request[url] = new XMLHttpRequest();
107 if (http_request[url].overrideMimeType) {
108 http_request[url].overrideMimeType('text/xml');
109 }
110 }
111 else if (window.ActiveXObject) {
112 try {
113 http_request[url] = new ActiveXObject('Msxml2.XMLHTTP');
114 } catch (e) {
115 try {
116 http_request[url] = new ActiveXObject('Microsoft.XMLHTTP');
117 } catch (e) {
118 }
119 }
120 }
121
122 if (!http_request[url]) {
123 alert('FATAL ERROR: Cannot create an XMLHTTP instance.');
124 return false;
125 }
126
127 http_request[url].onreadystatechange = function() {
128 if (http_request[url].readyState == 4) {
129 if (http_request[url].status == 200) {
130 // eval(doneFunc+'(http_request.responseText)');
131 eval(doneFunc+'(http_request[url].responseText,url)');
132 } else {
133 alert('Error while connecting to the server. Please try again leter.');
134 }
135 } else {
136 if (this.isOpera||!document.all||!navigator.userAgent.match(/msie/gi)) {
137 eval(loadingFunc+'(http_request[url].responseText)');
138 }
139 }
140 }
141
142 try {
143 http_request[url].open('POST', '$aepnPluginLocate', true);
144 http_request[url].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
145 http_request[url].send('p='+url);
146 } catch (e) {
147 alert('I have encountered an problem while fetching data due to the cross-domain restriction of the XMLHTTPRequest object.');
148 }
149 }
150
151 function getData(id) {
152 postID = 'p'+id;
153 openStat[postID] = true;
154 if( ! http_request[id] ) {
155 makeRequest(id, 'fetchLoading', 'fetchDone');
156 }
157 gE('viewBox-'+postID).style.display = 'block';
158 gE('open-'+postID).style.display = 'none';
159 gE('close-'+postID).style.display = 'inline';
160 }
161
162 moreCount = 0;
163 function getDataOnLoad(id) {
164 moreCount++;
165 postID = 'p'+id;
166 makeRequest(id, 'fetchLoadingOnLoad', 'fetchDoneOnLoad');
167 }
168
169 cacheMore = new Array();
170 openStat = new Array();
171
172 function fetchLoading() {
173 if( ! cacheMore[ 'viewBox-'+postID ] ) {
174 wH(gE('viewBox-'+postID), '$aepnLoadingMsg');
175 }
176 }
177
178 function fetchLoadingOnLoad() {
179 // moreCount++;
180 }
181
182 function fetchDone(outcome) {
183 if( ! cacheMore[ 'viewBox-'+postID ] ) {
184 wH(gE('viewBox-'+postID), outcome);
185 cacheMore[ 'viewBox-'+postID ] = outcome;
186 }
187 }
188
189 function fetchDoneOnLoad(outcome,id) {
190 postID = 'p'+id;
191 if( openStat[postID] != true ) {
192 gE('viewBox-'+postID).style.display = 'none';
193 } else {
194 gE('viewBox-'+postID).style.display = 'block';
195 }
196 wH(gE('viewBox-'+postID), outcome);
197 cacheMore[ 'viewBox-'+postID ] = outcome;
198 moreCount--;
199 if( moreCount == 0 ) {
200 window.status = "'Read More' data is Preloaded !!";
201 }
202 }
203
204 function closeView(id) {
205 gE('viewBox-'+id).style.display = 'none';
206 gE('open-'+id).style.display = 'inline';
207 gE('close-'+id).style.display = 'none';
208 openStat[postID] = false;
209 }
210
211 function getElements(tag, attr)
212 {
213 var ary = new Array;
214
215 var oElements = document.getElementsByTagName(tag);
216 for(var i = 0; i < oElements.length; i++){
217 if( oElements[i][attr] ) {
218 ary.push(oElements[i][attr]);
219 }
220 }
221 return ary;
222 }
223
224 function getOpenClose() {
225 var e = getElements("A", "id");
226 for(var i = 0; i < e.length; i++) {
227 id = e[i];
228 if( id.indexOf( "open-", 0 ) == 0 ) {
229 idNum = id.substr( 6, ( id.length - 6 ) );
230 getDataOnLoad(idNum,'post');
231 }
232 }
233 }
◆header.php
18行目付近
18 < ?php
19 if ( isset( $_GET['p'] ) ) {
20 echo '<body>';
21 } else {
22 echo '<body onLoad="getOpenClose();">';
23 }
24 ?>
No comments yet. You should be kind and add one!
By submitting a comment you grant typista a perpetual license to reproduce your words and name/web site in attribution. Inappropriate and irrelevant comments will be removed at an admin’s discretion. Your email is used for verification purposes only, it will never be shared.