ํค | ๊ฐ
-- | --
์์คํ
ํ๋ซํผ | AMD64, Windows-10-10.0.14393
๋ธ๋ผ์ฐ์ | Firefox ๊ฐ๋ฐ์ ์๋์
(55.0 ๋ฒ ํ 1, 64๋นํธ)
์ฌ์ฉ์ ์์ด์ ํธ | Mozilla/5.0(Windows NT 10.0, Win64, x64, rv:55.0) Gecko/20100101 Firefox/55.0
๋น๋ ๊ตฌ์ฑ | https://hg.mozilla.org/releases/mozilla-beta/rev/6872377277a618b2b9e0d2b4c2b9e51765ac199e
๊ทธ๋ฆฌ์ค ์์ญ์ด | 3.11
// ==UserScript==
// <strong i="15">@name</strong> HeadTest
// <strong i="16">@namespace</strong> HeadTest
// <strong i="17">@include</strong> *
// @run-at document-start
// <strong i="18">@version</strong> 1
// <strong i="19">@grant</strong> none
// ==/UserScript==
AddGlobalStyle('body{background-color:red;}');
function AddGlobalStyle(css) {
var head, style;
head = document.getElementsByTagName("head")[0];
console.log("[Test]: Head: %o", head); //DEBUG
console.log("[Test]: document.head: %o", document.head); //DEBUG
}
[Test]: Head: <head>
[Test]: document.head: <head>
[Test]: Head: undefined
[Test]: document.head: null
์ด ๋ฌธ์ ๋ GM_addStyle
์๋ ์ ์ฉ๋ฉ๋๋ค.
๋ง์ง๋ง ์ฅ์ : Firefox 55.0a1(2017-03-17)
https://hg.mozilla.org/mozilla-central/rev/39607304b774591fa6e32c4b06158d869483c312 ์์ ๋น๋
์ฒซ ๋ฒ์งธ ๋ถ๋: Firefox 55.0a1(2017-03-17)
https://hg.mozilla.org/mozilla-central/rev/23a4b7430dd7e83a2809bf3dc41471f154301eda ์์ ๋น๋
๋ฒ๊ทธ 1333990 ์ด ์ฉ์์์ ๋๋ค.
์ฐธ์กฐ #1849
@janekptacijarabaci ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ฐพ์์ต๋๋ค.
setTimeout(()=>{
AddGlobalStyle('body{background-color:red;}');
});
๋ถ์์ ํ๊ณ ์ ๋ขฐํ ์ ์์ด ๋ณด์ด์ง๋ง ์ด๊ฒ์ด ํ์ฌ ๋ด๊ฐ ์ฐพ์ ์ ์๋ ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ผ๋ก ์๋ํ๊ฒ ๋ง๋ญ๋๋ค.
...๊ทธ๋ฆฌ๊ณ ๋ฌธ์๋ ์ฌ์ ํ ์๋ชป๋์์ต๋๋ค.
https://developer.mozilla.org/en-US/docs/Observer_Notifications#Documents
๋๋ ์ด๊ฒ์ด ๋ฒ๊ทธ๋ผ๊ณ ์๊ฐํ์ง ์๋๋ค. document-start
๋ ๋งค์ฐ ์ผ์ฐ ๋ฐ์ํฉ๋๋ค. ์ด๋ฅผ ํตํด ์ฌ์ฉ์ ์คํฌ๋ฆฝํธ๋ ์ฝํ
์ธ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ์คํ๋๊ธฐ ์ ์ ํ์ด์ง๋ฅผ ์กฐ์ํ ์ ์์ต๋๋ค.
๋์ค์ ์คํํ๋ ค๋ฉด document-end
๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ ๋์ฐ๋ณ์ด ๊ด์ฐฐ์๋ฅผ ์ฌ์ฉํ์ฌ ํน์ ์์๊ฐ ์ฝ์
๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ์ญ์์ค.
@the8472
์ด์ฉ๋ฉด ๋น์ ์ด ์ณ์ต๋๋ค.
MDN์ ๋ฐ๋ฅด๋ฉด:
๋ฌธ์ ์์ ์ฝ์
๋ฌธ์์ ๋ฃจํธ ์์๊ฐ ์์ฑ๋ ์งํ, ๊ทธ๋ฌ๋ ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ๊ธฐ ์ ์ ์ ์ก๋ฉ๋๋ค.
document-start
( document-element-inserted
๊ธฐ์ค)์ <html></html>
๊ฐ ์์ฑ๋ ์งํ์ ๋ฐ์ํ๋ฉฐ, ์ด๋ <head>
๋ null์
๋๋ค.
๊ทธ๋ฌ๋ ์ด๊ฒ์ ํ์ด์ง๊ฐ ๋ ๋๋ง๋๊ธฐ ์ ์ ์คํ์ผ์ ์ถ๊ฐํ๋ ค๊ณ ์๋ํ๋ ๋ง์ ์คํฌ๋ฆฝํธ๋ฅผ ์ค๋จ์ํต๋๋ค( document-end
GM_addstyle
๋ฅผ ์ฌ์ฉํ๋ฉด ํ์ด์ง๊ฐ ๊น๋ฐ์์ ์ ์ ์์ต๋๋ค).
์๋์ ์ธ ๊ฒ์ธ์ง๋ ๋ชจ๋ฅด๊ฒ ์ง๋ง
GM_addStyle์ ์ด์จ๋ ์ด๋ฆฌ์์ต๋๋ค. <style>
ํ๊ทธ๋ฅผ <head>
์ ์ฝ์
ํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค. ํ์ํ ๊ฒฝ์ฐ ๋์ฐ๋ณ์ด ๊ด์ฐฐ์๋ฅผ ์ฌ์ฉํ์ฌ ์ง์ ์ํํ ์ ์์ต๋๋ค.
๊ทธ๋ฌ๋ ๊ทธ๊ฒ์ด ์ค์ ๋ฌธ์ ๋ผ๋ฉด <head>
์กด์ฌ ์ฌ๋ถ๋ฅผ ํ์ธํ๊ธฐ ์ํด ํจ์น๋ ์ ์์ต๋๋ค. ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ ์ต์ ๋ฒ๋ฅผ ์ฌ์ฉํ์ญ์์ค.
@the8472 ํ์ฌ document-start
์ ๋์์ด ์ฌ๋ฐ๋ฅด๊ณ ์๋์ ์ด๋ผ๋ฉด ์คํ์ผ์ ์ถ๊ฐํด์ผ ํ ๋๋ง๋ค ๊ด์ฐฐ์๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
๊ฐ์ ๊ฒ
function addGlobalStyle(css){
let headHunter = new MutationObserver(
records => {
; check mutation records
; is added node's tag name 'head'?
; create a style node then throw it in
; then disconnect us
}
);
headhunter.observer(document, {childlist : true});
}
document-end
์์ ์คํ์ผ์ ์ถ๊ฐํ๋ ๋ชจ๋ ์คํฌ๋ฆฝํธ์ ํ์ํฉ๋๋ค. ๊ทธ๊ฒ์ ์ง๋ฃจํ๋ค.
GM_addstyle
๊ฐ ์ ๋ฅผ ์ํด ๊ทธ๋ ๊ฒ ํด์ฃผ๊ฑฐ๋ ์ํค์ ์ ์ฉํ ๊ฒฝ๊ณ ๋ฅผ ์์ฑํ๊ธฐ๋ฅผ ๋ฐ๋๋๋ค. ", ๋ฑ)
์, gm_addstyle์ ์์ ํด์ผ ํฉ๋๋ค.
๊ทธ๋ฌ๋ ์คํฌ๋ฆฝํธ๊ฐ ์ํํ ์ ์๋ ๋ค๋ฅธ ๋ชจ๋ ์์ ๋ ๋ฌธ์ ์์ ์ ๋์ผํ ํํ๋ฅผ ๊ฑด๋๋ฐ์ด์ผ ํ๋ฉฐ ์ด์ ๊ด๋ จํ์ฌ ํน๋ณํ์ง ์์ ์คํ์ผ์ ์ถ๊ฐํด์ผ ํฉ๋๋ค. ์ฌ์ฉํ ์ ์๋ DOM์ด ์์ผ๋ฏ๋ก ํ์ํ ๊ฒ์ด ๋ฌด์์ด๋ ๊ธฐ๋ค๋ ค์ผ ํฉ๋๋ค. ์ฆ, ๋ฌธ์ ์์ ์ ์์ ์ ์คํํ๋ ๊ฒฝ์ฐ ์ด์จ๋ ๊ด์ฐฐ์๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
์์ธํ ๋ณด๊ณ ์์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค. ํ์ง๋ง ์ด๊ฒ์ WAI์ด๊ณ 3.x ๋ถ๊ธฐ๋ ์ด์จ๋ ~์ฃฝ์์ต๋๋ค. (๋๋ ์ฐ์ ์ฐ๋ฆฌ์ ์๋ก์ด ์น ํ์ฅ ๋๊ตฐ์ฃผ๋ฅผ ํ์ํฉ๋๋ค.) GM_addStyle
์ ๊ฐ์ ๊ฒ๋ค์ 4.x์์ ์ ๋ ์กด์ฌํ์ง ์์์ผ๋ก์จ ์์ ๋ ๊ฒ์
๋๋ค.
https://arantius.com/misc/greasemonkey/amazon-url-cleaner.user.js ์ ๊ฐ์ ์คํฌ๋ฆฝํธ๋ DOM์ด ์ ํ ํ์ํ์ง ์์ผ๋ฉฐ ์ต๋ํ ๋นจ๋ฆฌ ์คํํด์ผ ํฉ๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
@the8472 ํ์ฌ
document-start
์ ๋์์ด ์ฌ๋ฐ๋ฅด๊ณ ์๋์ ์ด๋ผ๋ฉด ์คํ์ผ์ ์ถ๊ฐํด์ผ ํ ๋๋ง๋ค ๊ด์ฐฐ์๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.๊ฐ์ ๊ฒ
document-end
์์ ์คํ์ผ์ ์ถ๊ฐํ๋ ๋ชจ๋ ์คํฌ๋ฆฝํธ์ ํ์ํฉ๋๋ค. ๊ทธ๊ฒ์ ์ง๋ฃจํ๋ค.GM_addstyle
๊ฐ ์ ๋ฅผ ์ํด ๊ทธ๋ ๊ฒ ํด์ฃผ๊ฑฐ๋ ์ํค์ ์ ์ฉํ ๊ฒฝ๊ณ ๋ฅผ ์์ฑํ๊ธฐ๋ฅผ ๋ฐ๋๋๋ค. ", ๋ฑ)