์๋ ํ์ธ์,
์ ๋ Greasemonkey๋ฅผ ์ฒ์ ์ฌ์ฉํฉ๋๋ค.
๋ค๋ฅธ Greasemonkey ์ฌ์ฉ์ ์คํฌ๋ฆฝํธ์์ ํจ์๋ฅผ ํธ์ถํ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
๊ฐ์ฌ ํด์.
์๋์, ๋ค๋ฅธ ์คํฌ๋ฆฝํธ๊ฐ ์ด๋ค ์๋จ์ ํตํด API๋ฅผ ๋ ธ์ถํ๋ ค๋ ๋ ธ๋ ฅ์ ๊ธฐ์ธ์ด์ง ์๋ ํ ์๋๋๋ค.
๋ด๋ถ์ ์ผ๋ก๋ ๋ฐฉ๋ฒ์ด ์์ง๋ง ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์์ต๋๋ค.
๋ ธ์ถ๋ ๊ธฐ๋ฅ์ด ์๋ ์คํฌ๋ฆฝํธ:
unsafeWindow.hello = hello;
function hello() {alert("hello world");}
ํจ์๋ฅผ ํธ์ถํ๋ ์คํฌ๋ฆฝํธ
hello();
๊ทธ๊ฒ ์์ง์ ๋๋ค.
๋ฌผ๋ก ๊ทธ๊ฒ์ ๋ค๋ฅธ ์ฌ์ฉ์ ์คํฌ๋ฆฝํธ๋ฟ๋ง ์๋๋ผ ์น ์ฌ์ดํธ์์ ํธ์ถ ํ ์ ์์์ ์๋ฏธํฉ๋๋ค.
์ง์ค. ์ํธ ๋งค๊ฐ๋ณ์๋ฅผ ์ง์ ํ์ญ์์ค.
๊ฐ์ ํญ์ด์ด์ผ ํฉ๋๋ค.
// ==UserScript==
// <strong i="5">@name</strong> Test call 1
// <strong i="6">@namespace</strong> https://github.com/tiansh/
// <strong i="7">@include</strong> http://example.com/
// <strong i="8">@version</strong> 1
// <strong i="9">@grant</strong> unsafeWindow
// ==/UserScript==
unsafeWindow.x = function (str, callback) {
console.log('%o.x: %o', this, arguments);
alert('x says: ' + str);
callback(str + 'from x');
};
// ==UserScript==
// <strong i="12">@name</strong> Test call 2
// <strong i="13">@namespace</strong> https://github.com/tiansh/
// <strong i="14">@include</strong> http://example.com/
// <strong i="15">@version</strong> 1
// <strong i="16">@grant</strong> unsafeWindow
// ==/UserScript==
setTimeout(function () {
unsafeWindow.x('hello', function (nstr) {
console.log('%o.y: %o', this, arguments);
alert('y says: ' + nstr);
});
}, 0);
์น ์ฌ์ดํธ์ ํ์ด์ง์ ์๋ @the8472 ์คํฌ๋ฆฝํธ๋ ์๋๋ฐ์ค ๋ณดํธ๋ก ์ธํด ํจ์๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค.
@tiansh , ์ด ์์ ์์ ์๋๋ฐ์ค ๋ณดํธ๋ <strong i="6">@grant</strong> unsafeWindow
์ํด ๋นํ์ฑํ๋์์ต๋๋ค.
@pyhedgehog ์๋ํด