Last active
May 28, 2020 00:39
-
-
Save yuta0801/79c2300b3799745cbf010f8dd332ab0e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 他人のScrapboxプロジェクトにUserCSSを適応する | |
// @namespace https://scrapbox.io/yuta0801/他人のScrapboxプロジェクトにUserCSSを適応する | |
// @version 0.2 | |
// @description 他人のScrapboxプロジェクトにUserCSSを適応する | |
// @author yuta0801 | |
// @match https://scrapbox.io/*/* | |
// @grant unsafeWindow | |
// ==/UserScript== | |
const page = 'yuta0801/styles' | |
const addUserStyle = (page, name) => { | |
const attrs = { | |
rel: 'stylesheet', href: `/api/code/${page}/${name}.css`, class: 'usercss' | |
} | |
unsafeWindow.$('body').append(unsafeWindow.$('<link>').attr(attrs)) | |
} | |
const update = (() => { | |
let project | |
return ({ state } = {}) => { | |
const newProject = state?.path.split('/')[1] ?? location.href.split('/')[3] | |
console.warn('yuta0801:update', project, newProject, state) | |
if (project === newProject) return | |
project = newProject | |
document.querySelectorAll('.usercss').forEach(el => el.remove()) | |
if (page.startsWith(project + '/')) return | |
addUserStyle(page, project) | |
addUserStyle(page, '_all') | |
} | |
})() | |
!(function (history) { | |
const pushState = history.pushState | |
history.pushState = function (state) { | |
if (typeof window.onpushstate === 'function') | |
window.onpushstate({ state }) | |
return pushState.apply(history, arguments) | |
} | |
})(window.history) | |
window.onpushstate = update | |
window.onpopstate = update | |
window.addEventListener('load', update) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment