Skip to content

Commit

Permalink
Add alternative extraction method.
Browse files Browse the repository at this point in the history
Looks at the srcset of the image on the page, which has the advantage of getting
the currently selected pictures if there are multiple in the set.
  • Loading branch information
mihaip committed Jan 25, 2018
1 parent ed86690 commit f028c81
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
2 changes: 1 addition & 1 deletion extension/manifest.json
Expand Up @@ -21,5 +21,5 @@
"activeTab",
"downloads"
],
"version": "1.0.1"
"version": "1.0.2"
}
75 changes: 50 additions & 25 deletions extension/script.js
@@ -1,36 +1,61 @@
var imageUrl = document.querySelector("meta[property='og:image']").getAttribute("content");
var filename;

try {
// We can't access the window._sharedData variable directly, since we're in
// an isolated world. But we can extract the script text and parse it.
var sharedDataScriptNode = Array.prototype.find.call(
document.querySelectorAll("script"),
node => node.textContent.includes("window._sharedData"));
var sharedDataJson = sharedDataScriptNode.textContent;
sharedDataJson = sharedDataJson
.replace(/^window\._sharedData\s*=\s*/, "")
.replace(/;*\s*$/, "");
var sharedData = JSON.parse(sharedDataJson);

var caption = sharedData.entry_data.PostPage[0].media.caption;
function run() {
try {
const imageNode = document.querySelector("img[srcset]");
const srcMap = new Map(imageNode.srcset
.split(",")
.map(src => src.split(" ").reverse()));
const imageUrl = srcMap.get("1080w");

chrome.runtime.sendMessage({
type: "download",
url: imageUrl,
filename: generateFilename(imageNode.alt)
});
return;
} catch (err) {
console.warn("Could not extract current image, falling back on metadata.");
}

var imageUrl = document.querySelector("meta[property='og:image']")
.getAttribute("content");
var filename;

try {
// We can't access the window._sharedData variable directly, since we're in
// an isolated world. But we can extract the script text and parse it.
var sharedDataScriptNode = Array.prototype.find.call(
document.querySelectorAll("script"),
node => node.textContent.includes("window._sharedData"));
var sharedDataJson = sharedDataScriptNode.textContent;
sharedDataJson = sharedDataJson
.replace(/^window\._sharedData\s*=\s*/, "")
.replace(/;*\s*$/, "");
var sharedData = JSON.parse(sharedDataJson);

var caption = sharedData.entry_data.PostPage[0].media.caption;
filename = generateFilename(caption);
} catch (ex) {};

chrome.runtime.sendMessage({
type: "download",
url: imageUrl,
filename: filename
});
}

function generateFilename(caption) {
// Remove hashtags, question marks and trailing periods.
caption = caption
.replace(/#\w+/g, "")
.trim()
.replace(/[?:]/g, "")
.replace(/[?:"]/g, "")
.replace(/\.+$/, "")
.trim();

if (caption.length > 200) {
caption = caption.substring(0, 200);
}
return caption + ".jpeg";
}

filename = caption + ".jpeg";
} catch (ex) {};

chrome.runtime.sendMessage({
type: "download",
url: imageUrl,
filename: filename
});
run();

0 comments on commit f028c81

Please sign in to comment.