5

I have a support chrome extension as a workaround to a custom website to get information from it. I am trying to determine the redirect link of each line in a table as it does not have a normal href to the react table.

enter image description here

Looking at the code, I have determined that the page is redirected once the screen x/y position on the screen is clicked to perform an action. This then goes through the inherent script on the website to obtain an ID/Link based on that position to provide the link.

After the following section, the rest of it is minified React.

export function _onClick(_ref) { //_ref = MouseEvent {isTrusted: true, screenX: 1230, screenY: 458, clientX: 201, clientY: 332, …}

  var target = _ref.target; //target = td.col-xs-2 {__reactInternalInstance$db9spb3eyf: At, __reactEventHandlers$db9spb3eyf: {…}, colSpan: 1, rowSpan: 1, headers: "", …}


  store.activate([].concat(_toConsumableArray(store.getInstances())).reduce(domHelpers.findContainerNodes(target), []).sort(domHelpers.sortByDOMPosition).map(function (item) { //target = td.col-xs-2 {__reactInternalInstance$db9spb3eyf: At, __reactEventHandlers$db9spb3eyf: {…}, colSpan: 1, rowSpan: 1, headers: "", …}

    return item.instance;
  }));
}

With that said, one of the purposes of the chrome extension is to obtain all of the links that it redirects to on the page (Each row of the table gets redirected without displaying the href link in the table). Since it does not have a simple href link attached to a button I am having a hard time determining how to grab the link and without redirecting to the page itself.

I attempted something like this but it did not obtain the information I need without redirecting.

window.onclick = function(e) { 
            console.log(e);
        };

window.onclick redirect event srcElement shows baseURI where it is redirecting

Any help would be much appreciated. Thank You


Added some new content to try and help with getting the webRequest but doesn't seem to work in the manifest.js.

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"permissions": [
    "webRequest",
    "*://*.example.com/",
    "webRequestBlocking"
],
Robert Lee
  • 1,541
  • 1
  • 10
  • 20
  • The posted event info doesn't seem useful for the task at hand. Try listening to chrome.webNavigation.onBeforeNavigation in the background script to obtain the URL and prevent navigation in the content script's click handler with setTimeout(window.stop) – wOxxOm Jun 11 '19 at 02:26
  • The `MouseEvent` code and logs provides nothing relevant at all. Provide code from the concerned website, try to find out how they are binding the click event to redirect. Can't help with zero info. – Munim Munna Jun 13 '19 at 06:37
  • added additional information on the click event. – Robert Lee Jun 14 '19 at 02:51
  • hello, please what do you mean by *after another javascript executes and gets the href link without redirecting*? what javascript? is it under your control? and what href link? also, what does *I am trying to determine the redirect link of each line in a table* mean? – Scaramouche Jun 14 '19 at 03:05
  • @Scaramouche - The website javascript is not under my control so I was using a chrome extension that I do have control of and the ability to execute when the page loads to attempt to grab the hyperlinks but the react table seems to use a X/Y coordinate to determine which row was clicked and redirects the page after it interacts with the backend. – Robert Lee Jun 14 '19 at 04:05
  • I see, so let me see if I understood. What you want is all the *links* that exist in that page (not the *window location* as stated in the question, right?). and the problem is that there are no actual navigation links, because the page is using the click's coordinates on a *react table* (what is a *react table* btw?) to determine the next page it'll go to, correct? also, is the page to go to determined per row, or could there be several destinations in just one row? finally, the actual site's url would b very helpful, at least the JS script responsible for such navigation method – Scaramouche Jun 14 '19 at 04:20
  • @Scaramouche - Let me help clarify. Hyperlinks do not exist in the page as there is just a ReactJS table that uses coordinates to determine the location that you get redirected to. In the table there are 10 rows of records showing that I need the hyperlinks it will ultimately redirect you to when you click on each row of the table without actually navigating to the page. In my example I showed a method of obtaining the link when a row is clicked but I need something that will grab that link without transferring me to the page. – Robert Lee Jun 14 '19 at 12:19
  • I understand it a bit better now so, when a row is clicked it (only) shows the link you'll b redirected to, it does not actually redirect you to the page but, I don't see the problem there, where is the (unwanted) redirection occurring then? you also said you came up with a way of obtaining the link when the row is clicked but it is transferring you to the page, why does it transfer you to the page? – Scaramouche Jun 14 '19 at 13:24
  • @Scaramouche - I do not think you are understanding the problem. When clicking on a row in the table it redirects me to a page just fine. What I am trying to accomplish is to use a chrome extension and stop it from redirecting when I use DOM elements to click on the table and just get the hyperlink that it is trying to take me to. – Robert Lee Jun 14 '19 at 13:30
  • oh, i was confused by this part *it will ultimately redirect you to when you click on each row of the table* ***without actually navigating to the page***, well it seems very unlikely anyone will b able to help you without taking a look into the JS code that's doing all that, specially since there is nothing to work with in the DOM, it seems to be all happening in JS events. good luck though – Scaramouche Jun 14 '19 at 13:35
  • 2
    Why don't you just look at the Network requests being made, and then make your own AJAX requests with varying X,Y values to iterate through all of the possible URLs? – mootrichard Jun 14 '19 at 18:47
  • @mootrichard - I do not believe I can because it is part of another javascript file that checks where the requests come from. If I do the request from the chrome extension it gives a 40x error since it checks access-control-allow-origin. – Robert Lee Jun 17 '19 at 02:50

