Change all website links to affiliate links automa

2019-02-15 07:49发布

I would like to be able to automatically change links into affiliate links automatically on my MediaWiki installation. This would help to reduce the amount of time managing links in case the code needs to be changed in the future.

This is the setup of my GOG.com affiliate scheme: I need to append this key to the end of every GOG.com link: ?pp=708a77db476d737e54b8bf4663fc79b346d696d2

gog.com/en/gamecard/baldurs_gate_the_original_saga/?pp=708a77db476d737e54b8bf4663fc79b346d696d2

Is it possible for a piece of code, like Javascript, to intercept all links (like http://www.gog.com/en/gamecard/baldurs_gate_the_original_saga/) and append the affiliate code on the end, as in the above example?

I'm aware of this piece of Javascript code called Amazon Associate Link Localiser which does a similar thing. However, it only works for Amazon links, and it also localises links which is a feature I don't want.

3条回答
乱世女痞
2楼-- · 2019-02-15 08:30

Not sure how great it would be performance wise for hundreds of links.

// Plain Javascript

var links = document.getElementsByTagName('a');

for (var i = 0, max = links.length; i < max; i++) {
    var _href = links[i].href;

    if (_href.indexOf('gog.com') !== -1) {
        links[i].href = _href + '?pp=708a77db476d737e54b8bf4663fc79b346d696d2';   
    }
}

DEMO

查看更多
走好不送
3楼-- · 2019-02-15 08:35

So you can also use jquery to bind any link click. This way you can do your link eval on the fly. This jsfiddle is a rough run through of what i think you're trying to accomplish. The alerts are just for your benefit and should be removed.

$("a").click(function() {
    addAffiliate(this);
});

myCode = "?pp=708a77db476d737e54b8bf4663fc79b346d696d2";
myAmazonCode = "?tag=shihac-20"
    function addAffiliate(link) {
        alert("enterting script: " + link.href);
        if ((link.href).indexOf("gog.com") > -1 && (link.href).indexOf(myCode) < 0) {
                link.href = link.href + myCode;
        }else if((link.href).indexOf("amazon.com") > -1 && (link.href).indexOf(myAmazonCode) < 0){
                link.href = link.href + myAmazonCode;   
        }
            alert(link.href);
            return true;
        }​

http://jsfiddle.net/du47b/23/

UPDATE: added code and fully qualified paths

UPDATE: added 'else if' block for other codes. using 'else if' instead of just another if block will hopefully cut back on unnecessary processing.

查看更多
狗以群分
4楼-- · 2019-02-15 08:43

Right way is to use LinkerMakeExternalLink mediawiki hook like that ( you can put it at bottom of your LocalSettings.php:

$wgHooks['LinkerMakeExternalLink'][] = 'ExampleExtension::exampleLinkerMakeExternalLink';

class ExampleExtension {
    public static function exampleLinkerMakeExternalLink( &$url, &$text, &$link, &$attribs, $linktype ) {
        if( strpos( $url, 'gog.com') !== false ) {
            $url .= '?pp=708a77db476d737e54b8bf4663fc79b346d696d2';
        }
        return false;
    }
}
查看更多
登录 后发表回答