init commit

This commit is contained in:
Gary Wang 2020-06-09 21:27:28 +08:00
commit 3727ec6800
5 changed files with 88 additions and 0 deletions

11
background.js Normal file
View File

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

14
common_content_script.js Normal file
View File

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

20
manifest.json Normal file
View File

@ -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": ["<all_urls>"],
"run_at": "document_start",
"js": ["common_content_script.js"]
}
],
"permissions": ["activeTab", "declarativeContent"]
}

16
popup.html Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<style>
button {
height: 30px;
width: 90px;
outline: none;
}
</style>
</head>
<body>
<button id="download"></button>
<script src="popup.js"></script>
</body>
</html>

27
popup.js Normal file
View File

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