Hi I am working with a webservice and I have to manipulate the design to make it look better. Recently I had to make it work on IPad.
So my Problem is my edit's don't work in Iphone because the service adds if its a mobile device a viewport and an extra .js file. This is causing unwanted changes. So is there a way to prevent the system from loading / opening? I can use Javascript to do this. My recent trys was to get the "IPhone.js" and make it empty
var scriptElements = document.getElementsByTagName("script");
var patt = /iPhone.js/g;
var sourceOfElement = "";
for (var i = 0; i < scriptElements.length; i++) {
sourceOfElement = scriptElements[i].src;
if (patt.test(sourceOfElement)) {
scriptElements[i].src = "";
};
This didn't really worked because the IPhone.js is loaded before I can "make it empty".
Another try was to remove the viewport, this also did't worked.
So anybody have any idea how to prevent the service from loading/executing the IPhone.js?
can you add iphone.js via javascript not script tag?
It will be something like this:
if (shouldILoad) {
(function() {
var myscript = document.createElement('script');
myscript.type = 'text/javascript';
myscript.src = ('iphone.js');
var s = document.getElementById('myscript');
s.parentNode.insertBefore(myscript, s);
})();
}
If you don't want iphone.js to execute just add a return statement at the start of the iphone.js file if condition is matched
iPhone.js
if(YOUR_CONDITION_NOT_TO_EXECUTE_IPHONE_JS) return; //check for your condition and return
//Other iphone.js code
Related
I'm a newbie NuxtJS programmer. I just searched all over the internet but couldn't find the answer.
I'm just wondering that is it natural that NuxtJS automatically adds the script at the end of the body described as below.
<script type="text/javascript">
document.oncontextmenu = null;
document.onselectstart = null;
document.ondragstart = null;
document.onmousedown = null;
document.body.oncontextmenu = null;
document.body.onselectstart = null;
document.body.ondragstart = null;
document.body.onmousedown = null;
document.body.oncut = null;
document.body.oncopy = null;
document.body.onpaste = null;
</script>
Everytime I change my page, it keeps adding this script, so it stacks the same script over and over.
Do you guys have any ideas why this is happening?
I also searched entire code where it does add this script but couldn't find it.
I can give any informations you need, so please help me with this problem.
Many thanks!!!
Since this one is added only on Chrome and not Firefox, and looking at the code added, I guess that you can simply ignore that one and move along (something on your side and not directly related to Nuxt).
Dynamically appended script never loads:
const nodeTemplate = document.importNode(template.content, true)
const script = nodeTemplate.firstElementChild
script.removeAttribute('async')
script.setAttribute('defer', 'defer')
document.body.appendChild(nodeTemplate)
Any idea why? Is there a way to force it to load?
I've looked around at diverse solutions here and elsewhere and none seem to work as of now.
It works fine if it happens at page load.
I think you need to check whether it's loaded or not, if not, better to remove completely that script from the DOM, and then create new script tag.
And then try something like this, it's common pattern to inject script widgets:
// 1. remove your old script
// 2. create new
(function(){
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = false;
s.defer = true;
s.src = "https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js";
document.getElementsByTagName('head')[0].appendChild(s);
})();
I'm trying to build a browser bookmarklet that lets a user save a quotation (block of text) - and with the press of the bookmarklet - will save the quote, along with a timestamp and URL to their profile inside a web app.
You can see the code I've written below, but running into a few problems.
Wrapping the code nicely to work in a bookmarklet.
Obviously using the $post may be heavy given some pages will require it to be added by the bookmarklet.
Any ideas on how to proceed?
You can see where I'm at currently here http://jsfiddle.net/Rh7zx/1/
(function() {
function getSelectionHtml() {
var html = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
return html;
}
function saveToBiblio() {
var url = window.location;
var dateSaved = new Date();
var selectedText = getSelectionHtml();
console.log(url + dateSaved + selectedText);
/*
do the $post here
*/
}
)();
1) Wrapping the code nicely to work in a bookmarklet.
A bookmarklet is just JavaScript code encoded into a URL using the javascript: pseudo-protocol. There are several sites and tools that will take your code and turn it into a bookmarklet. The fundamental thing is to make it so that when the code is run, the thing you want to do occurs. Your code currently defines two functions, but doesn't call either of them. You'd want to call the relevant one. It's also generally best to wrap your bookmarklet code in a scoping function so you don't add to the page's global namespace (because of the possibility of conflicts):
(function() {
// Your code here
)();
Some of the bookmarklet makers may have an option to do that for you, but doing it yourself is trivial.
2) Obviously using the $post may not work cross browser.
That will work fine cross-browser, but won't work in pages that don't have jQuery loaded, and loading jQuery into the page is probably overkill. I'd use XMLHttpRequest directly.
I am trying to write a userscript that will remove the effects of onselectstart from an id on a page.
So far I am assuming that I can rebind the original function using something like this:
document.getElementById('nodrag').onselectstart = function() { /* code original action */ };
JSFiddle
Any ideas would be appreciated.
Looks like I managed to work out the answer. Thank you LightStyle for giving me the hints.
So I create an script tag with document.getElementById('nodrag').onselectstart = function() { }; inside it and append this to the body.
// Create the script to remove the nodrag from chrome
var script = document.createElement('script');
script.type='text/javascript';
script.innerHTML = "document.getElementById('nodrag').onselectstart = function() { };";
var body = document.getElementsByTagName('body')[0];
body.appendChild(script);
This might not work in traditional contexts however works a charm on a userscript. Here is the final script:
https://gist.github.com/lgoldstien/5928021
I have some code where I am trying to include a script file and then call a function inside the script file. The code is:
function includeJS(p_file) {
var v_js = document.createElement('script');
v_js.type = 'text/javascript';
v_js.src = p_file;
document.getElementsByTagName('head')[0].appendChild(v_js);
}
function checkFlash(){
includeJS('/scripts/swfobject.js');
var playerVersion = swfobject.getFlashPlayerVersion();
return playerVersion.major;
}
alert(checkFlash());
The problem is it is failing at checkFlash() where it tries to get the player version. I checked firebug and it shows the script is loaded and its reading the file correct, so its not a path issue.
I thought that maybe it was a delay issue in the includeJS function, but when I would that code in before the alert without it being a function, it still gives the same problem.
Any ideas on how I would accomplish something like this?
Thanks.
I think your problem is that the script file doesn't finish loading before you attempt to check the player version (just like you originally thought). You might want to try this:
function includeJS(p_file, callback) {
var v_js = document.createElement('script');
v_js.type = 'text/javascript';
v_js.src = p_file;
v_js.onreadystatechange = callback;
v_js.onload = callback;
document.getElementsByTagName('head')[0].appendChild(v_js);
}
function checkFlash(){
var playerVersion;
includeJS('/scripts/swfobject.js', function () {
var playerVersion = swfobject.getFlashPlayerVersion();
alert(playerVersion.major);
});
}
checkFlash();
This is a modified version of the solution presented in the accepted answer to this similar question.