Skip to content

Instantly share code, notes, and snippets.

@BubuMVX
Last active September 20, 2020 17:10
Show Gist options
  • Save BubuMVX/48eeebdc5d9b746e298d23dc90ecf531 to your computer and use it in GitHub Desktop.
Save BubuMVX/48eeebdc5d9b746e298d23dc90ecf531 to your computer and use it in GitHub Desktop.
An userscript to display your tracking code on PledgeBox.
// ==UserScript==
// @name PledgeBox details
// @version 1.1
// @description Add additional infos on PledgeBox interface.
// @author Romain
// @match https://survey.pledgebox.com/projects
// @match https://survey.pledgebox.com/projects/detail/*
// @grant none
// @require https://code.jquery.com/jquery-3.5.1.min.js
// @homepageURL https://gist.github.com/grobux/48eeebdc5d9b746e298d23dc90ecf531
// @supportURL https://gist.github.com/grobux/48eeebdc5d9b746e298d23dc90ecf531
// @downloadURL https://gist.githubusercontent.com/grobux/48eeebdc5d9b746e298d23dc90ecf531/raw/pledgebox-get-tracking.user.js
// @updateURL https://gist.githubusercontent.com/grobux/48eeebdc5d9b746e298d23dc90ecf531/raw/pledgebox-get-tracking.user.js
// ==/UserScript==
/* globals jQuery, $, waitForKeyElements */
(function() {
'use strict';
window.addEventListener('load', function() {
listener();
});
})();
function listener() {
setInterval(function() {
var monitor = $('.project-page');
var marker = 'pledgebox-details-loaded';
var loaded = monitor.hasClass(marker);
if(!loaded) {
monitor.addClass(marker);
router();
}
}, 1000);
}
function router() {
if(location.href == 'https://survey.pledgebox.com/projects') {
loadProjects();
} else if(location.href.match(/^https:\/\/survey\.pledgebox\.com\/projects\/detail\//)) {
loadProjectDetails();
}
}
function loadProjects() {
var auth = JSON.parse(localStorage.getItem('pro__ACCESS_TOKEN'));
if(!auth) {
return;
}
$.ajax({
url: 'https://survey.pledgebox.com/api/projects?status=0',
dataType: 'json',
contentType: 'application/json;charset=utf-8',
headers: {
'Authorization': auth.value
},
success: function(response) {
var items = $('.project-list .project-item');
$.each(response.data, function(index, datas) {
$('.project-name', items.eq(index)).append(getSurveyDetails(datas));
});
}
});
}
function loadProjectDetails() {
$.ajax({
url: 'https://survey.pledgebox.com/api/survey/order/info',
method: 'POST',
dataType: 'json',
contentType: 'application/json;charset=utf-8',
data: JSON.stringify({
'token': location.href.split('/')[5]
}),
success: function(response) {
$('.summary > .content-box:nth-child(3) .shipping-info').append(getTrackingLink(response.data));
}
});
}
function getSurveyDetails(datas) {
var container = $('<div>');
var tracking = getTrackingElement(datas);
var completed_at = (datas.completed_at ? getDate(datas.completed_at): '<span style="color:#ea4335; font-weight:bold;">no</span>');
container
.append(tracking)
.append('&nbsp;|&nbsp;')
.append('Survey completed: ' + completed_at);
return container;
}
function getTrackingElement(datas) {
if(datas.tracking_code) {
var link = document.createElement('a');
link.textContent = 'Tracking: ' + datas.tracking_code;
link.setAttribute('href', datas.tracking_url + '#nums=' + datas.tracking_code);
link.setAttribute('target', '_blank');
link.setAttribute('style', 'color:#34a853; font-weight:bold;');
return link;
} else {
var span = document.createElement('span');
span.textContent = 'No tracking found';
span.setAttribute('style', 'color:#ea4335; font-weight:bold;');
return span;
}
}
function getTrackingLink(datas) {
if(datas.tracking_code) {
var url = datas.tracking_url + '#nums=' + datas.tracking_code;
return '<br>Tracking link: <a href="' + url + '" target="_blank">' + url + '</a>';
}
}
function getDate(date) {
if (Number.isInteger(date)) {
date = date * 1000;
}
return new Date(date).toLocaleString();
}
@fairwood136
Copy link

fairwood136 commented Sep 14, 2020

Could you modify the 'href'
from: json.data.tracking.url
to: json.data.tracking.url + "#nums=" + json.data.tracking.code

                link.setAttribute('href', json.data.tracking_url + '#nums=' + json.data.tracking_code);

That way, you don't have to retype the tracking code. Thanks.

@BubuMVX
Copy link
Author

BubuMVX commented Sep 14, 2020

Thanks for your help, script updated!
Version bumped to 0.2.

@BubuMVX
Copy link
Author

BubuMVX commented Sep 17, 2020

Big update:

  • code redesign,
  • display tracking on the projects list and the project view,
  • display the date the survey was submitted.

Version bumped to 1.0.

@BubuMVX
Copy link
Author

BubuMVX commented Sep 20, 2020

Update: on an order details, display the tracking URL as a link in the tracking section.

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