javascript override new tab page chrome - javascript

I'm trying to create an extension for Google Chrome overriding the "new tab" page, in which there is content in JavaScript. In the first tab, it runs correctly, but when new tabs are opened, the scripts don't work. What should I do for fixing it?

You can make an extension for Google Chrome. Chrome extensions require a manifest.json file to be included, and you can set overridable pages to bookmarks history or newtab. Like such:
"chrome_url_overrides" : {
"newtab": "index.html"
},
For more info about making Chrome extensions, read the docs.

I'm not sure what you mean by 'content in JavaScript'.
You can check out the code for my chrome extension, New Tab Redirect: https://github.com/jimschubert/newtab-redirect
The way I have it set up is that there is a redirect.html page which is used as the override page. On load, it checks for a user-specified url from the background/event page (background.js), then redirects to that url with a simple JavaScript redirect.
The master branch is v1.0 (background page) and the 2.0 branch uses an event page.
edit
To specifically answer why you might be having problems with your code, without seeing any actual code.. I assume you're doing the initialization of your script in a background page in very much the same way as in my extension and querying that data from the redirect page, where you're then getting/setting data in local storage.
If any of that data needs to change, you can't use local storage from your redirecting page. Instead, you need to send the data back to the background page and store options there. Think of your background page as a service and your redirecting page as a very thin, very stateless client.

Related

Website refreshing randomly with an unknown parameter &mn

My website is refreshing randomly by itself with an additional unknown URL parameter ?mn followed by a Hash. Example:
?mn=l4ehjinilk0ids2leotmtyrmttfuq4oqedu.eebubnay47ddfhvd
I really don't know where it comes from. It happened even in Incognito mode.
Website is on Sitecore.
Any ideas?
If the page is loading correctly from the server but then refreshes with additional parameters in your browser, it must be triggered by JavaScript code. It can be caused by your own JavaScript, scripts injected by a third-party snippet, scripts executed by your browser extensions and so on.
Follow the steps below to find what triggers this refresh:
Open Developer Tools in your browser and navigate to the Network tab
Select the Preserve log checkbox
Reload your web page to replicate the issue
Find the request with parameter ?mn= and select it
Go to the Initiator tab, it will show the request call stack or request initiator chain similar to this:
This can help you understand what in your browser triggered the web page refresh.
Thank you.
I finally found out.
It was due to the Zoom application, a Post-attendee URL was set, as soon as a meeting is done, Zoom triggers the default browser and redirects to a website.

When page loads, give me url - Firefox extension

Whenever I load a webpage, I need to get the URL of the current tab to be stored in some variable. I would prefer, to get the URL of the website I am requesting, before anything loads, so I can do some logic depending on the URL.
What are the methods that I can use so I can achieve that?
(this is for firefox extension)
Depending on the specifics of your task, three of the how-tos on the main MDN page on web extensions are relevant:
Intercepting HTTP requests (webRequest)
Modify a web page (content scripts), and
the tabs API (specifically onUpdated).

Keep the console script persistent in Google Chrome

I have a script I want to use in the Google Chrome console. But this script is going to reload the page. A bit like this :
setInterval(function(){location.reload();},3000);
The problem is, once it's reloaded, the script stops and the console is cleared. I tried the option "Preserve log on navigation" : it preserves the log, but the script doesn't restart after reloading.
How should I do ? Thanks :)
There is no way to actually do that. The only possible way I found is to develop a Chrome' extension and place your script on it. Your script will be excecuted every time the target page is loaded, so when you execute the location.refresh() method , the next time the page is loaded your script will be executed all again and so on. If you wish to persist some data between page loads, then you can use localStorage.
Find more information here https://developer.chrome.com/extensions/getstarted
How to inject scripts via extensions ?: https://developer.chrome.com/extensions/content_scriptsremember that the scope of the extensions is isolated from the rest of the page, so you cant directly access JS variables or functions declared in the page itself from an extension BUT you can interact with the DOM. Good luck
I am using a cool Chrome Extension called Resource Override, which allows you to inject a script on specific urls. So all you have to do is put your url (with * on each end), and create a JS file injected into the HEAD, and it will be ran every time. You start from scratch on every page though, so I'm not sure how you can persist data. Maybe in cookies?
Try creating a parent html document that has an iframe whose source is the original html page. Place the javascript in the parent html page and tell it to reload the iframe.

How can I prevent saving/downloading web page?

I was wondering if there was a way to prevent a user from saving/downloading a web page? Specifically, I mean not letting them have access to the data displayed through my web application on their own machine?
I've heard that this is not possible since the browser must have access to the source code/data, but at the same time, I've noticed that if I to my gmail account, open an email, save the page, but when I try to open that page on my computer, it doesn't work. Furthermore, if I click "view source", I can see that even the source does not display the entire email message, even though the email is opened in my browser.
How it's possible for gmail to prevent me from seeing that email data?
Thats what called rendering pages using dynamic data without refreshing page (AJAX). The entire page source code is not downloaded in one go and components within the page request data asynchronously to display content. Try googling it and you will find more information.
In View source you can only see the HTML, CSS, JavaScript codes. No one can copy any dynamic code (PHP) from view source.
You can't stop anyone to see your html,css code in browser, as we are having view source option.
Maximum what you can do is disable right click on your page. Thant can be done through JavaScript.

Accessing/writing to Chrome app localStorage

I'm writing a packaged app for Chrome. Is there an advantage to using the background page - instead of the app's main HTML page - to read/write the localStorage values?
Currently users seem to be losing data in ways I cannot duplicate. Right now the app reads and writes localStorage in the main HTML page's JavaScript. Would changing the app to use the background page's JavaScript fix this?
LocalStorage is limited to 5 megabytes, regardless of set permissions.
When users checks the "Delete cookies and other website and plugin data" on chrome://settings/clearBrowserData then I believe both site specific and extension localstorage files are deleted. Maybe this is how your users are "losing" data.
Using the background page to read/write to localstorage prevents corruption of your data from other extensions, which can happen for site domain localstorage files as only your background page can access the file.
While other extensions indeed can call your background page, they still have to use your save/load functions to access your extension localstorage file.
The problem might be with the context of the localStorage container. When run from the background script you are saving the the localStorage of your extension. When run from a content script you are saving to a localStorage are for that specific website. localStorage.setItem( 'xx', 'yyy' ) that is written by a content script on a google.com page can't be read by localStore.getItem( 'xx' ) call from a content script on yahoo.com

Categories

Resources