Last active
February 12, 2018 05:46
-
-
Save dannvix/d2d5f8e0b7daa2635254 to your computer and use it in GitHub Desktop.
Customization/Simplification for Pocket Web
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Pocket Web Customized | |
// @description Cusotmizations/Simplifications for Pocket Web | |
// @namespace http://getpocket.com | |
// @author Shao-Chung Chen | |
// @license MIT (http://opensource.org/licenses/MIT) | |
// @version 1.9.1 | |
// @include http://getpocket.com/* | |
// @include https://getpocket.com/* | |
// | |
// @history 1.9 restore scroll position after item synchronization | |
// @history 1.8 remove "utm_xxx=yyy" from URL parameters | |
// @history 1.7 fix original link parsing | |
// @history 1.6 disable automatic item synchronization | |
// @history 1.5 hide the live chat button | |
// @history 1.4 rename; update for new Pocket Web | |
// @history 1.3 efficient patch; hide share buttons | |
// @history 1.2 unbind click event of items | |
// @history 1.1 add indicator | |
// @history 1.0 initial commmit | |
// ==/UserScript== | |
var switchToOriginalLinks = function() { | |
var allItems = document.querySelectorAll('.item:not(.original-link)'); | |
[].forEach.call(allItems, function(item, index) { | |
var a = item.querySelector('a'); | |
var originalUrl = item.querySelector('.favicon').getAttribute('data-originalurl'); | |
var parsedUrl = decodeURIComponent(originalUrl.match(/https?:\/\/getpocket.com\/redirect\?url=(.*)&formCheck=./)[1]); | |
var effectiveUrl = (parsedUrl || originalUrl).replace(/utm_\w+=[^&]+&?/g, ""); | |
a.href = effectiveUrl; | |
item.classList.add('original-link'); | |
}); | |
// unbind click event handler to prevent batch selection | |
// since we cannot access jQuery here, we use this tricky hack | |
var script = document.createElement('script'); | |
script.text = ['(function() {' | |
,' jQuery(".item").off("click");' | |
,' jQuery(".item_link").off("click");' | |
,' jQuery(".item_content").off("click");' | |
,'})();' | |
].join('\n'); | |
document.body.appendChild(script); | |
}; | |
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; | |
var target = document.querySelector('#queue'); | |
var config = { attributes: true, childList: true, characterData: true }; | |
var mutationObserver = new MutationObserver(function(mutations) { | |
switchToOriginalLinks(); | |
}); | |
mutationObserver.observe(target, config); | |
switchToOriginalLinks(); | |
var addCustomizationIndicator = function() { | |
var styles = { | |
background: 'hsla(0, 0%, 0%, 0.67)', | |
bottom: '10px', | |
color: 'white', | |
fontSize: '8pt', | |
padding: '5px 7px', | |
position: 'fixed', | |
right: '10px', | |
zIndex: '9999', | |
} | |
var indicator = document.createElement('div'); | |
indicator.innerHTML = 'Customizations Applied' | |
for (var attr in styles) { | |
indicator.style[attr] = styles[attr]; | |
} | |
document.body.appendChild(indicator); | |
}; | |
var hideAllShareButtons = function() { | |
var style = document.createElement('style'); | |
style.textContent = 'li.action_share { display: none; }'; | |
document.head.appendChild(style); | |
}; | |
var hideLiveChatButton = function() { | |
var chatBtn = document.querySelector('#livechat-compact-container'); | |
if (chatBtn) { | |
document.body.removeChild(chatBtn); | |
} | |
}; | |
var preserveScrollPositionAfterSyncUp = function() { | |
// hook and `queue.syncCallback` via injected script | |
var script = document.createElement('script'); | |
script.text = ['(function() {' | |
,' var origSyncCallback = queue.syncCallback;' | |
,' var lastScrollY = 0;' | |
,' queue.syncCallback = function() {' | |
,' lastScrollY = window.scrollY;' | |
,' origSyncCallback.apply(this, arguments);' | |
,' window.scrollTo(window.scrollX, lastScrollY);' | |
,' };' | |
,'})();' | |
].join('\n'); | |
document.body.appendChild(script); | |
}; | |
if (window.location.hostname.toLowerCase().indexOf('getpocket.com') >= 0) { | |
window.addEventListener('load', addCustomizationIndicator, false); | |
window.addEventListener('load', hideAllShareButtons, false); | |
window.addEventListener('load', hideLiveChatButton, false); | |
window.addEventListener('load', preserveScrollPositionAfterSyncUp, false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello !
First of all thanks for the code, it helps me immensely in my daily web-browsing activities ! Kudos to you.
However there seems to be something glitchy when used in Firefox with GreaseMonkey, doesn't work at all actually. Is there some blink-specific JavaScript extracts in there that i'm missing ?