function findTableDescendant(tagname)
{
var parent = document.getElementById("vB_Editor_QR_controls");
if(parent == null){
parent = document.getElementById("vB_Editor_001_controls");
var descendants = parent.getElementsByTagName(tagname);
if ( descendants.length)
return descendants[6];
else
return null;
}else{
var descendants = parent.getElementsByTagName(tagname);
if ( descendants.length)
return descendants[0];
else
return null;
}
}
if( document.getElementById("vB_Editor_QR_controls") || document.getElementById("vB_Editor_001_controls")){
var button = document.createElement("div");
button.style.color = "#000000";
button.style.padding = 1;
button.style.border = "none";
button.style.background = "#e1e1e2";
button.onmouseover = function(){
button.style.background = "#c1d2ee";
button.style.border = "1px solid #316AC5";
}
button.onmouseout = function(){
button.style.background = "#e1e1e2";
button.style.border = "none";
}
var image = document.createElement("img");
image.type = 'image';
image.src = chrome.extension.getURL("images/ybi.png");
image.width = 21;
image.height = 20;
image.alt = "Insert Youtube Link";
image.title = "Insert Youtube Link";
button.appendChild(image);
button.onclick = function(){
button.style.background = "#98b5e2";
button.style.border = "1px solid #316AC5";
var link = prompt("Please enter the URL of your Youtube Video:","http://");
if(link != null){
var rlink;
if(link.indexOf("youtu.be")!= -1){
if(link.indexOf("?")!= -1){
rlink = link.substring(link.indexOf("e/")+2, link.indexOf("?"));
}else{
rlink = link.substring(link.indexOf("e/")+2, link.length);
}
}else if(link.indexOf("watch?v")!= -1){
if(link.indexOf("&")!= -1){
rlink = link.substring(link.indexOf("?v")+3, link.indexOf("&"));
}else{
rlink = link.substring(link.indexOf("?v")+3, link.length);
}
}else{
rlink = link;
}
var txtarea = document.getElementById("vB_Editor_QR_textarea");
if(txtarea == null){
txtarea = document.getElementById("vB_Editor_001_textarea");
}
var startPos;
var endPos;
if (txtarea.selectionStart || txtarea.selectionStart == '0') {
startPos = txtarea.selectionStart;
endPos = txtarea.selectionEnd;
txtarea.value = txtarea.value.substring(0, startPos)
+ "[youtbe]"
+ rlink
+ "[/youtbe]"
+ txtarea.value.substring(endPos, txtarea.value.length);
} else {
txtarea.value = txtarea.value
+ "[youtbe]"
+ rlink
+ "[/youtbe]";
}
txtarea.focus();
txtarea.selectionStart = startPos + 9;
txtarea.selectionEnd = startPos + 9 + rlink.length;
}
}
var table = findTableDescendant("table");
var p = document.getElementById("vB_Editor_QR_controls");
if(p == null){
var newRow = table.rows[0];
var newCell = newRow.insertCell(17);
newCell.appendChild(button);
}else{
var newRow = table.rows[0];
var newCell = newRow.insertCell(10);
newCell.appendChild(button);
}
}