From 59cce908434ebcfeda383f0d2fa58e4377926cb2 Mon Sep 17 00:00:00 2001 From: Johannes Ahnlide Date: Wed, 20 Jun 2018 11:59:08 +0200 Subject: [PATCH 1/2] Basic parsing of soundcloud page --- src/content_scripts/content_soundcloud.js | 25 +++++++++++++++++++++++ src/content_scripts/content_youtube.js | 15 +------------- src/content_scripts/contentlib.js | 4 ++-- 3 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 src/content_scripts/content_soundcloud.js diff --git a/src/content_scripts/content_soundcloud.js b/src/content_scripts/content_soundcloud.js new file mode 100644 index 00000000..a996cfa7 --- /dev/null +++ b/src/content_scripts/content_soundcloud.js @@ -0,0 +1,25 @@ +import browser from 'webextension-polyfill'; +import { + sendCreator, + addPageChangeListener, + waitForElement, +} from './contentlib.js'; + +function crawlPage() { + // Tries to extract channel URL from page, retries after 1 second if not successful. + waitForElement('.playbackSoundBadge__titleContextContainer', 1000).then( + soundbadge => { + let url = document.location.href; + let links = ownerContainer.getElementsByTagName('a'); // Song is at index 0, artist at index 1 + let creator = { + id: links[1].getAttribute('href'), + name: links[1].innerText, + }; + console.log(creator); + sendCreator(url, creator); + } + ); +} + +crawlPage(); +addPageChangeListener(crawlPage); diff --git a/src/content_scripts/content_youtube.js b/src/content_scripts/content_youtube.js index a3dfe40f..dd811c20 100644 --- a/src/content_scripts/content_youtube.js +++ b/src/content_scripts/content_youtube.js @@ -7,11 +7,9 @@ import { waitForElement, } from './contentlib.js'; -// TODO: Should content_youtube.js be renamed to content_script.js and have checks for each site? - function crawlPage() { // Tries to extract channel URL from page, retries after 1 second if not successful. - waitForElement('owner-container', 1000).then(ownerContainer => { + waitForElement('#owner-container', 1000).then(ownerContainer => { let url = document.location.href; let channelLink = ownerContainer.getElementsByTagName('a')[0]; let creator = { @@ -24,15 +22,4 @@ function crawlPage() { } crawlPage(); - addPageChangeListener(crawlPage); -// FROM: https://stackoverflow.com/a/19758800/965332 -// Something like this needed to report the (content_url -> creator_url) mapping back to the background process. -chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) { - // If the received message has the expected format... - if (msg.text === 'report_back') { - // Call the specified callback, passing - // the web-page's DOM content as argument - sendResponse(document.all[0].outerHTML); - } -}); diff --git a/src/content_scripts/contentlib.js b/src/content_scripts/contentlib.js index c7053716..0509de55 100644 --- a/src/content_scripts/contentlib.js +++ b/src/content_scripts/contentlib.js @@ -2,9 +2,9 @@ import browser from 'webextension-polyfill'; -export function waitForElement(elementId, retryTime) { +export function waitForElement(selector, retryTime) { return new Promise((resolve, reject) => { - let element = document.getElementById(elementId); + let element = document.querySelector(selector); if (element) { resolve(element); return; From d46f0824af7f11c3618f54068fba7d21aa7db839 Mon Sep 17 00:00:00 2001 From: Johannes Ahnlide Date: Wed, 20 Jun 2018 12:01:37 +0200 Subject: [PATCH 2/2] Fixed wrong name bug --- src/content_scripts/content_soundcloud.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content_scripts/content_soundcloud.js b/src/content_scripts/content_soundcloud.js index a996cfa7..fae7b93d 100644 --- a/src/content_scripts/content_soundcloud.js +++ b/src/content_scripts/content_soundcloud.js @@ -10,7 +10,7 @@ function crawlPage() { waitForElement('.playbackSoundBadge__titleContextContainer', 1000).then( soundbadge => { let url = document.location.href; - let links = ownerContainer.getElementsByTagName('a'); // Song is at index 0, artist at index 1 + let links = soundbadge.getElementsByTagName('a'); // Song is at index 0, artist at index 1 let creator = { id: links[1].getAttribute('href'), name: links[1].innerText,