allow download pics in 2nd, 3rd and 4th article from twitter
This commit is contained in:
		@ -2,10 +2,8 @@ function getElementByXpath(path) {
 | 
			
		||||
	return document.evaluate(path, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
 | 
			
		||||
	if (request.action == "requestPics") {
 | 
			
		||||
		// TODO: check url, use different match rule to return data.
 | 
			
		||||
		let a = getElementByXpath("(//article[contains(@role,'article')])[1]//a[contains(@href,'/photo/')]//img/@src");
 | 
			
		||||
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;
 | 
			
		||||
@ -15,8 +13,20 @@ chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
 | 
			
		||||
		getParas.set("name", "orig");
 | 
			
		||||
		arr.push(baseUrl + '?' + getParas.toString());
 | 
			
		||||
	}
 | 
			
		||||
// 		console.log(arr);
 | 
			
		||||
 		console.log(arr);
 | 
			
		||||
	sendResponse({dataType: 'urlList', urlList: arr, source: window.location.href});
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
 | 
			
		||||
	
 | 
			
		||||
	if (request.action == "requestPics") {
 | 
			
		||||
		twitterXPath(1, sendResponse);
 | 
			
		||||
	} else if (request.action == "requestPics2") {
 | 
			
		||||
		twitterXPath(2, sendResponse);
 | 
			
		||||
	} else if (request.action == "requestPics3") {
 | 
			
		||||
		twitterXPath(3, sendResponse);
 | 
			
		||||
	} else if (request.action == "requestPics4") {
 | 
			
		||||
		twitterXPath(4, sendResponse);
 | 
			
		||||
	} else {
 | 
			
		||||
		sendResponse({}); // Send nothing..
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -11,6 +11,10 @@
 | 
			
		||||
  </head>
 | 
			
		||||
  <body>
 | 
			
		||||
    <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>
 | 
			
		||||
  </body>
 | 
			
		||||
</html>
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										25
									
								
								popup.js
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								popup.js
									
									
									
									
									
								
							@ -1,19 +1,17 @@
 | 
			
		||||
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) {
 | 
			
		||||
// 	changeColor.style.backgroundColor = data.color;
 | 
			
		||||
// 	changeColor.setAttribute('value', data.color);
 | 
			
		||||
// });
 | 
			
		||||
 | 
			
		||||
downloadBtn.onclick = function(element) {
 | 
			
		||||
 | 
			
		||||
// 	chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
 | 
			
		||||
// 		console.log(tabs[0].url);
 | 
			
		||||
// 	});
 | 
			
		||||
	
 | 
			
		||||
function sendActionAndResponse(actionStr) {
 | 
			
		||||
	chrome.tabs.getSelected(null, function(tab) {
 | 
			
		||||
		// 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);
 | 
			
		||||
			if (response.dataType == "urlList") {
 | 
			
		||||
				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;
 | 
			
		||||
// 	chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
 | 
			
		||||
@ -37,3 +44,7 @@ downloadBtn.onclick = function(element) {
 | 
			
		||||
// 			{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