Skip to content

Commit

Permalink
Merge pull request #14 from ddasutein/update
Browse files Browse the repository at this point in the history
Version 2.1.1
  • Loading branch information
ddasutein authored Jul 25, 2021
2 parents c5b50d6 + 0201f46 commit 6944b85
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 56 deletions.
164 changes: 110 additions & 54 deletions js/Event/SaveAsEventHandle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** MIT License
*
* Copyright (c) 2020 Dasutein
* Copyright (c) 2021 Dasutein
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
Expand All @@ -14,17 +14,16 @@

/* ---------------------CONTEXT MENU ITEMS----------------------- */
chrome.contextMenus.create({
"id": "saveImage",
"title": chrome.i18n.getMessage("context_menu_save_image_as"),
"contexts": ["image"]
id: "saveImage",
title: chrome.i18n.getMessage("context_menu_save_image_as"),
contexts: ["image"]
});

// Twitter Specific Context Menu Item
const viewOriginalImageSizeContextMenuItem = chrome.contextMenus.create({
"id": "viewOriginalImageSizeContextMenuItem",
"title": chrome.i18n.getMessage("context_menu_view_original_image"),
"contexts": ["image"],
"visible": false
chrome.contextMenus.create({
id: "viewOriginalImageSizeContextMenuItem",
title: chrome.i18n.getMessage("context_menu_view_original_image"),
contexts: ["image"]
});

/* ----------END OF CONTEXT MENU ITEMS FUNCTIONS------------------ */
Expand All @@ -44,79 +43,134 @@ const Website = {
* Global parameters to store browser tab information. This can be called
* on any part of the extension as needed
*/
var BrowserTabInfo = {
let BrowserTabInfo = {
Title: "",
URL: ""
}

/* Listens for URL from address bar when browser window is opened or entering a new URL */
chrome.tabs.onUpdated.addListener(function(tabID, changeInfo, tab){

if (changeInfo.status == "complete"){
if (DevMode){
const DEBUG_TAG = "tabsOnUpdated => ";
console.log(DEBUG_TAG + tab.url + " " + tab.title);
}
BrowserTabInfo.URL = tab.url;
BrowserTabInfo.Title = tab.title;
ToggleViewOriginalImageContextMenuVisibility(tab.url)
/**
* When the user changes tabs, the extension should be able to grab
* the URL and Title
*/
chrome.tabs.onActivated.addListener((activeInfo) => {

DevMode ? console.log("tabs onActivated") : "";
QueryTab();

});

/**
* When the user clicks a link on the same tab, the extension should be able to
* get the updated data.
*/
chrome.tabs.onUpdated.addListener((tabId, selectInfo) => {
DevMode ? console.log("-- on update --") : ""

if (selectInfo.status == "complete"){
QueryTab();
}

});

/* Listens for tab change by the user */
chrome.tabs.onActiveChanged.addListener(function(){
function QueryTab() {

chrome.tabs.query({
"active": true,
"currentWindow": true},
setTimeout(() => {

function(tabs){
if (DevMode){
const DEBUG_TAG = "tabsOnActiveChanged => ";
console.log(DEBUG_TAG + tabs[0].url);
chrome.tabs.query({
active: true,
currentWindow: true
}, ((tabs)=>{

let url;
let title;

if (tabs[0] != undefined){
DevMode ? console.log(BrowserTabInfo) : "";
url = tabs[0].url;
title = tabs[0].title;
}

ToggleViewOriginalImageContextMenuVisibility(tabs[0].url);
url = url.split("/");
url = url[2];
console.log(url);
UpdateContextMenus(url);

BrowserTabInfo.URL = tabs[0].url;
BrowserTabInfo.Title = tabs[0].title;

},
);
});
BrowserTabInfo.Title = title;
BrowserTabInfo.URL = url;
console.log(BrowserTabInfo)
}));

function ToggleViewOriginalImageContextMenuVisibility(url){
const currentUrl = url;
const currentUrlSplit = currentUrl.split("/");
const currentWebsite = currentUrlSplit[2];

switch(currentWebsite){
}, 500);

};

function UpdateContextMenus(url) {

switch(url){
case Website.Twitter:
chrome.contextMenus.update(viewOriginalImageSizeContextMenuItem, {

chrome.contextMenus.update("saveImage", {
visible: true
});

chrome.contextMenus.update("viewOriginalImageSizeContextMenuItem", {
"visible": true
});
break;

case Website.Mobile_Twitter:
chrome.contextMenus.update(viewOriginalImageSizeContextMenuItem, {

chrome.contextMenus.update("saveImage", {
visible: true
});

chrome.contextMenus.update("viewOriginalImageSizeContextMenuItem", {
"visible": true
});
break;
default:
chrome.contextMenus.update(viewOriginalImageSizeContextMenuItem, {

case Website.LINE_BLOG:

chrome.contextMenus.update("saveImage", {
visible: true
});

chrome.contextMenus.update("viewOriginalImageSizeContextMenuItem", {
"visible": false
});
break;

default:

if (Object.keys(Website).map(key => Website[key]).indexOf(url) == -1){
DevMode ? console.log("website not supported. removing context menu items") : "";

chrome.contextMenus.update("viewOriginalImageSizeContextMenuItem", {
"visible": false
});

chrome.contextMenus.update("saveImage", {
visible: false
});

} else {
DevMode ? console.log("add saveImage context menu item") : "";
chrome.contextMenus.update("saveImage", {
visible: true
});
}
break;
}
}
};

/* Execute everything when save image as is clicked. */
chrome.contextMenus.onClicked.addListener(function (info, tab) {

const currentUrl = tab.url;
const currentUrlSplit = currentUrl.split("/");
const currentWebsite = currentUrlSplit[2];
let currentUrl = tab.url;
currentUrl = currentUrl.split("/");
currentUrl = currentUrl[2];

switch (currentWebsite) {
switch (currentUrl) {
case Website.Twitter:

if (info.menuItemId === "viewOriginalImageSizeContextMenuItem"){
Expand All @@ -128,6 +182,7 @@ chrome.contextMenus.onClicked.addListener(function (info, tab) {

}
break;

case Website.Mobile_Twitter:
if (info.menuItemId === "viewOriginalImageSizeContextMenuItem"){
ViewOriginalMedia(info.srcUrl);
Expand All @@ -137,7 +192,8 @@ chrome.contextMenus.onClicked.addListener(function (info, tab) {
SaveTwitterMedia(tab.url, info.srcUrl);

}
break;
break;

case Website.LINE_BLOG:
SaveLINEBLOGMedia(tab.url, info.srcUrl);
break;
Expand All @@ -146,4 +202,4 @@ chrome.contextMenus.onClicked.addListener(function (info, tab) {
break;
}

});
});
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "AutoRename",
"short_name": "AutoRename",
"version": "2.1.0",
"version": "2.1.1",
"description": "Rename image files from the internet with a meaningful file name",
"default_locale": "en",
"manifest_version": 2,
Expand Down
2 changes: 1 addition & 1 deletion options_about.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<td>AutoRename</td>
</tr>
<tr>
<td>Copyright © 2020 Dasutein</td>
<td>Copyright © 2021 Dasutein</td>
<td><a href="https://github.com/ddasutein/AutoRename#mit-license">MIT License</a></td>
</tr>
<tr>
Expand Down

0 comments on commit 6944b85

Please sign in to comment.