Skip to content

Commit

Permalink
Merge pull request #881 from suwacrab/master
Browse files Browse the repository at this point in the history
Support custom media download templates
  • Loading branch information
dimdenGD authored Jul 15, 2024
2 parents 18c69c2 + 1bceeca commit 4fef06e
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 9 deletions.
13 changes: 11 additions & 2 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -632,5 +632,14 @@
"anniversary_tweet": { "message": "Do you remember when you joined Twitter? I do!\n#MyTwitterAnniversary" },
"you_shared_tweet": { "message": "You shared a tweet" },
"user_shared_tweet": { "message": "$NAME$ shared a tweet", "placeholders": { "name": { "content": "Display Name" } } },
"transition_profile_banner": { "message": "Use transition on profile banner" }
}
"transition_profile_banner": { "message": "Use transition on profile banner" },
"custom_downloadtemplate": { "message": "Custom download template" },
"custom_downloadtemplate_text": { "message": "Allows you to customize the names of the files when downloading a tweet's media." },
"save_downloadtemplate": { "message": "Save download template" },
"custom_downloadtemplate_hintName1": { "message": "<code>{user_name}</code>: User's name." },
"custom_downloadtemplate_hintName2": { "message": "<code>{user_screen_name}</code>: User's account name. (Example: @dril)" },
"custom_downloadtemplate_hintTime": { "message": "<code>{timestamp}</code>: Tweet's post date & time." },
"custom_downloadtemplate_hintExtension": { "message": "<code>{extension}</code>: Downloaded tweet's file extension." },
"custom_downloadtemplate_hintID": { "message": "<code>{id}</code>: Tweet's ID." },
"custom_downloadtemplate_hintIndex": { "message": "<code>{index}</code>: Index for each file to download. (for posts with multiple media)" }
}
16 changes: 15 additions & 1 deletion layouts/settings/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,20 @@ <h1>__MSG_custom_css__</h1><br>
<button class="nice-button" id="custom-css-save">__MSG_save_css__</button>
<br>
<hr>
<h1>__MSG_custom_downloadtemplate__</h1><br>
<span>__MSG_custom_downloadtemplate_text__</span><br>
<br><span>__MSG_custom_downloadtemplate_hintName1__</span>
<br><span>__MSG_custom_downloadtemplate_hintName2__</span>
<br><span>__MSG_custom_downloadtemplate_hintTime__</span>
<br><span>__MSG_custom_downloadtemplate_hintExtension__</span>
<br><span>__MSG_custom_downloadtemplate_hintID__</span>
<br><span>__MSG_custom_downloadtemplate_hintIndex__</span>
<br><br>
<div class="setting">
<input type="text" style="width: 550px;" id="custom-download" placeholder="{user_screen_name}_{timestamp}_{id}{index}.{extension}"></input>
</div>
<button class="nice-button" id="custom-download-save">__MSG_save_downloadtemplate__</button>
<hr>
<h1>__MSG_advanced_options__</h1><br>
<div class="setting" id="advanced-settings">
<button class="nice-button" id="import-settings">__MSG_import_settings__</button>
Expand Down Expand Up @@ -437,4 +451,4 @@ <h1>__MSG_who_to_follow__</h1><br>
</div>
</main>
</body>
</html>
</html>
13 changes: 12 additions & 1 deletion layouts/settings/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ setTimeout(async () => {
let disableFindHotkey = document.getElementById('disable-find-hotkey');
let customCSS = document.getElementById('custom-css');
let customCSSSave = document.getElementById('custom-css-save');
let customDownloadTemplate = document.getElementById('custom-download');
let customDownloadTemplateSave = document.getElementById('custom-download-save');
let savePreferredQuality = document.getElementById('save-preferred-quality');
let roundAvatars = document.getElementById('round-avatars-switch');
let modernUI = document.getElementById('modern-ui-switch');
Expand Down Expand Up @@ -907,6 +909,14 @@ setTimeout(async () => {
console.error("Error saving CSS to DB:", error);
});
});
customDownloadTemplateSave.addEventListener('click', () => {
let val = customDownloadTemplate.value;

vars.customDownloadTemplate = val;
chrome.storage.sync.set({
customDownloadTemplate: val
}, () => { });
});
autotranslateLanguageList.addEventListener('change', () => {
addAutotranslateLanguage.disabled = autotranslateLanguageList.value === 'select';
});
Expand Down Expand Up @@ -1046,6 +1056,7 @@ setTimeout(async () => {
writeCSSToDB(vars.customCSS)
}
customCSS.value = await readCSSFromDB();
customDownloadTemplate.value = vars.customDownloadTemplate;
document.getElementById('stt-div').hidden = vars.timelineType !== 'algo' && vars.timelineType !== 'algov2';
savePreferredQuality.checked = !!vars.savePreferredQuality;
showOriginalImages.checked = !!vars.showOriginalImages;
Expand Down Expand Up @@ -1281,4 +1292,4 @@ setTimeout(async () => {
setInterval(updateUserData, 60000 * 3);
setInterval(() => renderDiscovery(false), 60000 * 15);
setInterval(renderTrends, 60000 * 5);
}, 50);
}, 50);
10 changes: 8 additions & 2 deletions scripts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function loadVars() {
'acknowledgedCustomizationButton', 'modernUI', 'showExactValues', 'hideTimelineTypes', 'autotranslateLanguages',
'autotranslationMode', 'muteVideos', 'dontPauseVideos', 'showUserPreviewsOnMobile', 'systemDarkMode', 'localizeDigit',
'disableRetweetHotkey', 'disableLikeHotkey', 'disableFindHotkey', 'extensionCompatibilityMode', 'disableDataSaver', 'disableAcceptType',
'showUserFollowerCountsInLists', 'showQuoteCount', 'hideUnfollowersPage', 'transitionProfileBanner'
'showUserFollowerCountsInLists', 'showQuoteCount', 'hideUnfollowersPage', 'transitionProfileBanner', 'customDownloadTemplate'
], data => {
// default variables
if(typeof(data.linkColorsInTL) !== 'boolean') {
Expand Down Expand Up @@ -77,6 +77,12 @@ async function loadVars() {
customCSSVariables: ''
}, () => {});
}
if(typeof(data.customDownloadTemplate) !== 'string') {
data.customDownloadTemplate = '';
chrome.storage.sync.set({
customDownloadTemplate: ''
}, () => {});
}
if(typeof(data.copyLinksAs) !== 'string') {
data.copyLinksAs = 'twitter.com';
chrome.storage.sync.set({
Expand Down Expand Up @@ -234,4 +240,4 @@ async function loadVars() {
};

console.log(1, vars);
loadVars();
loadVars();
16 changes: 15 additions & 1 deletion scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3542,8 +3542,22 @@ async function appendTweet(t, timelineContainer, options = {}) {
let extension = url.pathname.split('.').pop();
let _index = t.extended_entities.media.length > 1 ? "_"+(index+1) : "";
let filename = `${t.user.screen_name}_${ts}_${t.id_str}${_index}.${extension}`;
a.download = filename;
let filename_template = vars.customDownloadTemplate;

// use the filename from the user's custom download template, if any
if(filename_template && (filename_template.length > 0)) {
const filesave_map = {
"user_screen_name": t.user.screen_name,
"user_name": t.user.name,
"extension": extension,
"timestamp": ts,
"id": t.id_str,
"index": _index
};
filename = filename_template.replace(/\{([\w]+)\}/g, (_, key) => filesave_map[key]);
}

a.download = filename;
a.click();
a.remove();
}).catch(e => {
Expand Down
24 changes: 22 additions & 2 deletions scripts/tweetviewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2455,8 +2455,28 @@ class TweetViewer {
downloading = false;
let a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = media.type === 'photo' ? media.media_url_https.split('/').pop() : media.video_info.variants[0].url.split('/').pop();
a.download = a.download.split('?')[0];

let ts = new Date(t.created_at).toISOString().split("T")[0];
let extension = url.split('.').pop();
//let _index = t.extended_entities.media.length > 1 ? "_"+(index+1) : "";
let _index = "";
let filename = `${t.user.screen_name}_${ts}_${t.id_str}${_index}.${extension}`;
let filename_template = vars.customDownloadTemplate;

// use the filename from the user's custom download template, if any
if(filename_template && (filename_template.length > 0)) {
const filesave_map = {
"user_screen_name": t.user.screen_name,
"user_name": t.user.name,
"extension": extension,
"timestamp": ts,
"id": t.id_str,
"index": _index
};
filename = filename_template.replace(/\{([\w]+)\}/g, (_, key) => filesave_map[key]);
}

a.download = filename;
a.click();
a.remove();
}).catch(e => {
Expand Down

0 comments on commit 4fef06e

Please sign in to comment.