Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/soundcloud #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/content_scripts/content_soundcloud.js
Original file line number Diff line number Diff line change
@@ -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 = soundbadge.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);
15 changes: 1 addition & 14 deletions src/content_scripts/content_youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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);
}
});
4 changes: 2 additions & 2 deletions src/content_scripts/contentlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down