Chrome Extension Javascript: How to get the window.location after another javascript executes and gets the href link without redirecting

Viewed 1123

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"
],
2 Answers

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:

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']);
Related