Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Can I use XMLHttpRequest and this chrome extension to fix the problem that can't execute the code in the secondary window with a different domain through the console of the first window in javascript?

https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf

I wanna open Spotify in another tab, and click the target artist, that's it.

let elements;
let element_text;
let artistname = 'Hans Zimmer';
let spotify_window = window.open('https://open.spotify.com/search/Hans%20Zimmer');

//I thought it's becaue the window hadn't loaded, but it didn't work
setTimeout(function () {
}, 5000);

elements = spotify_window.document.querySelectorAll('._45331a50e3963ecc26575a06f1fd5292-scss._3957b7dd066dbbba6a311b40a427c59f-scss:not(.good-one)');
alert(elements[0]);
//always undefine which is wrong

for (let element of elements) {
    element_text = element.innerText;
    if (element_text == artistname) {
        element.click();
        break;
    }

}


Also, if I execute the following code in that tab's console, it worked

let elements;
let element_text;
let artistname = 'Hans Zimmer';

elements = document.querySelectorAll('._45331a50e3963ecc26575a06f1fd5292-scss._3957b7dd066dbbba6a311b40a427c59f-scss:not(.good-one)');
alert(elements[0]);
//always undefine which is wrong

for (let element of elements) {
    element_text = element.innerText;
    if (element_text == artistname) {
        element.click();
        break;
    }

}


see:

https://ibb.co/PGn9cG4

then it clicked and jumped to the page

https://ibb.co/n6CPmvy

What I have tried:

Allow CORS: Access-Control-Allow-Origin - Chrome Web Store[^]
Posted
Updated 7-Feb-21 22:19pm
v2

1 solution

Check out the reviews of that extension - it doesn't look particularly good.

And as far as I can see, you are not issuing a cross-origin network requests, which is what would be blocked by CORS. Instead, you are trying to access the DOM of a page from another domain, which is a different security feature called "same origin policy".

If you want to severely compromise the security of your own browser, you might be able to do what you want. But don't expect to convince your users to do the same.

For your own system, if you feel like surfing without this security protection, try running Chrome with the --disable-web-security command-line argument.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900