pocketSelectAll

10/6/2020, 4:45:17 PM

目前為 2020-10-06 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        pocketSelectAll
// @namespace   Violentmonkey Scripts
// @match       https://app.getpocket.com/*
// @grant       none
// @version     1.0
// @author      AdrianSkar
// @description 10/6/2020, 4:45:17 PM
// ==/UserScript==

(function () {
	setTimeout(() => {// Pocket loads content (including buttons) asynchronously
		let listen = document.querySelector("button[aria-label='Bulk Edit']");

		function buttons() {

			setTimeout(() => {// Pocket loads content (including buttons) asynchronously
				let selectVis = function () {
					// https://stackoverflow.com/questions/2705583/how-to-simulate-a-click-with-javascript
					function eventFire(el, etype) {
						if (el.fireEvent) {
							el.fireEvent('on' + etype);
						} else {
							let evObj = document.createEvent('Events');
							evObj.initEvent(etype, true, false);
							el.dispatchEvent(evObj);
						}
					}
					//svg
					let target = document.getElementsByTagName("use");

					//loop trough <use> elements (some are not articles)
					for (let i = 0; i < target.length; i++) {
						// check if targets are selectable articles
						if ((target[i].getAttribute('xlink:href').indexOf("CheckOpen") > -1)) {
							//click on them
							eventFire(target[i], 'click');
						}
						else {
							// console.log(target[i], 'is not clickable'); 
						}
					}

				};
				console.log(' close bulk');
				//prepend buttons before the close bulk edit one
				let bulk = document.querySelector("button[aria-label='Close Bulk Edit']");
				console.log(bulk);
				let selVis = bulk.cloneNode();
				let selAll = bulk.cloneNode();

				selVis.textContent = 'Select visible';
				selAll.textContent = 'Select all';
				selVis.setAttribute('margin-right', '1em'); // todo: fix; not working
				selAll.setAttribute('margin-right', '1em');

				bulk.parentElement.prepend(selVis);
				bulk.parentElement.prepend(selAll);

				// select all articles (selectVis + scroll every 2secs)
				let selectAll = function () {
					let target = document.getElementsByTagName("use"),
						last = target[target.length - 1];

					function scrollEnd() {
						if (last.getAttribute('xlink:href').indexOf("CheckOpen") === -1) {
							selAll.textContent = 'Done';
							console.log('done');
							clearInterval(timer);
							selectVis();
						}
						else {
							selectVis();
							last.scrollIntoView();
							selAll.textContent = 'working...';
							console.log('scrolling');
						}
					}
					let timer = setInterval(() => {
						target = document.getElementsByTagName("use");
						last = target[target.length - 1];
						scrollEnd();
					}, 2000);
				};

				selVis.addEventListener('click', selectVis);
				selAll.addEventListener('click', selectAll);
			}, 100);
		}
		if (Boolean(listen)) {
			console.log('listen');
			listen.addEventListener('click', buttons);
		}
		else {
			console.log('no listen');
		}
	}, 2000);
})();