Skip to content

Commit

Permalink
Added custom template
Browse files Browse the repository at this point in the history
Now one can specify custom filename templates via context menu; {handle} will be replaced with AuthorHandle,
{OR} will be replaced with Tags Origin, {name} will be replaced with Author Name,
{caption} will be replaced with Picture Caption, {tags} will be replaced with a whole tags string.
  • Loading branch information
Brawlence committed Jan 20, 2020
1 parent 4a3ad5f commit be1d825
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 76 deletions.
5 changes: 0 additions & 5 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ image: node:latest
before_script:
- npm install --global web-ext

verify:
stage: build
script:
- web-ext lint --source-dir=./Extension/

submit_amo:
stage: build
only:
Expand Down
15 changes: 10 additions & 5 deletions Extension/AS/tagsParser.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
var tagsOrigin = "AS";
var windowDisplacement = 90;

function getImageTags() {
function getImageTags(template) {
var resultingTags = new Array;
var profilelink = document.querySelector('aside div.name a').href;

var authorHandle = profilelink.substring(profilelink.lastIndexOf('/')+1);
var authorName = document.querySelector('aside div.name a').innerText;
var pictureName = document.querySelector('aside div h1.h3').innerText;
var tempArray = document.getElementsByClassName("tags")[0].innerText.substring(7).split('#');

resultingTags.push(authorHandle + "@" + tagsOrigin);
resultingTags.push(authorName.replace(/[ ]/g, '-'));
resultingTags.push(pictureName.replace(/[ ]/g, '-'));
template = template.replace(/\{handle\}/g, authorHandle.replace(/[ \n\t\r\v\f]/g, '-'));
template = template.replace(/\{OR\}/g, tagsOrigin);
template = template.replace(/\{name\}/g, authorName.replace(/[ \n\t\r\v\f]/g, '-'));
template = template.replace(/\{caption\}/g, pictureName.replace(/[ \n\t\r\v\f]/g, '-'))

for (var i = 0; i < tempArray.length; i++) {
resultingTags.push(tempArray[i]);
template = template.replace(/\{tags\}/g, tempArray[i] + ' {tags}');
};
template = template.replace(/ \{tags\}/g, '');

resultingTags = template.split(' ');
return resultingTags;
};
50 changes: 31 additions & 19 deletions Extension/Background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var invokeSaveAs = true;
var keepAuthorTags = true;
var fileNameTemplate = "{handle}@{OR} {name} {caption} {tags}";
var firefoxEnviroment = false;
var useIcons = false;

Expand Down Expand Up @@ -130,22 +130,13 @@ var sir = {
sir.makeMenuItem("dl", "Download with tags","Icons/dl.png", false, useIcons);
sir.makeMenuItem("gts","Get tags string", "Icons/gts.png", false, useIcons);

chrome.contextMenus.create({type:"separator", id:"separator"});

chrome.contextMenus.create({
type: "checkbox",
id: "saveSilently",
title: "Supress \"Save As\" dialog?",
checked: !invokeSaveAs,
contexts: ["image"]
});
chrome.contextMenus.create({
type: "checkbox",
id: "keepAuthorTag",
title: "Keep author tags (author@OR)?",
checked: keepAuthorTags,
contexts: ["image"]
})
chrome.contextMenus.create({type: "separator", id:"separator1", contexts: ["image"]});

sir.makeMenuItem("tmpl","Specify custom filename template...", "Icons/no_gts.png", true, useIcons);

chrome.contextMenus.create({type: "separator", id:"separator2", contexts: ["image"]});

chrome.contextMenus.create({type: "checkbox", id: "saveSilently", title: "Supress \"Save As\" dialog?", checked: !invokeSaveAs, contexts: ["image"]});
},

//gets browser info and passes it to makeMenuItems to determine if things like icons are supported
Expand All @@ -162,24 +153,28 @@ var sir = {
if (useIcons) {
chrome.contextMenus.update("dl", {icons: {"16": "Icons/no_dl.png"}, title: "No tags were fetched from this page", enabled: false});
chrome.contextMenus.update("gts", {icons: { "16": "Icons/no_gts.png" }, enabled: false});
chrome.contextMenus.update("tmpl", {enabled: false});
} else {
chrome.contextMenus.update("dl", {title: "No tags were fetched from this page", enabled: false});
chrome.contextMenus.update("gts", {enabled: false});
chrome.contextMenus.update("tmpl", {enabled: false});
}
},

enableMenu: function () {
if (useIcons) {
chrome.contextMenus.update("dl", {icons: { "16": "Icons/dl.png" }, title: "Download with tags", enabled: true});
chrome.contextMenus.update("gts", { icons: { "16": "Icons/gts.png" }, enabled: true});
chrome.contextMenus.update("tmpl", {enabled: true});
} else {
chrome.contextMenus.update("dl", {title: "Download with tags", enabled: true});
chrome.contextMenus.update("gts", {enabled: true});
chrome.contextMenus.update("tmpl", {enabled: true});
}
},

invokeTagsField: function (tabId) {
chrome.tabs.sendMessage(tabId, { order: "getTagsString" },
chrome.tabs.sendMessage(tabId, { order: "getTagsString", template: fileNameTemplate },
function justWaitTillFinished(response) {
if (chrome.runtime.lastError) {
//console.warn(chrome.runtime.lastError.message);
Expand Down Expand Up @@ -214,6 +209,20 @@ var sir = {
};
},

promptTemplate: function (tabId, stubTemplate) {
chrome.tabs.sendMessage(tabId, { order: "askForTemplate", stub: stubTemplate },
function updateTemplate(response) {
if (chrome.runtime.lastError) {
//console.warn(chrome.runtime.lastError.message);
} else {
if (typeof response !== 'undefined') {
if ((response.newTemplate !== "") && ( response.newTemplate !== null)) fileNameTemplate = response.newTemplate;
}
}
}
);
},

initialize: function () {
if (navigator.userAgent.indexOf('Firefox') > -1) {
firefoxEnviroment = true;
Expand Down Expand Up @@ -247,7 +256,7 @@ var sir = {
},

dlWithTags: function (imageObject, tabId) {
chrome.tabs.sendMessage(tabId, { order: "giffTags" }, // ! tabId is an integer
chrome.tabs.sendMessage(tabId, { order: "giffTags", template: fileNameTemplate }, // ! tabId is an integer
function workWithThis(response) {
if (chrome.runtime.lastError) {
//console.warn(chrome.runtime.lastError.message);
Expand Down Expand Up @@ -343,6 +352,9 @@ chrome.contextMenus.onClicked.addListener(function (info, tab) { // ! info is an
case 'saveSilently':
invokeSaveAs = !invokeSaveAs;
break;
case 'tmpl':
sir.promptTemplate(tab.id, fileNameTemplate);
break;
case 'gts':
sir.invokeTagsField(tab.id); // ! so tab.id will be passed
break;
Expand Down
30 changes: 20 additions & 10 deletions Extension/Content.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// A unified content script for almost ALL the sites, relies on defined getImageTags(); in XX\tagsParser.js
"use strict";

function createTagsStringField() {
function createTagsStringField(template) {
if (document.getElementById('sirArea') === null) {
var arrayOfTags = getImageTags();
var arrayOfTags = getImageTags(template);
var tagsString = "";
for (var i = 0; i < arrayOfTags.length; i++) {
tagsString += arrayOfTags[i].replace(/ /g, '_') + " ";
Expand Down Expand Up @@ -48,14 +48,24 @@ function createTagsStringField() {

chrome.runtime.onMessage.addListener(
function(request, _sender, sendResponse) {
if (request.order === "ping") {
sendResponse({message: true, origin: tagsOrigin});
} else if(request.order === "giffTags") {
sendResponse({ tags: getImageTags(), origin: tagsOrigin });
} else if (request.order === "getTagsString") {
createTagsStringField();
} else if (request.order === "displayWarning") {
alert(request.warning);
switch (request.order) {
case 'ping':
sendResponse({message: true, origin: tagsOrigin});
break;
case 'giffTags':
sendResponse({tags: getImageTags(request.template), origin: tagsOrigin});
break;
case 'getTagsString':
createTagsStringField(request.template);
break;
case 'displayWarning':
alert(request.warning);
break;
case 'askForTemplate':
sendResponse({newTemplate: prompt("\t\t\t\tCUSTOM TEMPLATE\t\t\t\t\n{handle} - Author Handle (unique for the platform)\n{OR} - Tags Origin (AS, DA, DF, HF, PX, TU, TW)\n{name} - Author name\n{caption} - Picture caption\n{tags} - Tags string\n\nPlease specify your custom template:", request.stub)});
break;
default:
break;
};
}
);
16 changes: 12 additions & 4 deletions Extension/DA/tagsParser.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
var tagsOrigin = "DA";
var windowDisplacement = 0;

function getImageTags() {
function getImageTags(template) {
var resultingTags = new Array;


var authorHandle = document.URL.substring(document.URL.lastIndexOf('.com/')+5,document.URL.lastIndexOf('/art/'));
var authorName = "";
var pictureName = document.URL.substring(document.URL.lastIndexOf('/art/')+5,document.URL.lastIndexOf('-'));
var tempArray = document.querySelectorAll("[href*='/tag/']");

resultingTags.push(authorHandle + "@" + tagsOrigin);
resultingTags.push(pictureName.replace(/[ ]/g, '-')); //replace is not needed
template = template.replace(/\{handle\}/g, authorHandle.replace(/[ \n\t\r\v\f]/g, '-'));
template = template.replace(/\{OR\}/g, tagsOrigin);
template = template.replace(/\{name\}/g, authorName.replace(/[ \n\t\r\v\f]/g, '-'));
template = template.replace(/\{caption\}/g, pictureName.replace(/[ \n\t\r\v\f]/g, '-'))

for (var i = 0; i < tempArray.length; i++) {
resultingTags.push(tempArray[i].innerText.replace(/[#]/g, '')); // Eclipse design has no hash here #, old site has hash
template = template.replace(/\{tags\}/g, tempArray[i].innerText.replace(/[#]/g, '') + ' {tags}'); // Eclipse design has no hash here #, old site has hash
};
template = template.replace(/ \{tags\}/g, '');

resultingTags = template.split(' ');
return resultingTags;
};
42 changes: 30 additions & 12 deletions Extension/DF/tagsParser_content_script.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
"use strict";
var tagsOrigin = "DF";

// Drawfriends is a special case because it already HAS a field with all the tags.
function getImageTags() {
function getImageTags(template) {
var resultingTags = new Array;
resultingTags = document.querySelector('td textarea[id="tags"]').innerHTML.replace(/_\(artist\)/g, '@DF').split(' ');

var tempString = document.querySelector('div [id="tag_list"]').innerText.trim();
var tempString = document.querySelector('td textarea[id="tags"]').innerHTML;
if (template.indexOf('@{OR}') > -1) { // TODO: FIX THIS to a proper template renaming
tempString = tempString.replace(/_\((art|color)ist\)/g, '@DF');
} else {
tempString = tempString.replace(/\s[\w]+?_\((art|color)ist\)/g, ' ');
};
resultingTags = tempString.split(' ');

tempString = document.querySelector('div [id="tag_list"]').innerText.trim();
resultingTags.unshift("drawfriends_" + tempString.substring(tempString.indexOf('Id: ') + 4, tempString.indexOf('\nPosted: '))); //add the drawfriends_ ID to the tags array

return resultingTags;
Expand Down Expand Up @@ -42,14 +50,24 @@ function createTagsStringField() {

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.order === "ping") {
sendResponse({message: true, origin: "DF"});
} else if (request.order === "giffTags") {
sendResponse({ tags: getImageTags(), origin: "DF" });
} else if (request.order === "getTagsString") {
createTagsStringField();
} else if (request.order === "displayWarning") {
alert(request.warning);
switch (request.order) {
case 'ping':
sendResponse({message: true, origin: tagsOrigin});
break;
case 'giffTags':
sendResponse({tags: getImageTags(request.template), origin: tagsOrigin});
break;
case 'getTagsString':
createTagsStringField();
break;
case 'displayWarning':
alert(request.warning);
break;
case 'askForTemplate':
sendResponse({newTemplate: prompt("WARNING: currently the booru parser only looks if '@{OR}' is present in the template.\nPlease specify your custom template:", request.stub)});
break;
default:
break;
};
}
);
15 changes: 11 additions & 4 deletions Extension/HF/tagsParser.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
var tagsOrigin = "HF";
var windowDisplacement = 0;

function getImageTags() {
function getImageTags(template) {
var resultingTags = new Array;
var urlDivided = document.URL.substring(document.URL.lastIndexOf('/user/')+6).split('/');

var authorHandle = urlDivided[0];
var authorName = "";
var pictureName = urlDivided[2];
var tempArray = document.querySelectorAll('div.boxbody td a[rel="tag"]');

resultingTags.push(authorHandle + "@" + tagsOrigin);
resultingTags.push(pictureName.replace(/[ ]/g, '-')); //replacement is not needed
template = template.replace(/\{handle\}/g, authorHandle.replace(/[ \n\t\r\v\f]/g, '-'));
template = template.replace(/\{OR\}/g, tagsOrigin);
template = template.replace(/\{name\}/g, authorName.replace(/[ \n\t\r\v\f]/g, '-'));
template = template.replace(/\{caption\}/g, pictureName.replace(/[ \n\t\r\v\f]/g, '-'))

for (var i = 0; i < tempArray.length; i++) {
resultingTags.push(tempArray[i].innerText.replace(/[#]/g, ''));
template = template.replace(/\{tags\}/g, tempArray[i].innerText.replace(/[#]/g, '') + ' {tags}');
};
template = template.replace(/ \{tags\}/g, '');

resultingTags = template.split(' ');
return resultingTags;
};
18 changes: 13 additions & 5 deletions Extension/PX/tagsParser.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
var tagsOrigin = "PX";
var windowDisplacement = 0;

function getImageTags() {
function getImageTags(template) {
var resultingTags = new Array;


var authorHandle = document.querySelector('aside section h2 div div a div').innerText;
var authorName = "";
var pictureName = document.querySelector('figcaption div div h1').innerText;
var tempArray = document.querySelectorAll('figcaption div footer ul li a');

resultingTags.push(authorHandle.replace(/[ ]/g, '-') + "@" + tagsOrigin);
resultingTags.push(pictureName.replace(/[ ]/g, '-'));

template = template.replace(/\{handle\}/g, authorHandle.replace(/[ \n\t\r\v\f]/g, '-'));
template = template.replace(/\{OR\}/g, tagsOrigin);
template = template.replace(/\{name\}/g, authorName.replace(/[ \n\t\r\v\f]/g, '-'));
template = template.replace(/\{caption\}/g, pictureName.replace(/[ \n\t\r\v\f]/g, '-'))

for (var i = 0; i < tempArray.length; i++) {
resultingTags.push(tempArray[i].innerText);
template = template.replace(/\{tags\}/g, tempArray[i].innerText + ' {tags}');
};
template = template.replace(/ \{tags\}/g, '');

resultingTags = template.split(' ');
return resultingTags;
};
16 changes: 12 additions & 4 deletions Extension/TU/tagsParser.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
var tagsOrigin = "TU";
var windowDisplacement = 0;

function getImageTags() {
function getImageTags(template) {
var resultingTags = new Array;


var authorHandle = document.URL.substring(document.URL.lastIndexOf('://')+3,document.URL.lastIndexOf('.tumblr'));
var authorName = document.querySelector("header div figcaption, header div h1 a").innerText; //it's either one or the other
var pictureName = "";
var tempArray = document.querySelectorAll("[href*='/tagged/']");

resultingTags.push(authorHandle + "@" + tagsOrigin);
resultingTags.push(authorName.replace(/[ ]/g, '-'));
template = template.replace(/\{handle\}/g, authorHandle.replace(/[ \n\t\r\v\f]/g, '-'));
template = template.replace(/\{OR\}/g, tagsOrigin);
template = template.replace(/\{name\}/g, authorName.replace(/[ \n\t\r\v\f]/g, '-'));
template = template.replace(/\{caption\}/g, pictureName.replace(/[ \n\t\r\v\f]/g, '-'))

for (var i = 0; i < tempArray.length; i++) {
resultingTags.push(tempArray[i].innerText.replace(/[#]/g, ''));
template = template.replace(/\{tags\}/g, tempArray[i].innerText.replace(/[#]/g, '') + ' {tags}');
};
template = template.replace(/ \{tags\}/g, '');

resultingTags = template.split(' ');
return resultingTags;
};
Loading

0 comments on commit be1d825

Please sign in to comment.