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

Video Consumption Module Functionality #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

2016rshah
Copy link
Collaborator

Fixes #26.

Haven't tested it integrated with the new version of web-science yet, but if this code can get upstreamed to web-science, the radicalization study should be easier to build.

…Haven't tested it integrated with the new version of web-science yet
@2016rshah
Copy link
Collaborator Author

I know this module still needs work, but if I could get an initial review to tell me what I should be looking to work on in terms of style/convention that would be really helpful.

@jonathanmayer jonathanmayer added the enhancement New feature or request label Apr 11, 2021
@jonathanmayer jonathanmayer self-requested a review April 11, 2021 16:48
* message sending Promise remains open. This error does not affect functionality,
* because we do not depend on resolving the Promise (i.e., a response to the
* page visit stop message).
* @module WebScience.Measurements.content-scripts.videoConsumption
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update to match the current @module conventions.

Comment on lines +12 to +15
*
* # Note
* * The console.log represent the proper functionality (if an expected console.log does not appear, there is a bug).
* * After the testing infrastructure is added, all the console.log statements can be removed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would get rid of this and the various console.log debugging output statements, and instead use the built-in Firefox debugger at about:debugging. It's exceptionally helpful.

console.log("videoConsumption content script running");

/**
* The ID for a timer to periodically check for new videos on the page.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and throughout, video elements specifically.

*/


/* Outer function encapsulation to maintain unique variable scope for each content script */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this IIFE wrapper when you switch to Rollup bundling. Our Rollup configuration for WebScience automatically outputs IIFE content scripts.

Comment on lines +36 to +54
/**
* @typedef VideoConsumption
* @type {object}
* @property {object} video - the video element from the DOM.
* @property {number} playbackStart - the timestamp when the playback started, or -1 if the playback hasn't started yet.
* @property {number} playbackDuration - the total amount of time this video has played on this page.
*/

/**
* Array of objects that contain details about the consumption of a particular video.
* @type {VideoConsumption[]}
*/
let videoConsumptions = [];

/**
* Set of video DOM elements seen. If a video DOM element has been seen, then a listener has already been attached to it.
* @type{Set<video>}
*/
let videosSeen = new Set();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest a different approach to the data structures:

  • A WeakMap that maps video elements to unique video element IDs. When you have a DOM node in a collection you should almost always reference the node weakly, since it could be removed from the DOM.
  • A Map that maps unique video element IDs to playback data that you'll eventually aggregate and post to the background script.

Comment on lines +71 to +86
/**
* A RegExp for the page match patterns.
* @type {RegExp|null}
*/
let matchPatternsRegExp = null;

/**
* The registered page navigation content script.
* @type {RegisteredContentScript|null}
*/
let registeredContentScript = null;

/**
* Whether to notify the page data listener about private windows.
*/
let notifyAboutPrivateWindows = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These values should be per-listener. See the pageNavigation and linkExposure PRs for examples.

// for object inheritance
delete videoData.type;

onVideoData.notifyListeners([ videoData ]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the pageNavigation and linkExposure PRs for examples of how to make this notification listener-specific.

Comment on lines +110 to +112
export async function startMeasurement({
matchPatterns // causes error when empty, so we need to change default value
}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and throughout, wouldn't use the term "measurement" for what the module is doing. We've moved away from that model, and are instead following WebExtensions event conventions.

pageVisitStartTime: "number",
pageVisitStopTime: "number",
playbackDuration: "number",
ytChannel: "string", // TODO: what to put here if this might be empty?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would just send an empty string if there is no channel.

Comment on lines +149 to +155
function stopMeasurement() {
messaging.unregisterListener("WebScience.Measurements.VideoConsumption.VideoData", videoDataListener)
registeredContentScript.unregister();
registeredContentScript = null;
notifyAboutPrivateWindows = false;
matchPatternsRegExp = null;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the pageNavigation and linkExposure PRs for examples of how to do per-listener teardown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a module for observing user video consumption
2 participants