开发者

Greasemonkey to inject Adfly ads

Is there any way to convert this into Greasemonkey code?

<script>
var adfly_id = 517450;
var adfly_advert = 'banner';
var frequency_cap = 5;
var frequency_delay = 5;
var init_delay = 3;
</script>
<script src="http://adf.ly/js/entry.js"></script>

It's a website entry script for adf.ly which when added to a we开发者_StackOverflow中文版bsite brings up adf.ly ads.

I want to make a script that will load adf.ly links in the browser while surfing.

Please help! Thank You :)


You'll need to inject the JS into the page. Avoid unsafeWindow if you can.

Here's a complete Greasemonkey translation of that script:

// ==UserScript==
// @name            _adFly why?
// @include         http://YOUR_SITE/YOUR_PATH/*
// ==/UserScript==


function myCode () {
    //--- Set global variables for adFly.
    window.adfly_id         = 517450;
    window.adfly_advert     = 'banner';
    window.frequency_cap    = 5;
    window.frequency_delay  = 5;
    window.init_delay       = 3;
}

addJS_Node (null, null, myCode);
addJS_Node (null, "http://adf.ly/js/entry.js");

function addJS_Node (text, s_URL, funcToRun) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ    = document.getElementsByTagName('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}


Yes, in theory. You'd just need to write these to the head. They will evaluate outside of the greasemonkey scope if you load them on the page direction. you can even just do a document.body.appendChild after creating script tags. So,

var sc = document.createElement('script');
sc.src = "http://adf.ly/js/entry.js"
document.body.appendChild(sc);

For the other one, it'd probably just be easier to do an unsafeWindow.varname assignment like

unsafeWindow.adfly_id = 517450;
unsafeWindow.adfly_advert = 'banner';
unsafeWindow.frequency_cap = 5;
unsafeWindow.frequency_delay = 5;
unsafeWindow.init_delay = 3;

Though you don't ever want to use these values in your script, as these can be changed by the website itself. Using them gives websites access to more control over a users browser and lets them do things that they would not otherwise be able to do (for example, accessing information from other websites via XSS ajax). Only write to values in unsafeWindow, don't read from them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