Created
March 13, 2018 17:25
-
-
Save anonymous/5a2162b67c3e197f10c5c4238ea40d0b to your computer and use it in GitHub Desktop.
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
// ==UserScript== | |
// @name Load image links 2ch | |
// @include https://2ch.*/*/res/*.html* | |
// @grant GM_addStyle | |
// ==/UserScript== | |
var linkgrab = linkgrab || {}; | |
linkgrab.file_ext = [".jpg", ".jpeg", ".png", ".mp4", ".gif", ".webm"]; | |
linkgrab.checkFileExt = function(href) { | |
for (var i = 0; i < linkgrab.file_ext.length; i++) { | |
if (href.indexOf(linkgrab.file_ext[i]) !== -1) { | |
return true; | |
} | |
} | |
return false; | |
} | |
linkgrab.makeArrayOfHref = function() { | |
var a = document.getElementsByClassName("post-wrapper"); | |
var str_links = new Array(); | |
for (var i = 0; i < a.length; i++) { | |
if (a[i].childNodes[1].childNodes[1].childNodes[1].checked) { | |
a[i].childNodes[1].childNodes[1].childNodes[1].checked = false; | |
var pics = a[i].getElementsByTagName('a'); | |
for (var j = 0; j < pics.length; j++) { | |
if (linkgrab.checkFileExt(pics[j].href) && !(str_links.indexOf(pics[j].href) > -1)) { | |
str_links.push(pics[j].href); | |
} | |
} | |
} | |
} | |
return str_links; | |
} | |
linkgrab.textFile = null; | |
linkgrab.makeTextFile = function(text) { | |
var data = new Blob([text], { | |
type: 'text/plain' | |
}); | |
// If we are replacing a previously generated file we need to | |
// manually revoke the object URL to avoid memory leaks. | |
if (linkgrab.textFile !== null) { | |
window.URL.revokeObjectURL(textFile); | |
} | |
linkgrab.textFile = window.URL.createObjectURL(data); | |
// returns a URL you can use as a href | |
return linkgrab.textFile; | |
}; | |
var a = document.createElement("a"); | |
a.id = 'linkgrab_fileLink'; | |
a.style = "display: none"; | |
document.body.appendChild(a); | |
var zNode = document.createElement('div'); | |
zNode.innerHTML = '<button id="linkgrab_myButton" type="button">Получить ссылки</button>'; | |
document.body.appendChild(zNode); | |
document.getElementById("linkgrab_myButton").addEventListener("click", ButtonClickAction, false); | |
function ButtonClickAction(zEvent) { | |
var str_links2 = linkgrab.makeArrayOfHref(); | |
if (str_links2.length === 0) { | |
return; | |
} | |
//read text from textbox placed in parent window | |
var text = str_links2.join("\n"); | |
var pathArray = window.location.pathname.split('/'); | |
var fileName = pathArray[1] + pathArray[3].split(".")[0]; | |
var url = linkgrab.makeTextFile(text); | |
var a = document.getElementById("linkgrab_fileLink"); | |
a.href = url; | |
a.download = fileName + ".txt"; | |
a.click(); | |
window.URL.revokeObjectURL(url); | |
} | |
//--- Style our newly added elements using CSS. | |
GM_addStyle(multilineStr(function() { | |
/*! | |
#linkgrab_myButton { | |
cursor: pointer; | |
position: fixed; | |
right: 20px; | |
top: 30px; | |
opacity: 0.1; | |
} | |
#linkgrab_myButton:hover { | |
opacity: 1; | |
} | |
*/ | |
})); | |
function multilineStr(dummyFunc) { | |
var str = dummyFunc.toString(); | |
str = str.replace(/^[^\/]+\/\*!?/, '') // Strip function () { /*! | |
.replace(/\s*\*\/\s*\}\s*$/, '') // Strip */ } | |
.replace(/\/\/.+$/gm, '') // Double-slash comments wreck CSS. Strip them. | |
; | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment