Last active
April 12, 2020 13:28
-
-
Save hogashi/11303cf67546644476bd1a533e89c6f9 to your computer and use it in GitHub Desktop.
unsupported script: tooi-like foxy script for save-in https://github.com/hogashi/twitterOpenOriginalImage/issues/69
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this is unsupported | |
// added from https://github.com/hogashi/twitterOpenOriginalImage/blob/master/tooi-forGreaseTamperMonkey.user.js | |
var collectUrlParams = function (rawUrl) { | |
if (!/https:\/\/pbs\.twimg\.com\/media/.test(rawUrl)) { | |
return null; | |
} | |
var url = new URL(rawUrl); | |
var searchSet = { | |
format: 'jpg', | |
name: null | |
}; | |
url.search | |
.slice(1) | |
.split('&') | |
.forEach(function (set) { | |
var _a = set.split('='), key = _a[0], value = _a[1]; | |
if (key === 'format' || key === 'name') { | |
searchSet[key] = value; | |
} | |
}); | |
var matched = url.pathname.match(/^(.*?)(?:|\.([^.:]+))(?:|:([a-z]+))$/); | |
var pathnameWithoutExtension = matched[1]; | |
var extension = matched[2]; | |
var legacyName = matched[3] || null; | |
return { | |
protocol: url.protocol, | |
host: url.host, | |
pathname: pathnameWithoutExtension, | |
format: extension || searchSet.format, | |
name: searchSet.name || legacyName | |
}; | |
}; | |
var formatUrl = function (imgUrl) { | |
if (!imgUrl || imgUrl.length === 0) { | |
return null; | |
} | |
var params = collectUrlParams(imgUrl); | |
console.log({params}); | |
if (!params) { | |
return imgUrl; | |
} | |
var protocol = params.protocol, host = params.host, pathname = params.pathname, format = params.format; | |
return protocol + "//" + host + pathname + "." + format + "?format=" + format + "&name=orig"; // modified | |
} | |
const source = data.element.mediaInfo && data.element.mediaInfo.source; | |
if (source) { | |
const sourceOriginal = formatUrl(source); // added | |
console.log({ source, sourceOriginal }); | |
const payload = { | |
type: "DOWNLOAD", | |
body: { | |
url: sourceOriginal, // modified | |
info: { pageUrl: `${window.location}`, srcUrl: sourceOriginal, comment: null } // modified | |
} | |
}; | |
browser.runtime.sendMessage("{72d92df5-2aa0-4b06-b807-aa21767545cd}", payload); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment