commit 3727ec6800f80e6b67a59ca3042fba953a61d135 Author: Gary Wang Date: Tue Jun 9 21:27:28 2020 +0800 init commit diff --git a/background.js b/background.js new file mode 100644 index 0000000..ad1c29a --- /dev/null +++ b/background.js @@ -0,0 +1,11 @@ +'use strict'; + +chrome.browserAction.onClicked.addListener(function(callback) { + //chrome.declarativeContent.ShowPageAction(); +}); + +chrome.runtime.onInstalled.addListener(function() { +// chrome.storage.sync.set({color: '#3aa757'}, function() { +// console.log("The color is green."); +// }); +}); diff --git a/common_content_script.js b/common_content_script.js new file mode 100644 index 0000000..8011c6b --- /dev/null +++ b/common_content_script.js @@ -0,0 +1,14 @@ +function getElementByXpath(path) { + return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; +} + +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"); + console.log(a); + sendResponse({dateType: 'urlList', urlList: a}); + } else { + sendResponse({}); // Send nothing.. + } +}); diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..99592b2 --- /dev/null +++ b/manifest.json @@ -0,0 +1,20 @@ +{ + "name": "Download The Pics (WebExtension Client)", + "version": "1.0", + "manifest_version": 2, + "description": "Download The Pics WebExtension Client. You also need the local daemon process to make this extension works", + "background": { + "scripts": ["background.js"] + }, + "browser_action": { + "default_popup": "popup.html" + }, + "content_scripts": [ + { + "matches": [""], + "run_at": "document_start", + "js": ["common_content_script.js"] + } + ], + "permissions": ["activeTab", "declarativeContent"] +} diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..1503adf --- /dev/null +++ b/popup.html @@ -0,0 +1,16 @@ + + + + + + + + + + diff --git a/popup.js b/popup.js new file mode 100644 index 0000000..a30ad83 --- /dev/null +++ b/popup.js @@ -0,0 +1,27 @@ +let downloadBtn = document.getElementById('download'); +// +// 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); +// }); + + chrome.tabs.getSelected(null, function(tab) { + // Send a request to the content script. + chrome.tabs.sendRequest(tab.id, {action: "requestPics"}, function(response) { + console.log(response); + }); + }); + +// let color = element.target.value; +// chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { +// chrome.tabs.executeScript( +// tabs[0].id, +// {code: 'document.body.style.backgroundColor = "' + color + '";'}); +// }); +};