|
// ==UserScript== |
|
// @name Button to old page |
|
// @namespace http://tampermonkey.net/ |
|
// @version 0.1 |
|
// @description Show a button to go to the old site! |
|
// @author Baku |
|
// @updateURL https://gist.github.com/kebien6020/f6489944130a8d891dbebffbe4388b08/raw/SankakuButtonToOldSite.user.js |
|
// @downloadURL https://gist.github.com/kebien6020/f6489944130a8d891dbebffbe4388b08/raw/SankakuButtonToOldSite.user.js |
|
// @match https://beta.sankakucomplex.com/* |
|
// @grant none |
|
// @require https://unpkg.com/xhook@latest/dist/xhook.min.js |
|
// @run-at document-start |
|
// ==/UserScript== |
|
|
|
//patch history object |
|
(function(history){ |
|
var pushState = history.pushState; |
|
history.pushState = function(state) { |
|
const retval = pushState.apply(history, arguments); |
|
if (typeof history.onpushstate == "function") { |
|
history.onpushstate({state: state}); |
|
} |
|
return retval; |
|
}; |
|
})(window.history); |
|
|
|
(function(window, history) { |
|
'use strict'; |
|
|
|
const CLASSNAME = 'baku-oldpage-container' |
|
|
|
history.onpushstate = window.onpopstate = window.onload = function () { |
|
const re = /\/post\/show\/(\d+)/ |
|
const matches = re.exec(window.location.pathname) |
|
if (matches) { |
|
const id = matches[1] |
|
const notesClass = window.postsWithNotes && window.postsWithNotes.indexOf(id) !== -1 ? ' notes' : '' |
|
const button = ` |
|
<style> |
|
.${CLASSNAME} { |
|
width: 100%; |
|
pointer-events: none; |
|
} |
|
.${CLASSNAME} a { |
|
position: fixed; |
|
display:block; |
|
bottom: 10px; |
|
right: 10px; |
|
width: 30px; |
|
height: 30px; |
|
background-color: orange; |
|
border-radius: 50%; |
|
z-index: 10000; |
|
pointer-events: auto; |
|
} |
|
.${CLASSNAME} a.notes { |
|
background-color: #2196F3; |
|
} |
|
@media (max-width: 992px) { |
|
.${CLASSNAME} a { |
|
left: 50%; |
|
margin-left: -15px; |
|
} |
|
} |
|
</style> |
|
<a |
|
class="${notesClass}" |
|
href="//chan.sankakucomplex.com/post/show/${id}" |
|
target="_blank" |
|
> |
|
</a>` |
|
let container = document.querySelector(`div.${CLASSNAME}`) |
|
if (!container) { |
|
container = document.createElement('div') |
|
container.classList.add(CLASSNAME) |
|
} |
|
console.log('Updated redirect to oldpage with id = ', id) |
|
container.innerHTML = button |
|
document.body.append(container) |
|
} |
|
} |
|
|
|
})(window, window.history); |
|
|
|
|
|
// Save posts with notes |
|
|
|
|
|
|
|
(function () { |
|
window.postsWithNotes = [] |
|
//modify 'responseText' of 'example2.txt' |
|
xhook.after(function(request, response) { |
|
if(request.url.match(/posts.*$/)) { |
|
let posts = null |
|
try { |
|
posts = JSON.parse(response.text) |
|
} catch (e) {} |
|
|
|
if (posts !== null && Array.isArray(posts)) { |
|
for (let i = 0; i < posts.length; ++i) { |
|
if (Array.isArray(posts[i].tags)) { |
|
if (posts[i].has_notes) { |
|
window.postsWithNotes.push(String(posts[i].id)) |
|
} |
|
} |
|
} |
|
response.text = JSON.stringify(posts) |
|
} |
|
} |
|
}); |
|
})() |