免责声明:我没有 gmail,所以我没有专门针对 gmail 进行测试。
这个答案的存在是为了解决你的评论 https://stackoverflow.com/questions/33481824/bookmarklet-that-works-on-2-pages#comment54753376_33482566:
iframe 怎么样?理论上是否可以在 iframe 中使用 gmail 登录,因此当 iframe 更改到另一个页面时,这不会对书签产生影响?
是的,技术上可以有一个持久的小书签使用 iframe(或者,上帝保佑,框架集)。
只要您的父窗口(并且它包含 iframe)保留在相同的域,它应该根据跨域规范工作。
然而,有可能(取决于所使用的方法)(非)有意地“反作用”(根据所使用的反作用,仍然可以被规避,等等)。
导航到网站,然后执行书签:
- 创建 iframe。
- 将 onload-handler 设置为 iframe。
- 用 iframe 替换当前网页内容(到窗口的完整宽度和高度)。
- 将 iframe 的源设置为当前 url(在注入的 iframe 中重新加载当前打开的页面)。
然后 iframe 的 onload-handler 的工作是检测(使用 url/title/page-content)加载了哪个页面以及应该采取哪些(如果有)操作。
示例(缩小(去除注释和不需要的空格)使用Dean Edward 的 Packer v3 http://dean.edwards.name/packer/):
javascript:(function(P){
var D=document
, B=D.createElement('body')
, F=D.createElement('iframe')
; //end vars
F.onload=function(){
var w=this.contentWindow //frame window
, d=w.document //frame window document
; //end vars
//BONUS: update address-bar and title.
//Use location.href instead of document.URL to include hash in FF, see https://stackoverflow.com/questions/1034621/get-current-url-in-web-browser
history.replaceState({}, D.title=d.title, w.location.href );
P(w, d); //execute handler
};
D.body.parentNode.replaceChild(B, D.body); //replace body with empty body
B.parentNode.style.cssText= B.style.cssText= (
F.style.cssText= 'width:100%;height:100%;margin:0;padding:0;border:0;'
) + 'overflow:hidden;' ; //set styles for html, body and iframe
//B.appendChild(F).src=D.URL; //doesn't work in FF if parent url === iframe url
//B.appendChild(F).setAttribute('src', D.URL); //doesn't work in FF if parent url === iframe url
B.appendChild(F).contentWindow.location.replace(D.URL); //works in FF
}(function(W, D){ //payload function. W=frame window, D=frame window document
alert('loaded');
// perform tests on D.title, W.location.href, page content, etc.
// and perform tasks accordingly
}));
注意:进一步缩小的明显方法之一是对 createElement、contentWindow 等使用带有字符串变量的括号访问。
这是要使用的有效负载函数(来自上面的小书签)的示例函数体http://www.w3schools.com http://www.w3schools.com/(抱歉,我无法很快想到另一个目标):
var tmp;
if(D.title==='W3Schools Online Web Tutorials'){
//scroll colorpicker into view and click it after 1 sec
tmp=D.getElementById('main').getElementsByTagName('img')[0].parentNode;
tmp.focus();
tmp.scrollIntoView();
W.setTimeout(function(){tmp.click()},1000);
return;
}
if(D.title==='HTML Color Picker'){
//type color in input and click update color button 'ok'
tmp=D.getElementById('entercolorDIV');
tmp.scrollIntoView();
tmp.querySelector('input').value='yellow';
tmp.querySelector('button').click();
//click 5 colors with 3 sec interval
tmp=D.getElementsByTagName('area');
tmp[0].parentNode.parentNode.scrollIntoView();
W.setTimeout(function(){tmp[120].click()},3000);
W.setTimeout(function(){tmp[48].click()},6000);
W.setTimeout(function(){tmp[92].click()},9000);
W.setTimeout(function(){tmp[31].click()},12000);
W.setTimeout(function(){tmp[126].click()},15000);
return;
}
上面的示例(在小书签内)已缩小:
javascript:(function(P){var D=document,B=D.createElement('body'),F=D.createElement('iframe');F.onload=function(){var w=this.contentWindow,d=w.document;history.replaceState({},D.title=d.title,w.location.href);P(w,d)};D.body.parentNode.replaceChild(B,D.body);B.parentNode.style.cssText=B.style.cssText=(F.style.cssText='width:100%;height:100%;margin:0;padding:0;border:0;')+'overflow:hidden;';B.appendChild(F).contentWindow.location.replace(D.URL)}(function(W,D){var tmp;if(D.title==='W3Schools Online Web Tutorials'){tmp=D.getElementById('main').getElementsByTagName('img')[0].parentNode;tmp.focus();tmp.scrollIntoView();W.setTimeout(function(){tmp.click()},1000);return}if(D.title==='HTML Color Picker'){tmp=D.getElementById('entercolorDIV');tmp.scrollIntoView();tmp.querySelector('input').value='yellow';tmp.querySelector('button').click();tmp=D.getElementsByTagName('area');tmp[0].parentNode.parentNode.scrollIntoView();W.setTimeout(function(){tmp[120].click()},3000);W.setTimeout(function(){tmp[48].click()},6000);W.setTimeout(function(){tmp[92].click()},9000);W.setTimeout(function(){tmp[31].click()},12000);W.setTimeout(function(){tmp[126].click()},15000);return}}));
希望这对您有所帮助(您开始了)!