Skip to content

Instantly share code, notes, and snippets.

@jackm
Last active January 19, 2018 11:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackm/56b0a262dde86ad8c8aa to your computer and use it in GitHub Desktop.
Save jackm/56b0a262dde86ad8c8aa to your computer and use it in GitHub Desktop.
Greasemonkey/Tampermonkey script for easy ad re-posting on Kijiji.ca - NOTE: This script is broken as of Nov 2016, it no longer works with Kijiji
// ==UserScript==
// @name Kijiji Repost Ad
// @namespace KijijiScripts
// @version 0.4
// @description Allows for easy ad reposting on Kijiji when managing your ads. A new link named "Repost Ad" will appear in the ad manage widget. Clicking it will redirect the page to the new ad page with most form field pre-filled. You can also specifiy a different category ID in the text field beside the "Repost Ad" button if you wish to repost to a different category than before. Note that this script assumes that you have a Kijiji account and are already logged in and able to manage your existing ads. Also note that when reposting the ad, you will have to manually upload the ad images again, as these are not saved or carried forward automatically (sorry!). Reposting an ad in this way actually creates a whole new ad, so you should delete your old ad as well.
// @match http://www.kijiji.ca/v-view-details.html?*
// @match https://www.kijiji.ca/v-view-details.html?*
// @match http://www.kijiji.ca/p-post-ad.html?*
// @match https://www.kijiji.ca/p-post-ad.html?*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_listValues
// @grant GM_deleteValue
// ==/UserScript==
// Add a "Repost Ad" link to the page if the ad manage widget exists
// (ie. they are viewing their own ad under their control)
function addLink(callback) {
if (document.querySelector("#ManageAdWidget")) {
var li_div = document.createElement('li');
li_div.className = "divider";
li_div.innerHTML = "|";
var a = document.createElement('a');
a.href = "#";
a.innerHTML = "Repost Ad";
a.onclick = callback;
var i = document.createElement('input');
i.type = "text";
i.size = 3;
i.maxLength = 4;
i.id = "newCat";
i.placeholder = "NewCat";
var li = document.createElement('li');
li.appendChild(a);
li.appendChild(i);
document.querySelector("#ManageAdWidget ul.vertical-ul").appendChild(li_div);
document.querySelector("#ManageAdWidget ul.vertical-ul").appendChild(li);
}
}
// Extract ad data from page and then redirect to new page
function extractDataAndRedirect() {
// Get current ad category ID
var catUrls = document.querySelectorAll(".category-selected[itemprop=url]");
var adCat = catUrls[catUrls.length - 1].getAttribute('data-id');
// Check if user specified their own category ID to use
if (document.querySelector("input#newCat")) {
var newCat = document.querySelector("input#newCat").value;
if (newCat && !isNaN(newCat)) {
adCat = newCat;
}
}
// Extract important ad data
var adTitle = document.querySelector("[itemprop=name]").innerText;
var adDesc = document.querySelector("#itemdetails [itemprop=description]").innerHTML.replace(/^\s*/, '').replace(/<br(\s*\/|)>/g, '');
var adPrice = document.querySelector("#itemdetails [itemprop=price]").innerText.replace(/\$/, '');
var adAddress = document.querySelector("#itemdetails .ad-attributes tr:nth-child(3) > td").innerHTML.split("<br>")[0];
var adPostal = /[A-Z][0-9][A-Z]\s*[0-9][A-Z][0-9]/g.exec(adAddress);
adPostal = (null === adPostal) ? "" : adPostal[0];
// Store data before loading new page
GM_setValue("adTitle", adTitle);
GM_setValue("adDesc", adDesc);
GM_setValue("adPrice", adPrice);
GM_setValue("adAddress", adAddress);
GM_setValue("adPostal", adPostal);
// Set flag for after page is reloaded
GM_setValue("repostAd", "true");
// Reload page to new location to post a new ad
window.location.href = window.location.protocol + "//" + window.location.host + "/p-post-ad.html?categoryId=" + adCat;
// A new page has now been requested, all previous javascript is lost, except for data stored using GM_setValue()
}
(function() {
addLink(extractDataAndRedirect);
if (window.location.href.indexOf("/p-post-ad.html?") != -1 && GM_getValue("repostAd") == "true") {
// User has clicked on the repost ad link and we are coming from a previous repost ad page
// We should now be on a "/p-post-ad.html?" page
var h = document.createElement('h3');
h.appendChild(document.createTextNode("Don't forget to re-upload ad pictures!"));
var d = document.createElement('div');
d.appendChild(h);
var mainForm = document.querySelector("#PostAdMainForm");
mainForm.insertBefore(d, mainForm.childNodes[0]);
// Retrieve data
var adTitle = GM_getValue("adTitle");
var adDesc = GM_getValue("adDesc");
var adPrice = GM_getValue("adPrice");
var adAddress = GM_getValue("adAddress");
var adPostal = GM_getValue("adPostal");
// Fill in copied ad data
document.querySelector("#priceAmount").value = adPrice;
document.querySelector("#forsaleby_s[value=ownr]").checked = true;
document.querySelector("#postad-title").value = adTitle;
document.querySelector("#pstad-descrptn").value = adDesc;
document.querySelector("#pstad-map-address").value = adAddress;
document.querySelector("#PostalCode").value = adPostal;
// Remove all previous saved data
var keys = GM_listValues();
for (var i = 0, key = null; key = keys[i]; i++) {
GM_deleteValue(key);
}
}
})();
@jackm
Copy link
Author

jackm commented Nov 17, 2016

@sallyyollande Try using this much simpler script instead. It's what I use now and it should work for you too.

@CalebDanechi
Copy link

kijiji changed their url when you post an ad, so instead of www.kijiji.ca/p-post-ad.html? its now www.kijiji.ca/p-admarkt-post-ad.html?
Itried to fix the code by my own by replacing it but it didnt work.

@jackm
Copy link
Author

jackm commented Aug 30, 2017

@CalebDanechi This script broke a year ago when Kijiji updated their site and I am no longer maintaining this particular script.

If you are comfortable with running a Python script (Python is available on both Linux and windows), then I would suggest using the Kijiji-Repost-Headless project.

This is what I use now to repost my Kijiji ads, and it is being actively developed. Unfortunately it is not quite as simple to use as clicking a button in your browser, but it does work. Perhaps someday it could be developed into a real browser extension to make it easier to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment