Skip to content

Commit

Permalink
Analyze article text content (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex0Blackwell authored Nov 27, 2021
1 parent 266bcd2 commit 9586fa8
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 14 deletions.
1 change: 1 addition & 0 deletions dist/background.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/content.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions dist/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Bias Monitor",
"version": "1.1.1",
"version": "1.2.0",

"description": "Gain insight on the political bias of a news article.",
"icons": {
Expand All @@ -10,7 +10,13 @@
"128": "./imgs/128.png"
},

"background": {
"service_worker": "background.js"
},

"permissions": [
"activeTab",
"scripting",
"tabs",
"storage"
],
Expand All @@ -22,8 +28,5 @@
},
"default_title": "Check this site's bias",
"default_popup": "index.html"
},
"host_permissions":[
"*://*/"
]
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Kick off the scripts conditional to if the base url
* is a news site or not.
*
* @param {*} tab_id
* @param {*} tab
*/
async function kick_off_script(tab_id, tab) {
try {
await chrome.scripting.executeScript({
target: { tabId: tab_id },
files: ["./content.js"],
});
} catch(error) {
console.debug(error)
}
}


/**
* Fires on selecting new tabs.
*/
chrome.tabs.onActivated.addListener(async (activeInfo) => {
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, async function(tabs) {
const tab_id = activeInfo.tabId;
const tab = tabs[0]
await kick_off_script(tab_id, tab);
});
});


/**
* Fires on tabs updating, such as reloads.
*/
chrome.tabs.onUpdated.addListener(async (tabID, changeInfo, tab) => {
if(changeInfo.status == "complete")
await kick_off_script(tabID, tab);
});
35 changes: 35 additions & 0 deletions src/bias_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,38 @@ export default class BiasRunner {
static async run(active_tab) {
BiasRunner.page_content = active_tab.title;
BiasRunner.website_url = active_tab.url;
await this.set_page_content();
await this.update_and_display();
}

static async set_page_content() {
let page_content = false;

const max_retries = 11;
for(let i = 0; i < max_retries && !page_content; ++i) {
const exponential_backoff = Math.pow(2, i);
await this.sleep(exponential_backoff)
page_content = await this._get_content_from_local_storage();
}

BiasRunner.page_content += page_content;
}

static _get_content_from_local_storage() {
return new Promise(function(resolve, reject) {
try {
chrome.storage.local.get({page_content: {}}, function(result) {
if(BiasRunner.website_url in result.page_content)
resolve(result.page_content[BiasRunner.website_url]);
else
resolve(false);
});
} catch (error) {
resolve(false);
}
});
}

static async update_and_display() {
let current_bias_score;
const has_cached_rating = await this._has_cached_rating();
Expand Down Expand Up @@ -74,4 +103,10 @@ export default class BiasRunner {
});
});
}

static sleep(milliseconds) {
return new Promise(
resolve => setTimeout(resolve, milliseconds)
);
}
}
44 changes: 44 additions & 0 deletions src/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
async function main() {
const page_content = get_page_content();
await set_local_storage(page_content);
}


function get_page_content() {
const considered_tags = ["h1"];
let content = "";

for (const tag of considered_tags) {
const element_arr = document.getElementsByTagName(tag);
for (const element of element_arr) {
content += element.textContent
content += " "
}
}
const sample_size = 800;
const body_text = document.body.innerText;
const start_sample = parseInt(body_text.length/10);
let body_text_sample = body_text.slice(start_sample, start_sample+sample_size);
content += body_text_sample;

return content;
}


async function set_local_storage(page_content) {
return new Promise(function(resolve, reject) {
const website_url = window.location.toString();
let url_to_content_map = {}
url_to_content_map[website_url] = page_content;

chrome.storage.local.set(
{page_content: url_to_content_map},
function() {
resolve();
}
);
});
}


main();
8 changes: 2 additions & 6 deletions src/services/post_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ export default class PostService {


/**
* The responses we get back are mostly in the [-2, 2]
* range because we don't send over large requests
* with lots of text data. This applies a cusp
* transformation to amplify some of the small numbers.
* A cusp transformation amplifys some of the small numbers.
*
* @param {*} result
* @returns a float after a cusp transformation
Expand All @@ -82,8 +79,7 @@ export default class PostService {
const abs_result_float = Math.abs(result_float);

const squash = 5.7;
const vertical_shift = 3;
const transformed_val = squash*Math.pow(abs_result_float, 1/2) + vertical_shift;
const transformed_val = squash*Math.pow(abs_result_float, 1/2);

return is_negative ? -1*transformed_val : transformed_val;
}
Expand Down
4 changes: 3 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ if (process.env.TESTBUILD) {
module.exports = {
entry: {
main: entry,
background: './src/background.js',
content: './src/content.js',
},
output: {
path: output_path,
filename: '[name].js'
},
};
};

0 comments on commit 9586fa8

Please sign in to comment.