Google Codes?!

10:57 0 Comments A+ a-

這算是一篇筆記文。昨晚要 PO 文在 Blogger 上時,執行了一個刪除文章的動作(~/delete.do),結果竟然在我的網頁上秀出一段程式碼!?定睛一看,竟然是 Google 的某支 javascript Source Code。

這段程式碼的作用,搞不太清楚,看來與圖(images)有關係,而且是採以 Ajax 的哦。至於為什麼會漏出來我也不得而知,只能猜是不是 Blogger 在更新什麼,然後不巧剛好被我執行到。想偷學 Google Ajax / javascript 的是如何動作的,可以參考一下,哈。


// Copyright 2006 Google Inc.
// Author: Tina Huang
// round the length and add the px so that it can be used in the style tag.
function BLOG_roundPx(x) {
return Math.round(x) + 'px';
}
// callback for when the image is finished loading.
function BLOG_finishLoad(displayImage) {
return (function() {
var thumb = document.createElement("img");
var mediaWrapper = displayImage.parentNode;
var linkDiv = mediaWrapper.getElementsByTagName("div")[0];
thumb.src = displayImage.src;
if (displayImage.height > displayImage.width) {
thumb.style.height = BLOG_roundPx(180);
thumb.style.width = BLOG_roundPx(180 * displayImage.width / displayImage.height);
} else {
thumb.style.width = BLOG_roundPx(180);
thumb.style.height = BLOG_roundPx(180 * displayImage.height / displayImage.width);
}
mediaWrapper.insertBefore(thumb, linkDiv);
var vertPadding = (180 - thumb.height) + 10;
var horzPadding = (180 - thumb.width) / 2 + 5;
mediaWrapper.style.padding = "5px "
+ BLOG_roundPx(horzPadding) + " "
+ BLOG_roundPx(vertPadding + linkDiv.clientHeight);
mediaWrapper.style.width = BLOG_roundPx(thumb.width);
mediaWrapper.style.height = BLOG_roundPx(thumb.height);
});
}
// callback for when there is an error on load. if we can't load the image,
// we assume that it's no longer on the server and thus we don't try to delete
// it.
function BLOG_errorOnLoad(displayImage) {
return (function() {
var mediaDiv = document.getElementById("_div_" + displayImage.id);
if (mediaDiv != null) {
mediaDiv.parentNode.removeChild(mediaDiv);
}
// check to see if we just removed the last image, and if so,
// get rid of the media div.
var mediaWell = document.getElementById("media");
var displayImages = mediaWell.getElementsByTagName("img");
if (displayImages.length == 0) {
mediaWell.parentNode.removeChild(mediaWell);
}
});
}
// this method is called on the body's onload method to basically
// set the source of the image buffers and their onload/onerror
// callbacks.
function BLOG_preloadImages() {
var mediaWell = document.getElementById("media");
if (mediaWell == null) {
// There are no images associated with the post.
return;
}
var displayImages = mediaWell.getElementsByTagName("img");
for (i=0; i<displayImages.length; ++i) {
var displayImage = displayImages[i];
if (HasClass(displayImage, "mediaBuffer")) {
var displayImageUrl = displayImage.id;
// We have to use this onload stuff to make IE happy.
displayImage.onload = BLOG_finishLoad(displayImage);
displayImage.onerror = BLOG_errorOnLoad(displayImage);
displayImage.src = displayImageUrl;
}
}
}
// this method gets called when the checkbox is (un)selected.
function BLOG_highlightSelected(checkbox) {
var mediaDiv = checkbox.parentNode.parentNode;
if (HasClass(mediaDiv, "selected")) {
RemoveClass(mediaDiv, "selected");
} else {
AddClass(mediaDiv, "selected");
}
}