Skip to content

Instantly share code, notes, and snippets.

@pccasto
Last active January 20, 2023 23:56
Show Gist options
  • Save pccasto/708b097ecbf1b9b8e22c30e450df804e to your computer and use it in GitHub Desktop.
Save pccasto/708b097ecbf1b9b8e22c30e450df804e to your computer and use it in GitHub Desktop.
tampermonkey script to force LinkedIn feed to 'Recent', rather than 'Top'
// ==UserScript==
// @name LinkedIn Recent Feed
// @namespace http://tampermonkey.net/
// @version 0.4
// @description I always want my LinkedIn feed in chronological order!
// @downloadURL https://gist.github.com/pccasto/708b097ecbf1b9b8e22c30e450df804e/raw/ae930ab765918d7d0d74622e4176b300f2b85e12/LinkedInRecentPlease.user.js
// @author pcasto
// @include https://linkedin.com/*
// @include https://www.linkedin.com/*
// @exclude https://www.linkedin.com/tscp-serving/*
// @grant none
// @run-at document-idle
// @require https://gist.githubusercontent.com/BrockA/2625891/raw/9c97aa67ff9c5d56be34a55ad6c18a314e5eb548/waitForKeyElements.js
// ==/UserScript==
// v0.4 add in a missing require statement for the waitForKeyElements, and tests to avoid acting on /feed/news, etc.
// v0.3 handle the url changes through pushstate - site uses AJAX and changes URL to avoid reload (maybe pjax?)
// v0.2 handle the real world, instead of first trivial case, due to dynamic emberIDs
'use strict';
var recentSort;
var topSort = 'Top'; // change for other languages
var triggerSelector = "#voyager-feed div.core-rail button.dropdown-trigger";
var triggerAvailableSelector = "#voyager-feed .dropdown .ember-view";
// use jQuery from the page
// var $ = window.$;
(recentSort = function() {
// Only needed on the /feed page - this handles case where starting URL is /jobs, etc
if(window.location.href.indexOf("/feed") === -1) return;
// and not /feed/new/..., etc.
if(window.location.href.match(/\/feed\/.+/)) return;
// if only it could be this easy... hence v 0.2 ...
// ($("#ember16048-trigger") === 'Recent') || $("#ember16048-trigger").click() && $("#ember16048-options > li:eq(1) > button").click();
// the IDs "ember####-trigger" and ember-####-options are dynamic, and elements have no fixed IDs so need to select based on structure and class
var $trigger = window.$(triggerSelector);
if ($trigger.length === 0) return; // not sure if this is effective at short-circuiting the test
console.log("Recent sort: ", window.location.href);
if ($trigger.find("span:eq(0)").text().trim() === topSort) {
($trigger.attr('aria-expanded') === 'true') || $trigger.click();
var optionsName = $trigger.attr('aria-controls');
if (optionsName === "" || optionsName === "undefined") {
console.log("could not get aria-controls info from linkedin feed order")
} else {
window.$("#" + optionsName).find("li:eq(1) > button").click(); // 'Top' then 'Recent' is all I've seen.
}
}
})();
(function(history){
var pushState = history.pushState;
history.pushState = function(state) {
if (typeof history.onpushstate == "function") {
history.onpushstate({state: state});
}
// ... whatever else you want to do
// maybe call onhashchange e.handler
return pushState.apply(history, arguments);
};
})(window.history);
// Tried 'waitFor, instead of requiring/including waitForKeyElements
// but when I added that code, it would cause script to fail even if I wasn't calling the function...
// https://stackoverflow.com/questions/12897446/userscript-to-wait-for-page-to-load-before-executing-code-techniques
window.onpopstate = history.onpushstate = function(e) {
// console.log("pushstate fired: ", e.state.path);
if (e.state.path.match(/\/feed\/$/)) {
waitForKeyElements(triggerAvailableSelector, recentSort);
}
return e;
};
@cfjedimaster
Copy link

Currently throws an error in console: Uncaught (in promise) TypeError: window.$ is not a function

@pccasto
Copy link
Author

pccasto commented Jan 20, 2023

Haven't had Tampermonkey enabled for quite some time, and only occasionally visit LinkedIn these days.
But I've turned that back on, and will see if I can spot what has changed.
I do see the error you mentioned.
If I find something I'll update the code.

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