allow download pics in 2nd, 3rd and 4th article from twitter

This commit is contained in:
Gary Wang 2021-03-17 23:44:34 +08:00
parent 75695f9d11
commit 4741eeabee
3 changed files with 45 additions and 20 deletions

View File

@ -2,10 +2,8 @@ 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);
} }
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { function twitterXPath(nth, sendResponse) {
if (request.action == "requestPics") { let a = getElementByXpath(`(//article[contains(@role,'article')])[${nth}]//a[contains(@href,'/photo/')]//img/@src`);
// TODO: check url, use different match rule to return data.
let a = getElementByXpath("(//article[contains(@role,'article')])[1]//a[contains(@href,'/photo/')]//img/@src");
let arr = []; let arr = [];
for (i=0; i<a.snapshotLength; i++) { for (i=0; i<a.snapshotLength; i++) {
let oneUrl = a.snapshotItem(i).textContent; let oneUrl = a.snapshotItem(i).textContent;
@ -15,8 +13,20 @@ chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
getParas.set("name", "orig"); getParas.set("name", "orig");
arr.push(baseUrl + '?' + getParas.toString()); arr.push(baseUrl + '?' + getParas.toString());
} }
// console.log(arr); console.log(arr);
sendResponse({dataType: 'urlList', urlList: arr, source: window.location.href}); 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 { } else {
sendResponse({}); // Send nothing.. sendResponse({}); // Send nothing..
} }

View File

@ -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>

View File

@ -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"); }