allow download pics in 2nd, 3rd and 4th article from twitter
This commit is contained in:
		@ -2,21 +2,31 @@ function getElementByXpath(path) {
 | 
				
			|||||||
	return document.evaluate(path, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
 | 
						return document.evaluate(path, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function twitterXPath(nth, sendResponse) {
 | 
				
			||||||
 | 
						let a = getElementByXpath(`(//article[contains(@role,'article')])[${nth}]//a[contains(@href,'/photo/')]//img/@src`);
 | 
				
			||||||
 | 
						let arr = [];
 | 
				
			||||||
 | 
						for (i=0; i<a.snapshotLength; i++) {
 | 
				
			||||||
 | 
							let oneUrl = a.snapshotItem(i).textContent;
 | 
				
			||||||
 | 
							let splited = oneUrl.split('?');
 | 
				
			||||||
 | 
							let baseUrl = splited[0];
 | 
				
			||||||
 | 
							var getParas = new URLSearchParams(splited[1]);
 | 
				
			||||||
 | 
							getParas.set("name", "orig");
 | 
				
			||||||
 | 
							arr.push(baseUrl + '?' + getParas.toString());
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					 		console.log(arr);
 | 
				
			||||||
 | 
						sendResponse({dataType: 'urlList', urlList: arr, source: window.location.href});
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
 | 
					chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
 | 
				
			||||||
 | 
						
 | 
				
			||||||
	if (request.action == "requestPics") {
 | 
						if (request.action == "requestPics") {
 | 
				
			||||||
		// TODO: check url, use different match rule to return data.
 | 
							twitterXPath(1, sendResponse);
 | 
				
			||||||
		let a = getElementByXpath("(//article[contains(@role,'article')])[1]//a[contains(@href,'/photo/')]//img/@src");
 | 
						} else if (request.action == "requestPics2") {
 | 
				
			||||||
		let arr = [];
 | 
							twitterXPath(2, sendResponse);
 | 
				
			||||||
		for (i=0; i<a.snapshotLength; i++) {
 | 
						} else if (request.action == "requestPics3") {
 | 
				
			||||||
			let oneUrl = a.snapshotItem(i).textContent;
 | 
							twitterXPath(3, sendResponse);
 | 
				
			||||||
			let splited = oneUrl.split('?');
 | 
						} else if (request.action == "requestPics4") {
 | 
				
			||||||
			let baseUrl = splited[0];
 | 
							twitterXPath(4, sendResponse);
 | 
				
			||||||
			var getParas = new URLSearchParams(splited[1]);
 | 
					 | 
				
			||||||
			getParas.set("name", "orig");
 | 
					 | 
				
			||||||
			arr.push(baseUrl + '?' + getParas.toString());
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
// 		console.log(arr);
 | 
					 | 
				
			||||||
		sendResponse({dataType: 'urlList', urlList: arr, source: window.location.href});
 | 
					 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		sendResponse({}); // Send nothing..
 | 
							sendResponse({}); // Send nothing..
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -11,6 +11,10 @@
 | 
				
			|||||||
  </head>
 | 
					  </head>
 | 
				
			||||||
  <body>
 | 
					  <body>
 | 
				
			||||||
    <button id="download">Download</button>
 | 
					    <button id="download">Download</button>
 | 
				
			||||||
 | 
						<br/>
 | 
				
			||||||
 | 
						<button id="download2">2nd</button>
 | 
				
			||||||
 | 
						<button id="download3">3rd</button>
 | 
				
			||||||
 | 
						<button id="download4">4th</button>
 | 
				
			||||||
    <script src="popup.js"></script>
 | 
					    <script src="popup.js"></script>
 | 
				
			||||||
  </body>
 | 
					  </body>
 | 
				
			||||||
</html>
 | 
					</html>
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										25
									
								
								popup.js
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								popup.js
									
									
									
									
									
								
							@ -1,19 +1,17 @@
 | 
				
			|||||||
let downloadBtn = document.getElementById('download');
 | 
					let downloadBtn = document.getElementById('download');
 | 
				
			||||||
 | 
					let download2Btn = document.getElementById('download2');
 | 
				
			||||||
 | 
					let download3Btn = document.getElementById('download3');
 | 
				
			||||||
 | 
					let download4Btn = document.getElementById('download4');
 | 
				
			||||||
// 
 | 
					// 
 | 
				
			||||||
// chrome.storage.sync.get('color', function(data) {
 | 
					// chrome.storage.sync.get('color', function(data) {
 | 
				
			||||||
// 	changeColor.style.backgroundColor = data.color;
 | 
					// 	changeColor.style.backgroundColor = data.color;
 | 
				
			||||||
// 	changeColor.setAttribute('value', data.color);
 | 
					// 	changeColor.setAttribute('value', data.color);
 | 
				
			||||||
// });
 | 
					// });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
downloadBtn.onclick = function(element) {
 | 
					function sendActionAndResponse(actionStr) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
// 	chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
 | 
					 | 
				
			||||||
// 		console.log(tabs[0].url);
 | 
					 | 
				
			||||||
// 	});
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	chrome.tabs.getSelected(null, function(tab) {
 | 
						chrome.tabs.getSelected(null, function(tab) {
 | 
				
			||||||
		// Send a request to the content script.
 | 
							// Send a request to the content script.
 | 
				
			||||||
		chrome.tabs.sendRequest(tab.id, {action: "requestPics"}, function(response) {
 | 
							chrome.tabs.sendRequest(tab.id, {action: actionStr}, function(response) {
 | 
				
			||||||
			console.log(response);
 | 
								console.log(response);
 | 
				
			||||||
			if (response.dataType == "urlList") {
 | 
								if (response.dataType == "urlList") {
 | 
				
			||||||
				const data = new URLSearchParams();
 | 
									const data = new URLSearchParams();
 | 
				
			||||||
@ -29,6 +27,15 @@ downloadBtn.onclick = function(element) {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					downloadBtn.onclick = function(element) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 	chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
 | 
				
			||||||
 | 
					// 		console.log(tabs[0].url);
 | 
				
			||||||
 | 
					// 	});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						sendActionAndResponse("requestPics");
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
// 	let color = element.target.value;
 | 
					// 	let color = element.target.value;
 | 
				
			||||||
// 	chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
 | 
					// 	chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
 | 
				
			||||||
@ -37,3 +44,7 @@ downloadBtn.onclick = function(element) {
 | 
				
			|||||||
// 			{code: 'document.body.style.backgroundColor = "' + color + '";'});
 | 
					// 			{code: 'document.body.style.backgroundColor = "' + color + '";'});
 | 
				
			||||||
// 	});
 | 
					// 	});
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					download2Btn.onclick = function(element) { sendActionAndResponse("requestPics2"); }
 | 
				
			||||||
 | 
					download3Btn.onclick = function(element) { sendActionAndResponse("requestPics3"); }
 | 
				
			||||||
 | 
					download4Btn.onclick = function(element) { sendActionAndResponse("requestPics4"); }
 | 
				
			||||||
		Reference in New Issue
	
	Block a user