0

I'm new to programming to begin with.I made a programm that opens a URL in Chrome and i want to close the latest opened Chrome tab. How can i do that? Any help is appreciated!

 #include <stdio.h>
 #include <windows.h>
 

int main(){
    int number;
    int flag = 0;
    printf("enter a number between 1 and 2: ");
    

 while (!flag) {
     scanf("%d",&number);

     switch (number) {
     case 1:
     system("system https://www.youtube.com");
     flag = 1;
     break;

     case 2:
     system("system https://stackoverflow.com");
     flag = 1;
     break;


     default:
     printf("error please enter a number again: ");
     break;
     }
}
 }
Clifford
  • 88,407
  • 13
  • 85
  • 165
Kent
  • 25
  • 5
  • `int number; scanf("%d",&number); switch (number)` is undefined behavior on input `a`. You must initialize `number` or check the value returned by `scanf` and avoid evaluating `number` if `scanf` does not assign it. http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html – William Pursell Apr 05 '21 at 10:43
  • Even if it were possible, you are opening the URL via windows Explorer, so it will open in the users default browser - which may not be Chrome. Moreover launching the browser via `system()` does not make this a C question - `system()` launches the command shell and executes the given command. Chrome has may [command line argument options](https://peter.sh/experiments/chromium-command-line-switches/) though I doubt any will help, the presentation of previous tabs is a user configuration setting in Chrome itself. – Clifford Apr 05 '21 at 10:46
  • Yeah i understand...So how can i close the latest tab openned by the default browser ? @Clifford – Kent Apr 05 '21 at 13:54
  • @panoskent Clearly you don't understand, because you are tagging it as a C question and it is a browser and/or OS question. Clearly if you open a URL in th edefault browser, it will use default behaviour - you cannot send command line options to the browser without invoking it directly. Also it is not entirely clear what it is you are tying to achieve. To get Chrome to open the URL in a new window then: `system( "chrome /new-window https://stackoverflow.com" ) ;` https://superuser.com/questions/731467/command-line-option-to-open-chrome-in-new-window-and-move-focus – Clifford Apr 05 '21 at 14:29
  • @Clifford i understand that the question is not about c but thats the language im currently working with. I'm trying to open a URL wait for like 5 seconds and then automatically close the tab just like pressing the X button on the top right of the tab – Kent Apr 05 '21 at 14:51
  • @panoskent the way you're opening the URLs very likely wouldn't work for also closing tabs. That's a far more complex task. Opening a URL via the `system` command is super straightforward, but closing a tab, like I and others have said, is a totally different, and the code you currently have wouldn't make that task any easier. Web automation is a common thing, but in C, you'll have to do a lot of research on your own to even see if it's possible. I did some research and it looks like it's difficult even if you were using C++: https://stackoverflow.com/questions/17345551/web-automation-from-c – Random Davis Apr 05 '21 at 15:05
  • Yes, but `system()` invokes the system shell - once you have done that, it is a system shell question. Most users would be horrified if some external application started manipulating their browser - that is malware-like behaviour. When I open Chrome it is set up to open all tabs open when it last closed - how are you going to know which tab to close - they are not your tabs. You could probably do it with a chrome extension that received commands from your C code, but I really hope it is not possible without an extension that the browser user has elected to install. – Clifford Apr 05 '21 at 15:05
  • perhaps: https://stackoverflow.com/questions/22898942/can-i-control-a-chrome-extension-externally-from-c-or-c – Clifford Apr 05 '21 at 15:06
  • @panoskent as CLifford pointed out, you could use a Chrome extension to communicate with your C program in order to close those tabs, but it would only work on a system with that Chrome extension, and you'd have to learn how to build the extension and all that. The point is that while it is possible to do what you're asking, it's likely very complicated, and there's a lot of research required. And that's the kind of research you should do before posting here; see: [How much research effort is expected of StackOverflow users?](https://meta.stackoverflow.com/q/261592/6273251) – Random Davis Apr 05 '21 at 15:09
  • @Clifford ok man i'll look deeper into web automation. Thanks for your time – Kent Apr 05 '21 at 15:11
  • @RandomDavis Yeah...It seems like the community here is stricter that i expected. It's ok that only makes me better.However the research im doing is through asking in stackoverflow.So i dont think people should criticize me or anyone else for that (which have been done in the past) – Kent Apr 05 '21 at 15:14
  • @panoskent while StackOverflow is a site you could use while doing research, you have to still make sure you're following the community guidelines and rules. That includes putting in enough effort to demonstrate that you've thoroughly researched the problem, you understand what the avenues are, and that you're also truly exhausted every possible resource before coming here. StackOverflow shouldn't be your first step, in other words. So, by the time you post on StackOverflow you're expected to have a lot more info and understanding of the issue. We're here to help those who help us help them. – Random Davis Apr 05 '21 at 15:18
  • @panoskent Good SO questions are _specific_. It is not really conducive to research or speculative questions - you are expected to have done that before you arrive. For example the links I provided were the result of my _minimal_ research. Your question might be valid if it were more specific and focused and more appropriately tagged. It is not a general C question just because that is the language you have chosen. It may not even be the most appropriate language for the task. The tag targets _all C developers_, and only a tiny fraction of that audience might be able to answer. – Clifford Apr 05 '21 at 15:25
  • I've qualified the tagging, but you really need to clean up the question. You have one comment (and code) that suggests that you want to target any browser rather then just Chrome, and another that clarifies the requirement (with respect to when the "last tab" is closed - all that information needs to be put in the question rather then hidden in comments. You may also need to define "last tab" since a browser could open with many unrelated tabs open is so configured. – Clifford Apr 05 '21 at 15:29

0 Answers0