pizu
pizu

Reputation: 31

Chrome Console/Extensions: Visit a URL and click a button. Rinse and repeat for a different URL

INITIAL QUESTION:

Is it possible to run javascript from Chrome's Console that will:

  1. Navigate to a given URL
  2. Click a button

I'd like to repeat the above steps for hundreds of URLs. I'm sure if I get the two steps working iterating over a number of URLs will be fairly straightforward.

So, I've made Console persistent between page refreshes, so that's a good start...but it seems the Click event it not being triggered. The clicking command works fine if I manually navigate to the URL then run it via Console, but not as a whole piece of code.

Here's the code I have so far:

function f() {
window.location.href = "https://mywebsite.com/post"; 
document.querySelector('.post_like_button_class').click(); 
     }
f();

EDIT: One of the commenters below suggested using Chrome Extensions to achieve that. This is what I have written so far:

manifest.json

{
"name": "Getting Started Example",
"version": "1.0",
"description": "Build an Extension!",
"browser_action": {
"default_icon": "images/get_started32.png",
"default_title": "Your title"
},
"background": {
"scripts": ["background.js"],
"persistent": false
},
"manifest_version": 2
}

background.js

chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.create({ url: "https://www.google.com" });
});

Now...how do I then carry out the second part of my task? I'm a bit stuck.

Click a button:

document.querySelector('.post_like_button_class').click();

Upvotes: 1

Views: 1492

Answers (1)

pizu
pizu

Reputation: 31

After some further digging, here's one way to do it:

manifest.json

{
"name": "Getting Started Example",
"version": "1.0",
"description": "Build an Extension!",
"permissions": [ "*://*/*"
                ],
"browser_action": {
  "default_icon": "images/get_started32.png",
  "default_title": "Your title"
 },
"background": {
"scripts": ["background.js"],
"persistent": true
},
"manifest_version": 2
}

background.js

chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.update({ url: "https://www.instagram.com/p/Bk6AGDQFqvn/" });
});

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
        if (changeInfo.status == 'complete' && tab.active) {

chrome.tabs.executeScript(null,   
{code:" <INSERT YOUR JS CODE HERE> "});                                 
 }
})

Upvotes: 1

Related Questions