How to rewrite some URLs in posts (Amazon Affiliate)

(Camille Roux) #1


Using a plugin, I’d like to rewrite all the Amazon links to add “tag” parameter for Amazon Affiliate. How can i do that?


(Manthan Mallikarjun) #2

You would need to make a plugin, or you could use JS.

It would be much easier to use jQuery to rewrite all of the URLs. All you have to do is add a snippet in the head area which replaces all links on-load.

(Camille Roux) #3

Which event can I use to catch each page refresh?

(Camille Roux) #4

I tried to use GitHub - deanbarrow/Affililink: A free way to automate your affiliate scheme. but it modifies only the links visible at the first loading of the page. How can I change that?

(Manthan Mallikarjun) #5

Im not sure if onLoad is a good idea. I actually do not know. I did $( document ).ready() but I have noticed that it doesnt work all the time either (only on refresh, and not through clicks)! :frowning:

@sam Whats the best way to do this on Discourse?

(Sam Saffron) #6

only correct way of doing this is writing a plugin which hooks into the process that bakes posts.

(Régis Hanol) #7

As @sam suggested, you may want to look at this plugin tutorial

(Camille Roux) #8

Is postProcessText method the good way to rewrite URL? I tried to code it quickly but only URL in the text were replaced (not links).

(Tuan Anh Tran) #9

It because you were processing text. Try again with jsonml.

(Camille Roux) #10

Have you got an example?

(Tuan Anh Tran) #11

You can go to discourse project on github and search for dialects. They got examples there.

(Camille Roux) #12


I’m trying to code a plugin. Here the main code:

    start: 'http',
    matcher: /(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*))/,
    emitter: function(content) {return addTagToEnd(content[0]);}

But this doesn’t work as I’d like. Links in plain text (like are no more linkable after modification (just text). And link tags (like Discourse) are not affected by my code currently.
Someone could help me to fix that?


PS: Here is the code of the addTagToEnd function: Modified and fixed Affililink · GitHub (fork from Affililink)

Amazon link rewriting for affiliate codes

On the last line of addTagToEnd I added this to the return link:

return ['a', {href: url}, url];

Couple of issues though:

  • When you copy and paste the link into the editor, it shows the Amazon onebox okay, but after posting it changes it to just the link for a few seconds. Then after the onebox process has happened it returns to normal, and it seems to insert the tag okay (haven’t tested changing tags). So the end result looks okay, but it will take a few seconds to format correctly.
  • Still doesn’t work with link tags as you say. It needs to be changed to look at the src link somehow which I’ll look into.


I’m trying to figure out why this seems to affect all links. It converts Amazon links correctly but for some reason any non-Amazon URL is returned as text. I have even tried adding a more specific matcher (for only URLs) but still everything seems to be impacted:

    start: 'http',
    matcher: /((https?:\/\/(www\.)?(amazon\.co\.uk))([^\s])?)/,
    emitter: function(content) {return addTagToEnd(content[0]);}

(Dean) #15

I’m going to create a plug for Affililink (GitHub - deanbarrow/Affililink: A free way to automate your affiliate scheme. which is compatible with Discourse. I’ll update this thread once I’ve made progress.

(Camille Roux) #17

@dean Have you got any news? Can we help you?

(Dean) #18

I’ve got a rough version working, it’s not very efficient at the moment so
I’m trying to work on that. The first release will be very basic but will
do the job.
I’ll upload it tomorrow as it’s late.

(Dean) #19

First working version: GitHub - deanbarrow/discourse_affililink: Affililink for Discourse


Thanks, this looks great.

Would it possibly be better to have it rewrite the text itself after the post is submitted?

I tried to do it with: Discourse.Dialect.inlineRegexp but didn’t get very far but I think it could be a cleaner solution than an onLoad event.

(Dean) #21

Ideally that would be the best solution, doing it server side (maybe on
post save) rather than on client side however I’ve just modified my
existing project which makes it a little more flexible by being compatible
with every site rather than being locked into a specific site/framework.