Skip to content

Instantly share code, notes, and snippets.

@axemclion
Created April 2, 2009 14:08
Show Gist options
  • Save axemclion/89202 to your computer and use it in GitHub Desktop.
Save axemclion/89202 to your computer and use it in GitHub Desktop.
A bookmarklet to resize any Flash object on HTML pages.
var FlashResizer = {
init: function() {
if (typeof(window["__FLASH_RESIZER__"]) === "undefined") {
window["__FLASH_RESIZER__"] = "started";
this.startResizer();
window.__resizeElements__ = [];
}
else
if (window["__FLASH_RESIZER__"] === "started") {
window["__FLASH_RESIZER__"] = "suspended";
for (var i = 0; i < window.__resizeElements__.length; i++) {
window.__resizeElements__[i].destroy();
}
window.__resizeElements__ = [];
}
else
if (window["__FLASH_RESIZER__"] === "suspended") {
window["__FLASH_RESIZER__"] = "started";
var elements = YAHOO.util.Dom.getElementsByClassName('move-handler');
for (var i = 0; i < elements.length; i++) {
var resize = new YAHOO.util.Resize(elements[i], {
handles: 'all',
knobHandles: true,
proxy: false,
draggable: true,
animate: false
});
window.__resizeElements__.push(resize);
}
}
},
startResizer: function() {
var yuiScript = document.createElement("script");
yuiScript.setAttribute("src", "http://yui.yahooapis.com/combo?2.7.0/build/utilities/utilities.js&2.7.0/build/button/button-min.js&2.7.0/build/json/json-min.js&2.7.0/build/logger/logger-min.js&2.7.0/build/resize/resize-min.js&2.7.0/build/stylesheet/stylesheet-min.js");
yuiScript.setAttribute("type", "text/javascript")
document.body.appendChild(yuiScript);
var curObj = this;
var loader = document.createElement("div");
loader.innerHTML = "Loading";
loader.className = ".loader";
document.body.appendChild(loader);
var timerHandler = window.setInterval(function() {
if (typeof(YAHOO) !== "undefined") {
window.clearInterval(timerHandler);
//this.myLogReader = new YAHOO.widget.LogReader();
YAHOO.widget.Logger.enableBrowserConsole()
YAHOO.log("Starting Flash Resizer");
curObj.setStyles();
loader.parentNode.removeChild(loader);
var flashElements = curObj.getFlashElements();
for (var i = 0; i < flashElements.length; i++) {
var handleDiv = curObj.createDivOnTop(flashElements[i]);
handleDiv.className = "flash-hilite";
curObj.addHandlers(handleDiv, flashElements[i]);
}
}
}, 1000);
},
/**
* Gets the list of all flash objects in the page.
* If the flash element is in the windowed mode, sets it to opaque mode
* so that the div elements can see it
*/
getFlashElements: function() {
var objectFlash = function() {
var result = [];
var embedTags = document.getElementsByTagName("object");
for (var i = 0; i < embedTags.length; i++) {
var parameters = document.getElementsByTagName("param");
var wmodeFound = false;
var wmodeChanged = false;
for (var j = 0; j < parameters.length; j++) {
if (parameters[i].getAttribute("name") === "wmode") {
if (parameters[i].getAttribute("value") === "window") {
parameters[i].setValue("value", "opaque");
wmodeFound = true;
wmodeChanged = true
break;
}
}
}
if (wmodeFound === false) {
var wmodeParam = document.createElement("param");
wmodeParam.setAttribute("name", "wmode");
wmodeParam.setAttribute("value", "opaque");
embedTags[i].appendChild(wmodeParam);
wmodeChanged = true
}
if (wmodeChanged === true) {
var newFlash = embedTags[i].cloneNode(true);
embedTags[i].parentNode.replaceChild(newFlash, embedTags[i]);
YAHOO.log("Changing wmode to transparent for " + embedTags[i])
}
result.push(embedTags[i]);
}
return result;
}
var embedFlash = function() {
var result = [];
var embedTags = document.getElementsByTagName("embed");
for (var i = 0; i < embedTags.length; i++) {
if (embedTags[i].getAttribute("wmode") == null || embedTags[i].getAttribute("wmode") === "window") {
var newFlash = embedTags[i].cloneNode(true);
newFlash.setAttribute("wmode", "opaque");
embedTags[i].parentNode.replaceChild(newFlash, embedTags[i]);
YAHOO.log("Changing wmode to transparent for " + embedTags[i])
}
result.push(embedTags[i]);
}
return result;
}
if (navigator.appName.indexOf("Internet Explorer") !== -1) {
return objectFlash();
}
else {
return embedFlash();
}
},
setStyles: function() {
YAHOO.util.Get.css(["http://n.parashuram.googlepages.com/FlashResize.css", "http://yui.yahooapis.com/combo?2.7.0/build/assets/skins/sam/skin.css"]);
},
createDivOnTop: function(element, parent) {
if (typeof(parent) === "undefined") {
parent = element.parentNode;
}
YAHOO.log("Creating Div on top of " + element);
YAHOO.log(element);
var handleDiv = document.createElement("div");
handleDiv.id = "handler-" + Math.random();
handleDiv.style.width = element.offsetWidth + "px";
handleDiv.style.height = element.offsetHeight + "px";
parent.appendChild(handleDiv);
var top = element.offsetTop;
var left = element.offsetLeft;
var parentList = {};
var tempElem = parent;
while (tempElem != null) {
parentList[tempElem] = true;
tempElem = tempElem.parentNode;
}
tempElem = element.offsetParent;
while (typeof(tempElem) !== "undefined" && tempElem !== null && !(tempElem in parentList)) {
top += tempElem.offsetTop;
left += tempElem.offsetLeft;
tempElem = tempElem.offsetParent;
}
YAHOO.log(["Setting ", handleDiv.id, "(", top, ",", left, ")-(", element.offsetWidth, ",", element.offsetHeight, ")"].join(""));
YAHOO.log(handleDiv);
handleDiv.style.top = top + "px";
handleDiv.style.left = left + "px";
return handleDiv;
},
addHandlers: function(elem, flashElement) {
YAHOO.util.Event.addListener(elem, "mousemove", function(e, obj) {
obj.style.opacity = "0.7";
}, elem, this);
YAHOO.util.Event.addListener(elem, "mouseout", function(e, obj) {
obj.style.opacity = "0.3";
}, elem, this);
YAHOO.util.Event.addListener(elem, "click", function(e, obj) {
this.augmentFlash(obj, flashElement);
}, elem, this);
},
augmentFlash: function(handler, flashElem) {
var clonedFlash = flashElem.cloneNode(true);
YAHOO.util.Event.purgeElement(handler);
handler.className = ".flash-clicked";
var moveHandler = this.createDivOnTop(flashElem, document.body);
moveHandler.className = "move-handler";
clonedFlash.setAttribute("width", "99%");
clonedFlash.setAttribute("height", "99%");
moveHandler.appendChild(clonedFlash);
flashElem.parentNode.removeChild(flashElem);
var resize = new YAHOO.util.Resize(moveHandler, {
handles: 'all',
knobHandles: true,
proxy: false,
draggable: true,
animate: false
});
window.__resizeElements__.push(resize);
moveHandler.style.position = "absolute";
}
}
FlashResizer.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment