File indexing completed on 2024-12-22 04:41:05

0001 // ==UserScript==
0002 // @name     _falkon_speeddial
0003 // @run-at   document-end
0004 // @include  falkon:speeddial
0005 // ==/UserScript==
0006 
0007 var scriptData = {};
0008 var editingId = -1;
0009 var ignoreNextChanged = false;
0010 
0011 function b64DecodeUnicode(str) {
0012     return decodeURIComponent(atob(str).split('').map(function(c) {
0013         return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
0014     }).join(''));
0015 }
0016 
0017 function emitChanged(pages)
0018 {
0019     ignoreNextChanged = true;
0020     external.speedDial.changed(pages);
0021 }
0022 
0023 function addSpeedDial()
0024 {
0025     onEditClick(addBox('', scriptData.newPage , ''));
0026     alignPage();
0027 }
0028 
0029 function configureSpeedDial()
0030 {
0031     // ====================== LOAD SETTINGS ========================= //
0032     $('#PgInRow').val(scriptData.maxPagesRow);
0033     $('#sliderValuePg').html(scriptData.maxPagesRow);
0034     $('#SdSize').val(scriptData.dialWidth);
0035     $('#SdSizeToggle').prop('checked', scriptData.dialWidth != 240);
0036     $('#sliderValueSd').html(scriptData.dialWidth);
0037     $('#BgImgSelSiz').val(scriptData.bSize).attr('selected', 'selected');
0038     $('#BgImgToggle').prop('checked', scriptData.imgBackground != "");
0039     $('#SdCntrToggle').prop('checked', scriptData.sdCenter == "true");
0040     $('#SdLockDials').prop('checked', scriptData.lockDials == "true");
0041     $('#BgImgToggle').is(':checked') ? $('#BgImgSel').removeAttr('disabled') : $('#BgImgSel').attr('disabled', 'disabled');
0042     $('#BgImgToggle').is(':checked') ? $('#BgImgSelSiz').removeAttr('disabled') : $('#BgImgSelSiz').attr('disabled', 'disabled');
0043     $('#SdSizeToggle').is(':checked') ? $('#SdSize').removeAttr('disabled') : $('#SdSize').attr('disabled', 'disabled');
0044     if ($('#BgImgToggle').prop('checked') != true) {
0045         $('#ImgSelectorMenu').css({'color' : 'rgba(0,0,0, 0.0)', 'text-shadow' : 'none'});
0046         $('#BgImgSel').css({'color' : 'rgba(0,0,0, 0.0)', 'text-shadow' : 'none'});
0047         $('#BgImgSelSiz').css('visibility', 'hidden');
0048         }
0049     if ($('#SdSizeToggle').prop('checked') != true)
0050         $('#SdSizeStateColor').css('color', 'rgba(0,0,0, 0.0)');
0051     // ======================== SHOW DIALOG ======================== //
0052     $('#fadeOverlay2').css({'filter' : 'alpha(opacity=100)'}).fadeIn();
0053     $('#fadeOverlay2').click(function() { $(this).fadeOut('slow'); });
0054     $('#settingsBox').click(function(event) { event.stopPropagation(); });
0055 }
0056 
0057 function escapeTitle(title) {
0058     title = title.replace(/"/g, '"');
0059     title = title.replace(/'/g, ''');
0060     return title;
0061 }
0062 
0063 function unescapeTitle(title) {
0064     title = title.replace(/"/g, '"');
0065     title = title.replace(/'/g, '\'');
0066     return title;
0067 }
0068 
0069 function escapeUrl(url) {
0070     url = url.replace(/"/g, '');
0071     url = url.replace(/'/g, '');
0072     return url;
0073 }
0074 
0075 function onRemoveClick(box) {
0076     removeBox($(box).index());
0077 }
0078 
0079 function onFetchTitleClick(checkbox) {
0080     var displayStyle;
0081     checkbox.checked ? displayStyle = 'hidden' : displayStyle = 'visible';
0082     $('#titleLine').css({'visibility' : displayStyle });
0083 }
0084 
0085 function hideEditBox() {
0086     $('#fadeOverlay').fadeOut("slow", function() {$("#fadeOverlay").remove();});
0087 }
0088 
0089 function onEditClick(box) {
0090     editingId = $(box).index();
0091     var boxUrl = $(box).children('a').first().attr('href');
0092     var boxTitle = escapeTitle($(box).children('span').first().text());
0093     if (boxUrl === '')
0094         boxUrl = 'http://';
0095 
0096     $('body').append('<div id="fadeOverlay" class="overlay" style="display:none;">' +
0097         '<div id="overlay-edit">' +
0098         '<img src="' + $(box).children('img').first().attr('src') + '"> ' +
0099         '<table><tr><td>' + scriptData.url + ': </td><td>' +
0100         '<input type="text" id="formUrl" value="' + boxUrl + '"></td></tr>' +
0101         '<tr id="titleLine"><td>' + scriptData.title + ': </td><td>' +
0102         '<input type="text" id="formTitle" value="' + boxTitle + '"></td></tr>' +
0103         '<tr><td></td><td><div class="checkbox"><input type="checkbox" id="fetchTitle">' +
0104         '<label for="fetchTitle"></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  ' + scriptData.titleFetchTitle + ' </div></td></tr>' +
0105         '</table><p class="buttonbox"><input id="cancelEditBox" type="button" value=" ' + scriptData.cancel + ' ">&nbsp;&nbsp;&nbsp;' +
0106         '<input id="acceptEditBox" type="button" value="   ' + scriptData.edit + '   "></p>' +
0107         '</div></div>');
0108 
0109     $('#fetchTitle').click(function() {
0110         onFetchTitleClick(this);
0111     });
0112 
0113     $('#cancelEditBox').click(function() {
0114         hideEditBox();
0115         location.reload();
0116     });
0117 
0118     $('#acceptEditBox').click(boxEdited);
0119 
0120     $('#fadeOverlay').css({'filter' : 'alpha(opacity=100)'}).fadeIn();
0121     $('#fadeOverlay').click(function() {hideEditBox()});
0122     $('#overlay-edit').click(function(event) { event.stopPropagation(); });
0123 
0124     var temp = $('#formUrl').val();
0125     $('#formUrl').focus().val('').val(temp); // focus and move cursor to end
0126 }
0127 
0128 function onReloadClick(box) {
0129     var url = $(box).children('a').first().attr('href');
0130     var img = $(box).children('img').first();
0131 
0132     if (url === '')
0133         return;
0134 
0135     $(img).attr('src', scriptData.loadingImage);
0136     external.speedDial.loadThumbnail(url, false);
0137 }
0138 
0139 function boxEdited() {
0140     if (editingId == -1)
0141         return;
0142 
0143     external.speedDial.urlFromUserInput($('#formUrl').val(), function(newUrl) {
0144         var box = document.getElementById('quickdial').getElementsByTagName('div')[editingId];
0145         var a = box.getElementsByTagName('a')[0];
0146         var originalUrl = a.getAttribute('href');
0147         setBoxUrl(editingId, newUrl);
0148         setBoxTitle(editingId, $('#formTitle').val());
0149         var changedUrl = a.getAttribute('href');
0150         var fetchTitleChecked = document.getElementById('fetchTitle').checked;
0151 
0152         var pages = allPages();
0153 
0154         if (fetchTitleChecked || (originalUrl != changedUrl && changedUrl !== '') ) {
0155             var img = box.getElementsByTagName('img')[0];
0156             img.setAttribute('src', scriptData.loadingImage);
0157 
0158             $('#fadeOverlay').fadeOut("slow", function() {
0159                 $("#fadeOverlay").remove();
0160             });
0161             external.speedDial.loadThumbnail(a.getAttribute('href'), fetchTitleChecked);
0162         } else {
0163             hideEditBox();
0164         }
0165         emitChanged(pages);
0166     });
0167 }
0168 
0169 function allPages() {
0170     var urls = $('a[class="boxUrl"]');
0171     var titles = $('span[class="boxTitle"]');
0172     var value = "";
0173     $('div.entry').each(function(i) {
0174         var url = $(this).children('a').first().attr('href');
0175         var title = $(this).children('span[class="boxTitle"]').first().text();
0176 
0177         value += 'url:"' + escapeUrl(url) + '"|title:"' + escapeTitle(title) + '";';
0178     });
0179 
0180     return value;
0181 }
0182 
0183 function reloadAll() {
0184     if (confirm(scriptData.titleWarnRel))
0185         $('div.entry').each(function(i) {
0186             onReloadClick($(this));
0187         });
0188 }
0189 
0190 function addBox(url, title, img_source) {
0191     var div = document.createElement('div');
0192     div.setAttribute('class', 'entry');
0193     var img = document.createElement('img');
0194     img.setAttribute('src', img_source);
0195     var a = document.createElement('a');
0196     a.setAttribute('href', url);
0197     a.setAttribute('class', 'boxUrl');
0198     var span1 = document.createElement('span');
0199     span1.setAttribute('class', 'boxTitle');
0200     span1.setAttribute('title', unescapeTitle(title));
0201     span1.innerText = unescapeTitle(title);
0202     var span2 = document.createElement('span');
0203     span2.setAttribute('class', 'edit');
0204     span2.setAttribute('title', scriptData.titleEdit);
0205     span2.onclick = function() {
0206         onEditClick(div);
0207     };
0208     var span3 = document.createElement('span');
0209     span3.setAttribute('class', 'close');
0210     span3.setAttribute('title', scriptData.titleRemove);
0211     span3.onclick = function() {
0212         onRemoveClick(div);
0213     };
0214     var span4 = document.createElement('span');
0215     span4.setAttribute('class', 'reload');
0216     span4.setAttribute('title', scriptData.titleReload);
0217     span4.onclick = function() {
0218         onReloadClick(div);
0219     };
0220     div.appendChild(img);
0221     div.appendChild(img);
0222     div.appendChild(a);
0223     div.appendChild(span1);
0224     div.appendChild(span2);
0225     div.appendChild(span3);
0226     div.appendChild(span4);
0227     document.getElementById("quickdial").appendChild(div);
0228     if (img_source == scriptData.loadingImage) {
0229         external.speedDial.loadThumbnail(url, false);
0230     }
0231     return div;
0232 }
0233 
0234 function setBoxImage(id, img_source) {
0235     var box = document.getElementById('quickdial').getElementsByTagName('div')[id];
0236     if (box === undefined)
0237         return;
0238     var img = box.getElementsByTagName('img')[0];
0239     img.setAttribute('src', img_source + '?' + new Date());
0240 }
0241 
0242 function setTitleToUrl(url, title) {
0243     var changed = false;
0244     var boxes = document.getElementById('quickdial').getElementsByTagName('div');
0245     for (i = 0; i < boxes.length; ++i) {
0246         var box = boxes[i];
0247 
0248         if (box === undefined)
0249             continue;
0250 
0251         var boxUrl = box.getElementsByTagName('a')[0].getAttribute('href');
0252         if (url != boxUrl)
0253             continue;
0254 
0255         var span = box.getElementsByTagName('span')[0];
0256         if (span.innerText != title) {
0257             changed = true;
0258             span.innerText = title;
0259         }
0260     }
0261     if (changed)
0262         emitChanged(allPages());
0263 }
0264 
0265 function setImageToUrl(url, img_source) {
0266     var aElement = $('a[href="' + url + '"]');
0267     $(aElement).each(function() {
0268         var box = $(this).parent();
0269         var imgElement = $(box).children("img").first();
0270         if ($(imgElement).size() == 0)
0271             return;
0272         $(imgElement).attr('src', img_source/* + '?' + new Date()*/);
0273     });
0274 }
0275 
0276 function setBoxUrl(id, url) {
0277     var box = document.getElementById('quickdial').getElementsByTagName('div')[id];
0278     if (box === undefined)
0279         return;
0280     var a = box.getElementsByTagName('a')[0];
0281     a.setAttribute('href', url);
0282 }
0283 
0284 function setBoxTitle(id, title) {
0285     var box = document.getElementById('quickdial').getElementsByTagName('div')[id];
0286     if (box === undefined)
0287         return;
0288     var span = box.getElementsByTagName('span')[0];
0289     span.innerText = title;
0290 }
0291 
0292 function removeBox(id) {
0293     if (confirm(scriptData.titleWarn))
0294         var box = document.getElementById('quickdial').getElementsByTagName('div')[id];
0295     if (box === undefined)
0296         return;
0297     var url = box.getElementsByTagName('a')[0].getAttribute('href');
0298     document.getElementById("quickdial").removeChild(box);
0299     alignPage();
0300     external.speedDial.removeImageForUrl(url);
0301     emitChanged(allPages());
0302 }
0303 
0304 function alignPage() {
0305     var dialWidth = parseInt(scriptData.dialWidth);
0306     var dialHeight = Math.floor(Math.round(dialWidth / 1.54));
0307     $('head').append('<style>#quickdial img{height:auto;width:'+dialWidth+'px}</style>');
0308     $('#quickdial div.entry').css({'width' : dialWidth + 'px',
0309         'height' : dialHeight  + 'px'});
0310     var fullwidth = $(window).width();
0311     var width = Math.floor(fullwidth - 76);
0312     var height = $(window).height();
0313     var boxWidth = Math.floor(dialWidth + 12);
0314     var boxHeight = dialHeight + 22;
0315     var maxBoxes = Math.floor(width / boxWidth);
0316     if (maxBoxes > scriptData.maxPagesRow) maxBoxes = scriptData.maxPagesRow;
0317     if (maxBoxes < 1) maxBoxes = 1;
0318     var maxwidth = maxBoxes * boxWidth;
0319     $("#quickdial").css('width', maxwidth + 'px');
0320     var boxesCount = $("#quickdial").children("div").size();
0321     var rows = Math.ceil(boxesCount / maxBoxes);
0322     var margintop = (height - rows * boxHeight) / 2;
0323     if (margintop < 0) margintop = 0;
0324     $("#quickdial").css('margin-top', margintop + 'px');
0325     $("span.boxTitle").css('font-size', ((dialHeight - dialWidth / 1.77) / 1.5) + 'px');
0326     if (scriptData.sdCenter == "true") {
0327         enableCentering();
0328     } else {
0329         disableCentering();
0330     }
0331 }
0332 
0333 function bgImageSel() {
0334     external.speedDial.getOpenFileName(function(arr) {
0335         if (arr.length) {
0336             document.getElementById('BgImgHold').value = arr[0];
0337             document.getElementById('BgImgHoldUrl').value = arr[1];
0338             bgImgUpdate();
0339         }
0340     });
0341 }
0342 
0343 function saveSettings() {
0344     scriptData.maxPagesRow = $('#PgInRow').val();
0345     scriptData.dialWidth = $('#SdSize').val();
0346     scriptData.sdCenter = $('#SdCntrToggle').prop('checked');
0347     scriptData.lockDials = $('#SdLockDials').prop('checked');
0348     external.speedDial.setBackgroundImage($('#BgImgHoldUrl').val());
0349     external.speedDial.setBackgroundImageSize($('#BgImgSelSiz').val());
0350     external.speedDial.setPagesInRow(scriptData.maxPagesRow);
0351     external.speedDial.setSdSize(scriptData.dialWidth);
0352     external.speedDial.setSdCentered(scriptData.sdCenter == "true");
0353     external.speedDial.setLockDials(scriptData.lockDials == "true");
0354     alignPage();
0355 }
0356 
0357 function bgImgToggle() {
0358     var check = document.getElementById('BgImgToggle');
0359     var BgImgSel = document.getElementById('BgImgSel');
0360     var BgImgHoldUrl = document.getElementById('BgImgHoldUrl');
0361     var BgImgSz = document.getElementById('BgImgSelSiz');
0362     BgImgSel.disabled = (check.checked ? false : true);
0363     BgImgHoldUrl.disabled = (check.checked ? false : true);
0364     BgImgSz.disabled = (check.checked ? false : true);
0365     BgImgHoldUrl.value = (check.checked ? scriptData.urlBackground : '');
0366     if ($('#BgImgToggle').prop('checked') != true) {
0367         $('#ImgSelectorMenu').css({'color' : 'rgba(0,0,0, 0.0)', 'text-shadow' : 'none'});
0368         $('#BgImgSel').css({'color' : 'rgba(0,0,0, 0.0)', 'text-shadow' : 'none'});
0369         $('#BgImgSelSiz').css('visibility', 'hidden');
0370     } else {
0371         $('#ImgSelectorMenu').css({'color' : '#eaeaea', 'text-shadow' : '1px 1px 2px #000000, 0 0 1em #000000'});
0372         $('#BgImgSel').css({'color' : '#eaeaea', 'text-shadow' : '1px 1px 2px #000000, 0 0 1em #000000'});
0373         $('#BgImgSelSiz').css('visibility', 'visible');
0374     };
0375 }
0376 
0377 function sdSizeToggle() {
0378     var check = document.getElementById('SdSizeToggle');
0379     var SdSize = document.getElementById('SdSize');
0380     var SdSizeSl = document.getElementById('sliderValueSd');
0381     SdSize.disabled = (check.checked ? false : true);
0382     SdSize.value = (check.checked ? SdSize.value : 240);
0383     SdSizeSl.innerHTML = (check.checked ? scriptData.dialWidth : 240);
0384     if ($('#SdSizeToggle').prop('checked') != true) {
0385         $('#SdSizeStateColor').css('color', 'rgba(0,0,0, 0.0)');
0386     } else {
0387     $('#SdSizeStateColor').css('color', '#eaeaea')
0388     }
0389 }
0390 
0391 function bgImgUpdate() {
0392     var imgUrl = document.getElementById('BgImgHold').value;
0393     var imgSize = document.getElementById('BgImgSelSiz').value;
0394     var imgThumb = document.getElementById('thumb');
0395     imgThumb.style.backgroundImage = 'url("' + imgUrl + '")';
0396     imgThumb.title = imgUrl.substring(imgUrl.lastIndexOf('/')+1);
0397     imgThumb.style.backgroundSize = imgSize;
0398     document.documentElement.style.backgroundImage = 'url("' + imgUrl + '")';
0399     document.documentElement.style.backgroundSize = imgSize;
0400 }
0401 
0402 function enableCentering() {
0403     $('#quickdial div.entry').css({
0404         float: 'none',
0405         display: 'inline-block'
0406     });
0407 }
0408 
0409 function disableCentering() {
0410     $('#quickdial div.entry').css({
0411         float: scriptData.leftStr,
0412         display: 'block'
0413     });
0414 }
0415 
0416 function init() {
0417     scriptData = document.getElementById("script-data").dataset;
0418 
0419     document.getElementById("button-configure-speed-dial").onclick = configureSpeedDial;
0420     document.getElementById("button-add-speed-dial").onclick = addSpeedDial;
0421     document.getElementById("PgInRow").oninput = function() {
0422         $('#sliderValuePg').html(this.value);
0423     };
0424     document.getElementById("SdSizeToggle").onchange = sdSizeToggle;
0425     document.getElementById("SdSize").oninput = function() {
0426         $('#sliderValueSd').html(this.value);
0427     };
0428     document.getElementById("BgImgSel").onclick = function() {
0429         if ($('#BgImgSelSiz').attr('disabled') != 'disabled') {
0430             bgImageSel();
0431         }
0432     };
0433     document.getElementById("BgImgToggle").onchange = function() {
0434         bgImgToggle();
0435         bgImgUpdate();
0436     };
0437     document.getElementById("BgImgSelSiz").onchange = bgImgUpdate;
0438     document.getElementById("button-cancel").onclick = function() {
0439         $('#fadeOverlay2').fadeOut('slow');
0440         location.reload();
0441     };
0442     document.getElementById("button-apply").onclick = function() {
0443         saveSettings();
0444         $('#fadeOverlay2').fadeOut('slow');
0445         location.reload();
0446     };
0447 
0448     if (scriptData.imgBackground == '') {
0449         document.getElementById("html").style.backgroundSize = "cover";
0450     }
0451 
0452     $(document).keyup(function(e) {
0453         if (editingId == -1)
0454             return;
0455         if (e.keyCode == 13)
0456             boxEdited();
0457         else if (e.keyCode == 27)
0458             $('#fadeOverlay').click();
0459     });
0460 
0461     var pages = JSON.parse(b64DecodeUnicode(scriptData.initialScript));
0462     for (var i = 0; i < pages.length; ++i) {
0463         var page = pages[i];
0464         addBox(page.url, page.title, page.img);
0465     }
0466 
0467     external.speedDial.pagesChanged.connect(function() {
0468         if (ignoreNextChanged) {
0469             ignoreNextChanged = false;
0470             return;
0471         }
0472         window.location.reload();
0473     });
0474 
0475     external.speedDial.thumbnailLoaded.connect(setImageToUrl);
0476     external.speedDial.pageTitleLoaded.connect(setTitleToUrl);
0477 
0478     $(window).resize(function() { alignPage(); });
0479     $("#quickdial").sortable({
0480         revert: true,
0481         cursor: 'move',
0482         containment: 'document',
0483         opacity: 0.8,
0484         distance: 40,
0485         disabled: scriptData.lockDials == "true",
0486         start: function(event, ui) {
0487             disableCentering();
0488         },
0489         stop: function(event, ui) {
0490             if (scriptData.sdCenter == "true")
0491                 enableCentering();
0492         },
0493         update: function(event, ui) {
0494             emitChanged(allPages());
0495         }
0496     });
0497 
0498     alignPage();
0499 }
0500 
0501 %JQUERY%
0502 %JQUERY-UI%
0503 
0504 // Initialize
0505 if (window._falkon_external) {
0506     init();
0507 } else {
0508     document.addEventListener("_falkon_external_created", init);
0509 }