I am making a Chrome extension for version 87.0.4280.66.
It's a little fun script library to mess around with apps.
Right now, I'm making a Chrome extension that when you click a button, it adds on to an Instructure link to make it play confetti.
I'm making it check if there are parameters present or not, as well as if it is an Instructure link too.
The problem is when I call the function, it doesn't run it at all.
Here is the popup.js that is responsible for updating the URL to add the parameter confetti=true.
let confetti = document.getElementById('confetti');
const regEx = /https:\/\/cbsd\.instructure\.com\/courses\/([0-9]{5})\/assignments\/([0-9]{6})\?module_item_id=([0-9]{7})|https:\/\/cbsd\.instructure\.com\/courses\/([0-9]{5})\/assignments\/([0-9]{6})/g;
var getParams = function (url) {
var params = {};
var parser = document.createElement('a');
parser.href = url;
var query = parser.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
params[pair[0]] = decodeURIComponent(pair[1]);
}
return params;
};
function error(message)
{
var notification = {
type: "basic",
title: "Error",
iconUrl: "images/error.ico",
message: message
}
chrome.notifications.create(notification);
}
function isCanvas(domain)
{
return regEx.test(domain);
}
function isMod(dom)
{
var parameters = getParams(dom);
if (parameters) {
return(true)
} else {
return(false)
}
}
confetti.onclick = function(element) {
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
let domain = tabs[0].url;
var canvasCheck = isCanvas(domain)
if (canvasCheck === true) {
error(isMod(domain).toString());
/*/ if (andOrQuestion) {
let newUrl = domain + "&confetti=true";
chrome.tabs.update({url: newUrl});
} else {
let newUrl = domain + "?confetti=true"
chrome.tabs.update({url: newUrl});
} /*/
} else {
// error("You are not on an assignment page!")
}
});
}
Thank you for taking the time to answer this question and have a nice day!
Related
When a button is clicked on the webpage a table of data is displayed. I want to scrape that data but I can't find where it comes from in the website source code.
This is the tag for the button:
<button type="submit" onclick="divChangeStateOn('load-raw-0062294377Amazon.com'); getRaw('0062294377', 'Amazon.com', 'lr-0062294377Amazon.com',this);"style="margin-bottom: 4px; width: 120px; text-align: left;" name="load-raw"><img src='images/workstation.png'/> raw data</button>
I believe that the getRaw function is where the data comes from (I'm not positive about this) so I looked at the javascript code for the getRaw function
function getRaw(asin, store, res, caller)
{ document.getElementById(res).innerHTML = '<p align="center" valign="top"><img align="center" src="phpmy_loading.gif"></p>';
var poststr = "raw=" + encodeURI(asin) +
"&site=" + encodeURI(store);
var updateResults = new ajaxObject(res, 'extra.php', caller);
updateResults.update(poststr);
}
I have been having a hard time finding any documentation about ajaxObject and can't find any information about the update function. What is ajaxObject.update doing and is it possible for me to access the data that appears when the button is clicked?
function divChangeStateOn(divID)
{ var divElem = document.getElementById(divID);
divElem.style.display = 'block';
}
EDIT: The link to the source code view-source:http://www.ranktracer.com/account_workstation.php it might be password protected but I was just using the demo version
EDIT 2:
I am basically trying to write a script that replicates the Ajax http request. This where I am at, it doesn't work and I am especially concerned about where data = uri
x = time.time()
print x
timestamp = datetime.fromtimestamp(x/1000.0)
print timestamp
uri = "raw=0062294377&site=Amazon.com×tamp="+str(timestamp);
url = "lr-0062294377Amazon.com"
length = str(len(uri))
headers = {'X-Requested-With': 'XMLHttpRequest',
"Content-type": "application/x-www-form-urlencoded",
"Content-length": length,
"Connection" : "close"}
s = Session()
r = s.post(url= url, data= uri, headers= headers)
The entire code for ajaxObject is present in the link you provided. Please let us know what help you are expecting here?
function ajaxObject(layer, url, caller) {
if (caller) {
disableButton(caller, 'disable');
}
var that = this;
var updating = false;
this.callback = function() {}
var LayerID = document.getElementById(layer);
this.update = function(passData) {
if (updating == true) {
return false;
}
updating = true;
var AJAX = null;
if (window.XMLHttpRequest) {
AJAX = new XMLHttpRequest();
} else {
AJAX = new ActiveXObject("Microsoft.XMLHTTP");
}
if (AJAX == null) {
alert("Your browser doesn't support AJAX.");
return false
} else {
AJAX.onreadystatechange = function() {
if (AJAX.readyState == 4 || AJAX.readyState == "complete") {
if (caller) {
disableButton(caller, 'enable');
}
LayerID.innerHTML = AJAX.responseText;
delete AJAX;
updating = false;
that.callback();
}
}
var timestamp = new Date();
var uri = passData + '×tamp=' + (timestamp * 1);
AJAX.open("POST", url, true);
AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
AJAX.setRequestHeader("Content-length", uri.length);
AJAX.setRequestHeader("Connection", "close");
AJAX.send(uri);
return true;
}
}
}
I have been trying to Capture the call that analytics.js makes after it is loaded in phantomjs headless browser.
The problem with this is that, analytics.js load after the page completely loads. So, its getting difficult to track analytics.js calls.
The code which I have tried till now is:
var url = "http://www.alexandani.com/necklaces/sand-dollar-expandable-necklace.html";
var auditlinks = {"www.google-analytics.com/analytics.js": 6, "metrics.alexandani.com": 9, "www.google-analytics.com/collect": 7};
var block_request = 1;
var execution_timeout = 40000;
var resource_timeout = 50000;
var inactivity_timeout = 50000;
var user_agent = "Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.0 Safari/534.34";
var page_http_status_target_url = url;
var page_http_status = null;
var inactivity_timeout_check_period = 100;
var requests = new Array();
var auditlink_urls = Object.keys(auditlinks);
function print(obj){
console.log(JSON.stringify(obj));
}
function create_url_cleaner(){
var rx_match_protocol = /^(http|https):\/\//i;
var rx_match_query_params = /\/*\?.*/i;
function clean_url(url){
return (
(url.replace(rx_match_protocol, ''))
.replace(rx_match_query_params, '')
);
}
return clean_url;
}
function exit(exit_reason){
console.log(requests);
print({
'requests': requests,
'exit_reason': exit_reason,
// Returning http status as an integer makes little sense to me.
'http_status': (page_http_status === null)
? null : page_http_status.toString(),
});
phantom.exit(0);
}
function start_exec_time_limiter(execution_timeout){
setTimeout(
function (){
console.log('hi');
exit("EXEC_TIMEOUT");
},
execution_timeout
);
}
function start_inactivity_tracker(
inactivity_timeout,
inactivity_timeout_check_period
){
var last_activity_time = Date.now();
function register_activity(){
last_activity_time = Date.now();
}
function check_inactivity(){
var now = Date.now();
if (now - last_activity_time > inactivity_timeout){
exit("INACTIVITY_TIMEOUT")
}
}
setInterval(check_inactivity, inactivity_timeout_check_period);
return register_activity;
}
start_exec_time_limiter(execution_timeout);
var clean_url = create_url_cleaner();
var register_activity = start_inactivity_tracker(
inactivity_timeout, inactivity_timeout_check_period);
var page = require('webpage').create();
page.settings.userAgent = user_agent;
page.settings.resourceTimeout = resource_timeout;
//page.injectJs('wait.js');
page.onError = function (msg, stack){
// Ignore errors in the webpage context.
}
page.onResourceReceived = function (response){
register_activity();
if (response.url == page_http_status_target_url){
if (response.redirectURL){
page_http_status_target_url = response.redirectURL;
}
else {
page_http_status = response.status;
page_http_status_target_url = null;
}
}
}
page.onResourceRequested = function (requestData, request){
register_activity();
var timestamp = Date.now();
var url = requestData["url"];
var bare_url = clean_url(url);
for (var k in auditlink_urls){
var alurl = auditlink_urls[k];
if (bare_url.indexOf(alurl) === 0){
requests[requests.length] = [url, auditlinks[alurl], timestamp];
if (block_request === true){
request.abort();
}
break;
}
}
}
page.open(url);
//---------------------------------------------------------------------------//
Why do you need to load analytics.js at all in PhantomJS? If all you want to do is test that the calls to ga() are what you'd expect, just stub out the ga() function and assert that the calls it receives are what you'd expect.
In fact, the analytics.js snippet itself is essentially just a stub that stores the calls it receives on its q property while it waits for the full library to download.
If you don't download the analytics.js script, that ga.q will always be inspectable.
PhantomJS is a headless WebKit browser. I can open a url with this and get content of a page that updates every second.
But I need to get the content of many (100) pages at the same time.
All pages must be opened concurrently and refresh every second.
It's possible for one page, but I don't know how to retrieve from multiple pages at once.
This is the example code from the PhantomJS website:
console.log('Loading a web page');
var page = require('webpage').create();
var url = 'http://www.phantomjs.org/';
page.open(url, function (status) {
//Page is loaded!
phantom.exit();
});
May I use many PhantomJS instances at one time ? I doesn't seem the best way. Does any body know how to open just one PhantomJS instance and get content from several pages?
Here is the code, I used before to parse the items for the E-shop and putting HTML code for each page of these items
I hope that it will help you!
var RenderUrlsToFile, system, url_string_for_array;
var arrayOfUrls = new Array();
system = require("system");
RenderUrlsToFile = function(urls, callbackPerUrl, callbackFinal) {
var getFilename, next, page, retrieve, urlIndex, webpage, link_name, sex;
var fs = {};
fs = require('fs');
urlIndex = 0;
webpage = require("webpage");
page = null;
// getFilename = function() {
// return "parsed/" + urlIndex + ".png";
// };
next = function(status, url, file) {
page.close();
callbackPerUrl(status, url, file);
return retrieve();
};
retrieve = function() {
var url;
if (urls.length > 0) {
url = urls.shift();
urlIndex++;
page = webpage.create();
page.viewportSize = {
width: 800,
height: 600
};
page.settings.userAgent = "Phantom.js bot";
return page.open("http://" + url, function(status) {
var file;
// file = getFilename();
if (status === "success") {
return window.setTimeout((function() {
// page.render(file);
var js = page.evaluate(function () {
return document;
});
fs.write('your_file_path'.html', js.all[0].outerHTML, 'w');
return next(status, url, file);
}), 100);
} else {
return next(status, url, file);
}
});
} else {
return callbackFinal();
}
};
return retrieve();
};
if (system.args.length > 1) {
arrayOfUrls = Array.prototype.slice.call(system.args, 1);
} else {
------------MAIN PART OF CODE FOR YOUR QUESTION------
For example: I need to parse the items on the E-shop, so I take the first page and then I set "for" for the exactly numbe of pages
url_string_for_array = "www.lamoda.ru/c/559/accs-muzhskieaksessuary/?genders=men&page=1";
for(var k=2; k<20; k++)
{
url_string_for_array += ",www.lamoda.ru/c/559/accs-muzhskieaksessuary/?genders=men&page="+k;
}
arrayOfUrls = url_string_for_array.split(',');
}
RenderUrlsToFile(arrayOfUrls, (function(status, url, file) {
if (status !== "success") {
return console.log("Unable to render '" + url + "'");
} else {
return console.log("Rendered '" + url + "'");
}
}), function() {
return phantom.exit();
});
Hi i am downloading selected links using chrome extension but I can't set downloads location. All the urls downloaded to default location of chrome. i know we can't do it because of security reason. can we prompt directory chooser dialog in chrome extension popup from here user can select the Download path.Need any information from my side let me know.
Is this possible at all? Any suggestions on how to go about it?
Thanks in advance
My code
function downloadFile(url, onSuccess,arrayOfUrl,zip) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = "blob";
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (onSuccess)
{
onDownloadComplete(xhr.response, arrayOfUrl,zip)
}
}
}
xhr.send("null");
}
function onDownloadComplete(blobData,urls,zip ){
if (count < urls.length) {
blobToBase64(blobData, function(binaryData){
var fileName = urls[count].substring(urls[count].lastIndexOf('/')+1);
zip.file(fileName+".docx", binaryData, {base64: true});
if (count < urls.length -1){
count++;
downloadFile(urls[count], onDownloadComplete, urls,zip);
}
else {
var content = zip.generate();
var zipName = 'download.zip';
var a = document.createElement('a');
a.href = "data:application/zip;base64," + content;
a.download = zipName;
a.click();
count = 0;
}
});
}
}
popup.js
function onDownloadComplete(blobData,urls,zip ){
if (count < urls.length) {
blobToBase64(blobData, function(binaryData){
// add downloaded file to zip:
var fileName = urls[count].substring(urls[count].lastIndexOf('/')+1);
// zip.file(fileName, binaryData, {base64: true});
zip.file(fileName+".docx", binaryData, {base64: true}); //file"+count+".docx"
if (count < urls.length -1){
count++;
downloadFile(urls[count], onDownloadComplete, urls,zip);
}
else {
chrome.runtime.getBackgroundPage(function () {
zipAndSaveFiles(zip);});
}
});
}
}
**background.js**
function zipAndSaveFiles(zip)
{
var content = zip.generate(zip);
var zipName = 'download.zip';
var dataURL = 'data:application/zip;base64,' + content;
chrome.downloads.download({
url: dataURL,
filename: zipName,
saveAs: true
});
}
Since you are generating and downloading just one ZIP file, you can use the chrome.downloads.download() method. E.g.:
var content = zip.generate();
var zipName = 'download.zip';
var dataURL = 'data:application/zip;base64,' + content;
chrome.downloads.download({
url: dataURL,
filename: zipName,
saveAs: true
});
count = 0;
If you omit the display of a SaveAs dialog, then you can only specify a file name that is inside the user-defined download folder or in a subfolder of it.
Regarding the issue with the popup (see comment below):
You should call the function from your background-page, not the popup. E.g. you could use chrome.runtime.sendMessage/onMessage to pass a message to your background-page:
In background.js:
...
function zipAndSaveFiles(...) { ... }
chrome.runtime.onMessage.addListener(function(msg, sender) {
if ((msg.action === 'zipAndSave')
&& (msg.params !== undefined)) {
zipAndSaveFiles(msg.params);
}
});
In popup.js:
...
chrome.runtime.sendMessage({
action: 'zipAndSave',
params: ['url1', 'url2', 'url3']
});
I've been trying to retrieve information about the latest commit of a specific repo using Javascript, but without many results.
I've been using a XMLHttpRequest to get the data from the repo's commits page (for example: https://api.github.com/repos/trapped/rotmg_svr/commits), but seems like it returns null.
Also, I tried to use regular expressions to get the first match of specific line values (for example, to get the value of the commit description, I used this one: "message": "(.*)", ), making it loop through all lines until it finds something (in the code I've posted I temporarily changed it to line numbers, trying to speed up the process since it wasn't working).
Also, when trying to load the page, it blocks. As my previous experience with C#'s webclient suggests me, it's probably related to the XMLHttpRequest thing (indeed when I tried to debug it was returning an undefined object).
Thanks in advance for all the help.
var messagelineindex = 14;
var linklineindex = 17;
var response = "";
function loadcommits() {
httpGet("https://api.github.com/repos/trapped/rotmg_svr/commits");
var lines = response.split("\n");
var message = parsemessage(lines[messagelineindex]);
var link = parselink(lines[linklineindex]);
addcommit(//+message+"");
//for (var i = 0; i < numlines; i++) {
// var line = lines[i];
// var commitmessage = parsemessage(line);
// if (commitmessage != "") {
// addcommit(commitmessage);
// break;
// }
//}
}
function parselink(text) {
var regex = /"link": "(.*)",/;
var match = regex.exec(text);
if (!match) {
return "";
}
else {
return match[1];
}
}
function parsemessage(text) {
var regex = /"message": "(.*)",/;
var match = regex.exec(text);
if (!match) {
return "";
}
else {
return match[1];
}
}
function addcommit(text) {
document.getElementById("commits").innerHTML = document.getElementById("commits").innerHTML + text.toString() + "\n\r";
}
function httpGet(theUrl) {
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
response = xmlHttp.responseText;
}
}
xmlHttp.open("GET", theUrl, false);
xmlHttp.send(null);
}