1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-05 02:34:07 +02:00

Refactor parts of code.js (make code shorter, use 'let')

This commit is contained in:
chylex 2017-07-30 20:19:59 +02:00
parent 93c1cbd231
commit 71925e1126

View File

@ -56,7 +56,7 @@
// //
var appendToFunction = function(func, extension){ var appendToFunction = function(func, extension){
return function(){ return function(){
var res = func.apply(this, arguments); let res = func.apply(this, arguments);
extension.apply(this, arguments); extension.apply(this, arguments);
return res; return res;
}; };
@ -66,7 +66,7 @@
// Function: Returns true if an object has a specified property, otherwise returns false without throwing an error. // Function: Returns true if an object has a specified property, otherwise returns false without throwing an error.
// //
var ensurePropertyExists = function(obj, ...chain){ var ensurePropertyExists = function(obj, ...chain){
for(var index = 0; index < chain.length; index++){ for(let index = 0; index < chain.length; index++){
if (!obj.hasOwnProperty(chain[index])){ if (!obj.hasOwnProperty(chain[index])){
$TD.crashDebug("Missing property "+chain[index]+" in chain [obj]."+chain.join(".")); $TD.crashDebug("Missing property "+chain[index]+" in chain [obj]."+chain.join("."));
return false; return false;
@ -237,12 +237,12 @@
onAppReady.push(function(){ onAppReady.push(function(){
$("[data-action='settings-menu']").click(function(){ $("[data-action='settings-menu']").click(function(){
setTimeout(function(){ setTimeout(function(){
var menu = $(".js-dropdown-content").children("ul").first(); let menu = $(".js-dropdown-content").children("ul").first();
if (menu.length === 0)return; if (menu.length === 0)return;
menu.children(".drp-h-divider").last().before('<li class="is-selectable" data-std><a href="#" data-action="tweetduck">TweetDuck</a></li>'); menu.children(".drp-h-divider").last().before('<li class="is-selectable" data-std><a href="#" data-action="tweetduck">TweetDuck</a></li>');
var button = menu.children("[data-std]"); let button = menu.children("[data-std]");
button.on("click", "a", function(){ button.on("click", "a", function(){
$TD.openContextMenu(); $TD.openContextMenu();
@ -272,7 +272,7 @@
var me = $(this); var me = $(this);
if (e.type === "mouseenter"){ if (e.type === "mouseenter"){
var text = me.text(); let text = me.text();
if (text.charCodeAt(text.length-1) !== 8230){ // horizontal ellipsis if (text.charCodeAt(text.length-1) !== 8230){ // horizontal ellipsis
return; return;
@ -280,7 +280,7 @@
if ($TDX.expandLinksOnHover){ if ($TDX.expandLinksOnHover){
tooltipTimer = window.setTimeout(function(){ tooltipTimer = window.setTimeout(function(){
var expanded = me.attr("data-full-url"); let expanded = me.attr("data-full-url");
expanded = cutStart(expanded, "https://"); expanded = cutStart(expanded, "https://");
expanded = cutStart(expanded, "http://"); expanded = cutStart(expanded, "http://");
expanded = cutStart(expanded, "www."); expanded = cutStart(expanded, "www.");
@ -298,7 +298,7 @@
} }
else if (e.type === "mouseleave"){ else if (e.type === "mouseleave"){
if ($TDX.expandLinksOnHover){ if ($TDX.expandLinksOnHover){
var prevText = me.attr("td-prev-text"); let prevText = me.attr("td-prev-text");
if (prevText){ if (prevText){
me.text(prevText); me.text(prevText);
@ -344,7 +344,7 @@
// Block: Hook into the notification sound effect. // Block: Hook into the notification sound effect.
// //
(function(){ (function(){
var soundEle = document.getElementById("update-sound"); let soundEle = document.getElementById("update-sound");
soundEle.play = prependToFunction(soundEle.play, function(){ soundEle.play = prependToFunction(soundEle.play, function(){
return $TDX.muteNotifications || $TDX.hasCustomNotificationSound; return $TDX.muteNotifications || $TDX.hasCustomNotificationSound;
@ -386,19 +386,19 @@
app.delegate("article.js-stream-item", "mouseenter mouseleave", function(e){ app.delegate("article.js-stream-item", "mouseenter mouseleave", function(e){
if (e.type === "mouseenter"){ if (e.type === "mouseenter"){
var me = $(this); let me = $(this);
if (!me[0].hasAttribute("data-account-key") || (!highlightedColumnObj && !updateHighlightedColumn(me.closest("section.js-column")))){ if (!me[0].hasAttribute("data-account-key") || (!highlightedColumnObj && !updateHighlightedColumn(me.closest("section.js-column")))){
return; return;
} }
var tweet = highlightedColumnObj.findChirp(me.attr("data-tweet-id")) || highlightedColumnObj.findChirp(me.attr("data-key")); let tweet = highlightedColumnObj.findChirp(me.attr("data-tweet-id")) || highlightedColumnObj.findChirp(me.attr("data-key"));
if (tweet){ if (tweet){
if (tweet.chirpType === TD.services.ChirpBase.TWEET){ if (tweet.chirpType === TD.services.ChirpBase.TWEET){
var link = tweet.getChirpURL(); let link = tweet.getChirpURL();
var embedded = tweet.quotedTweet ? tweet.quotedTweet.getChirpURL() : ""; let embedded = tweet.quotedTweet ? tweet.quotedTweet.getChirpURL() : "";
var images = tweet.hasImage() ? tweet.getMedia().filter(item => !item.isAnimatedGif).map(item => item.entity.media_url_https+":small").join(";") : ""; let images = tweet.hasImage() ? tweet.getMedia().filter(item => !item.isAnimatedGif).map(item => item.entity.media_url_https+":small").join(";") : "";
// TODO maybe handle embedded images too? // TODO maybe handle embedded images too?
updateHighlightedTweet(me, tweet, link || "", embedded || "", images); updateHighlightedTweet(me, tweet, link || "", embedded || "", images);
@ -432,11 +432,11 @@
window.TDGF_triggerScreenshot = function(){ window.TDGF_triggerScreenshot = function(){
if (selectedTweet){ if (selectedTweet){
var tweetWidth = Math.floor(selectedTweet.width()); let tweetWidth = Math.floor(selectedTweet.width());
var parent = selectedTweet.parent(); let parent = selectedTweet.parent();
var isDetail = parent.hasClass("js-tweet-detail"); let isDetail = parent.hasClass("js-tweet-detail");
var isReply = !isDetail && (parent.hasClass("js-replies-to") || parent.hasClass("js-replies-before")); let isReply = !isDetail && (parent.hasClass("js-replies-to") || parent.hasClass("js-replies-before"));
selectedTweet = selectedTweet.clone(); selectedTweet = selectedTweet.clone();
selectedTweet.children().first().addClass($(document.documentElement).attr("class")).css("padding-bottom", "0"); selectedTweet.children().first().addClass($(document.documentElement).attr("class")).css("padding-bottom", "0");
@ -470,13 +470,13 @@
selectedTweet.find(".js-poll-link").remove(); selectedTweet.find(".js-poll-link").remove();
selectedTweet.find(".td-screenshot-remove").remove(); selectedTweet.find(".td-screenshot-remove").remove();
var testTweet = selectedTweet.clone().css({ let testTweet = selectedTweet.clone().css({
position: "absolute", position: "absolute",
left: "-999px", left: "-999px",
width: tweetWidth+"px" width: tweetWidth+"px"
}).appendTo(document.body); }).appendTo(document.body);
var realHeight = Math.floor(testTweet.height()); let realHeight = Math.floor(testTweet.height());
testTweet.remove(); testTweet.remove();
$TD.screenshotTweet(selectedTweet.html(), tweetWidth, realHeight); $TD.screenshotTweet(selectedTweet.html(), tweetWidth, realHeight);
@ -512,18 +512,18 @@
}; };
var tryCloseModal1 = function(){ var tryCloseModal1 = function(){
var modal = $("#open-modal"); let modal = $("#open-modal");
return modal.is(":visible") && tryClickSelector("a.mdl-dismiss", modal); return modal.is(":visible") && tryClickSelector("a.mdl-dismiss", modal);
}; };
var tryCloseModal2 = function(){ var tryCloseModal2 = function(){
var modal = $(".js-modals-container"); let modal = $(".js-modals-container");
return modal.length && tryClickSelector("a.mdl-dismiss", modal); return modal.length && tryClickSelector("a.mdl-dismiss", modal);
}; };
var tryCloseHighlightedColumn = function(){ var tryCloseHighlightedColumn = function(){
if (highlightedColumnEle){ if (highlightedColumnEle){
var column = highlightedColumnEle.closest(".js-column"); let column = highlightedColumnEle.closest(".js-column");
return (column.is(".is-shifted-2") && tryClickSelector(".js-tweet-social-proof-back", column)) || (column.is(".is-shifted-1") && tryClickSelector(".js-column-back", column)); return (column.is(".is-shifted-2") && tryClickSelector(".js-tweet-social-proof-back", column)) || (column.is(".is-shifted-1") && tryClickSelector(".js-column-back", column));
} }
}; };
@ -551,7 +551,7 @@
// //
$(document).on("dataTweetSent", function(e, data){ $(document).on("dataTweetSent", function(e, data){
if (data.response.state && data.response.state === "scheduled"){ if (data.response.state && data.response.state === "scheduled"){
var column = Object.values(TD.controller.columnManager.getAll()).find(column => column.model.state.type === "scheduled"); let column = Object.values(TD.controller.columnManager.getAll()).find(column => column.model.state.type === "scheduled");
if (column){ if (column){
setTimeout(function(){ setTimeout(function(){
@ -658,41 +658,46 @@
// Block: Inject custom CSS and layout into the page. // Block: Inject custom CSS and layout into the page.
// //
(function(){ (function(){
var styleOfficial = document.createElement("style"); let styleOfficial = document.createElement("style");
document.head.appendChild(styleOfficial); document.head.appendChild(styleOfficial);
styleOfficial.sheet.insertRule("a[data-full-url] { word-break: break-all; }", 0); // break long urls let addRule = (rule) => {
styleOfficial.sheet.insertRule(".column-nav-link .attribution { position: absolute; }", 0); // fix cut off account names styleOfficial.sheet.insertRule(rule, 0);
styleOfficial.sheet.insertRule(".txt-base-smallest .sprite-verified-mini { width: 13px !important; height: 13px !important; background-position: -223px -99px !important; }", 0); // fix cut off badge icon when zoomed in };
styleOfficial.sheet.insertRule(".keyboard-shortcut-list { vertical-align: top; }", 0); // fix keyboard navigation alignment
styleOfficial.sheet.insertRule(".sprite-logo { background-position: -5px -46px !important; }", 0); // fix TweetDeck logo on certain zoom levels
styleOfficial.sheet.insertRule(".app-navigator .tooltip { display: none !important; }", 0); // hide broken tooltips in the menu
styleOfficial.sheet.insertRule(".account-inline .username { vertical-align: 10%; }", 0); // move usernames a bit higher
styleOfficial.sheet.insertRule(".btn-compose, .app-search-fake, .app-search-input { border-radius: 1px; }", 0); // use consistent menu button radius addRule("a[data-full-url] { word-break: break-all; }", 0); // break long urls
styleOfficial.sheet.insertRule(".is-condensed .app-header-inner { padding-top: 10px !important; }", 0); // add extra padding to menu buttons when condensed addRule(".keyboard-shortcut-list { vertical-align: top; }", 0); // fix keyboard navigation alignment
styleOfficial.sheet.insertRule(".is-condensed .btn-compose { padding: 8px !important; }", 0); // fix compose button icon when condensed addRule(".account-inline .username { vertical-align: 10%; }", 0); // move usernames a bit higher
styleOfficial.sheet.insertRule(".app-header:not(.is-condensed) .nav-user-info { padding: 0 5px; }", 0); // add padding to user info
styleOfficial.sheet.insertRule(".app-title { display: none; }", 0); // hide TweetDeck logo addRule(".column-nav-link .attribution { position: absolute; }", 0); // fix cut off account names
styleOfficial.sheet.insertRule(".nav-user-info { bottom: 10px !important; }", 0); // move user info addRule(".txt-base-smallest .sprite-verified-mini { width: 13px !important; height: 13px !important; background-position: -223px -99px !important; }", 0); // fix cut off badge icon when zoomed in
styleOfficial.sheet.insertRule(".app-navigator { bottom: 50px !important; }", 0); // move navigation addRule(".sprite-logo { background-position: -5px -46px !important; }", 0); // fix TweetDeck logo on certain zoom levels
styleOfficial.sheet.insertRule(".column-navigator-overflow { bottom: 192px !important; }", 0); // move column list addRule(".app-navigator .tooltip { display: none !important; }", 0); // hide broken tooltips in the menu
styleOfficial.sheet.insertRule(".column .column-header { height: 49px !important; }", 0); // fix one pixel space below column header addRule(".btn-compose, .app-search-fake, .app-search-input { border-radius: 1px; }", 0); // use consistent menu button radius
styleOfficial.sheet.insertRule(".column:not(.is-options-open) .column-header { border-bottom: none; }", 0); // fix one pixel space below column header addRule(".is-condensed .app-header-inner { padding-top: 10px !important; }", 0); // add extra padding to menu buttons when condensed
addRule(".is-condensed .btn-compose { padding: 8px !important; }", 0); // fix compose button icon when condensed
addRule(".app-header:not(.is-condensed) .nav-user-info { padding: 0 5px; }", 0); // add padding to user info
styleOfficial.sheet.insertRule(".activity-header { align-items: center !important; margin-bottom: 4px; }", 0); // tweak alignment of avatar and text in notifications addRule(".app-title { display: none; }", 0); // hide TweetDeck logo
styleOfficial.sheet.insertRule(".activity-header .tweet-timestamp { line-height: unset }", 0); // fix timestamp position in notifications addRule(".nav-user-info { bottom: 10px !important; }", 0); // move user info
addRule(".app-navigator { bottom: 50px !important; }", 0); // move navigation
addRule(".column-navigator-overflow { bottom: 192px !important; }", 0); // move column list
styleOfficial.sheet.insertRule(".app-columns-container::-webkit-scrollbar-track { border-left: 0; }", 0); // remove weird border in the column container scrollbar addRule(".column .column-header { height: 49px !important; }", 0); // fix one pixel space below column header
styleOfficial.sheet.insertRule(".app-columns-container { bottom: 0 !important; }", 0); // move column container scrollbar to bottom to fit updated style addRule(".column:not(.is-options-open) .column-header { border-bottom: none; }", 0); // fix one pixel space below column header
styleOfficial.sheet.insertRule(".js-column-header .column-header-link { padding: 0; }", 0); // fix column header tooltip hover box addRule(".activity-header { align-items: center !important; margin-bottom: 4px; }", 0); // tweak alignment of avatar and text in notifications
styleOfficial.sheet.insertRule(".js-column-header .column-header-link .icon { padding: 9px 4px; width: calc(1em + 8px); height: 100%; box-sizing: border-box; }", 0); // fix column header tooltip hover box addRule(".activity-header .tweet-timestamp { line-height: unset }", 0); // fix timestamp position in notifications
styleOfficial.sheet.insertRule(".is-video a:not([href*='youtu']), .is-gif .js-media-gif-container { cursor: alias; }", 0); // change cursor on unsupported videos addRule(".app-columns-container::-webkit-scrollbar-track { border-left: 0; }", 0); // remove weird border in the column container scrollbar
styleOfficial.sheet.insertRule(".is-video a:not([href*='youtu']) .icon-bg-dot, .is-gif .icon-bg-dot { color: #bd3d37; }", 0); // change play icon color on unsupported videos addRule(".app-columns-container { bottom: 0 !important; }", 0); // move column container scrollbar to bottom to fit updated style
addRule(".js-column-header .column-header-link { padding: 0; }", 0); // fix column header tooltip hover box
addRule(".js-column-header .column-header-link .icon { padding: 9px 4px; width: calc(1em + 8px); height: 100%; box-sizing: border-box; }", 0); // fix column header tooltip hover box
addRule(".is-video a:not([href*='youtu']), .is-gif .js-media-gif-container { cursor: alias; }", 0); // change cursor on unsupported videos
addRule(".is-video a:not([href*='youtu']) .icon-bg-dot, .is-gif .icon-bg-dot { color: #bd3d37; }", 0); // change play icon color on unsupported videos
window.TDGF_reinjectCustomCSS = function(styles){ window.TDGF_reinjectCustomCSS = function(styles){
$("#tweetduck-custom-css").remove(); $("#tweetduck-custom-css").remove();
@ -732,7 +737,7 @@
!ensurePropertyExists(TD, "ui", "Column", "prototype", "playGifIfNotManuallyPaused"))return; !ensurePropertyExists(TD, "ui", "Column", "prototype", "playGifIfNotManuallyPaused"))return;
TD.components.MediaGallery.prototype._loadTweet = appendToFunction(TD.components.MediaGallery.prototype._loadTweet, function(){ TD.components.MediaGallery.prototype._loadTweet = appendToFunction(TD.components.MediaGallery.prototype._loadTweet, function(){
var media = this.chirp.getMedia().find(media => media.mediaId === this.clickedMediaEntityId); let media = this.chirp.getMedia().find(media => media.mediaId === this.clickedMediaEntityId);
if (media && media.isVideo && media.service !== "youtube"){ if (media && media.isVideo && media.service !== "youtube"){
$TD.openBrowser(this.clickedLink); $TD.openBrowser(this.clickedLink);
@ -751,8 +756,8 @@
TD.mustaches["status/media_thumb.mustache"] = TD.mustaches["status/media_thumb.mustache"].replace("is-gif", "is-gif is-paused"); TD.mustaches["status/media_thumb.mustache"] = TD.mustaches["status/media_thumb.mustache"].replace("is-gif", "is-gif is-paused");
app.delegate(".js-gif-play", "click", function(e){ app.delegate(".js-gif-play", "click", function(e){
var parent = $(e.target).closest(".js-tweet").first(); let parent = $(e.target).closest(".js-tweet").first();
var link = (parent.hasClass("tweet-detail") ? parent.find("a[rel='url']") : parent.find("time").first().children("a")).first(); let link = (parent.hasClass("tweet-detail") ? parent.find("a[rel='url']") : parent.find("time").first().children("a")).first();
$TD.openBrowser(link.attr("href")); $TD.openBrowser(link.attr("href"));
e.stopPropagation(); e.stopPropagation();
@ -763,7 +768,7 @@
// Block: Fix youtu.be previews not showing up for https links. // Block: Fix youtu.be previews not showing up for https links.
// //
if (ensurePropertyExists(TD, "services", "TwitterMedia")){ if (ensurePropertyExists(TD, "services", "TwitterMedia")){
var media = TD.services.TwitterMedia; let media = TD.services.TwitterMedia;
if (!ensurePropertyExists(media, "YOUTUBE_TINY_RE") || if (!ensurePropertyExists(media, "YOUTUBE_TINY_RE") ||
!ensurePropertyExists(media, "YOUTUBE_LONG_RE") || !ensurePropertyExists(media, "YOUTUBE_LONG_RE") ||
@ -855,6 +860,7 @@
// //
$(document).one("TD.ready", function(){ $(document).one("TD.ready", function(){
onAppReady.forEach(func => func()); onAppReady.forEach(func => func());
onAppReady = null;
$TD.loadFontSizeClass(TD.settings.getFontSize()); $TD.loadFontSizeClass(TD.settings.getFontSize());
$TD.loadNotificationHeadContents(getNotificationHeadContents()); $TD.loadNotificationHeadContents(getNotificationHeadContents());