2 Answers2

0

You can try to unbind the click event. As it is not possible to do this from the content script side, you need to inject the code into the page.

manifest.json:

{
    "name": "Run code on twitter mobile",
    "version": "1.0",
    "manifest_version": 2,
    "content_scripts": [{
        "js": ["contentscript.js"],
        "matches": ["http://mobile.twitter.com/*"]
    }],
    "web_accessible_resources": ["script.js"]
}

contentscript.js:

var s = document.createElement('script');
s.src = chrome.extension.getURL('script.js');
(document.head||document.documentElement).appendChild(s);
s.onload = function() {
    s.parentNode.removeChild(s);
};

Then, I would try to unbind the element using removeEventListened:

document.getElementById("myDIV").removeEventListener("click", myFunction);

or just simply clone the element, according to this answer.

Please check these Answers for further details:

balcsida
  • 71
  • 4
  • Tried loop through the elements and tried but did not stop it from the click event. My theory is that it is because it is using the coordinates of the Mouse location that I am unable to do it based on the on-page DOM elements. – Robert Lee Jun 19 '19 at 05:23
0

This might be what you're looking for. If this is in your background script any attempt to redirect somewhere will be stopped and the url will be logged to the console. You'll need the webRequest and webRequestBlocking permissions, as well as what are called host permissions (basically just putting a url pattern in the permissions section). You can read about it here.

let nowhere = "https://www.google.com/gen_204"

chrome.webRequest.onBeforeRequest.addListener(e => {
    if (e.url != nowhere) {
        console.log(e.url);
        return {redirectUrl: nowhere}
    };
}, {urls: ArrayOfUrls}, ['blocking']);
Mason
  • 738
  • 7
  • 18
  • I set the "permissions": [ "webRequest", "*://*.example.com/", "webRequestBlocking", "tabs", "activeTab" ],, and get Uncaught (in promise) TypeError: Cannot read property 'onBeforeRequest' of undefined at testing_next_page (content.js:59) – Robert Lee Jun 19 '19 at 05:09
  • @RobertLee Is it in your background script? – Mason Jun 19 '19 at 07:18
  • Tried background script – Robert Lee Jun 21 '19 at 00:53
  • Well the error is saying you're trying to get `onBeforeRequest` from `undefined`, so either `chrome.webRequest` is undefined wherever you're trying to call this from, or you've made a typo. And is your background script named `content.js`? cuz that's where the error is coming from. – Mason Jun 21 '19 at 02:14
  • Yes, the background script is called content.js – Robert Lee Jun 24 '19 at 12:29