1 Star 0 Fork 0

zkboys/ajax-tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
content.js 4.04 KB
一键复制 编辑 原始数据 按行查看 历史
PengChen96 提交于 2023-02-23 21:52 . feat: rename
// 设置iframeVisible默认值,刷新后重置storage
chrome.storage.local.set({iframeVisible: true});
// 在页面上插入代码
function injectedScript (path) {
const scriptNode = document.createElement('script');
scriptNode.setAttribute('type', 'text/javascript');
scriptNode.setAttribute('src', chrome.runtime.getURL(path));
document.documentElement.appendChild(scriptNode);
return scriptNode;
}
injectedScript('html/iframePage/mock.js');
injectedScript('pageScripts/index.js').addEventListener('load', () => {
chrome.storage.local.get(['iframeVisible', 'ajaxToolsSwitchOn', 'ajaxToolsSwitchOnNot200', 'ajaxToolsSkin', 'ajaxDataList'], (result) => {
console.log('【ajaxTools content.js】【storage】', result);
const {ajaxToolsSwitchOn = true, ajaxToolsSwitchOnNot200 = true, ajaxDataList = []} = result;
postMessage({type: 'ajaxTools', to: 'pageScript', key: 'ajaxDataList', value: ajaxDataList});
postMessage({type: 'ajaxTools', to: 'pageScript', key: 'ajaxToolsSwitchOn', value: ajaxToolsSwitchOn});
postMessage({type: 'ajaxTools', to: 'pageScript', key: 'ajaxToolsSwitchOnNot200', value: ajaxToolsSwitchOnNot200});
});
});
let iframe;
// 只在最顶层页面嵌入iframe
if (window.self === window.top) {
document.onreadystatechange = () => {
if (document.readyState === 'complete') {
iframe = document.createElement('iframe');
iframe.className = "ajax-tools";
iframe.style.setProperty('height', '100%', 'important');
iframe.style.setProperty('width', '580px', 'important');
iframe.style.setProperty('min-width', '1px', 'important');
iframe.style.setProperty('position', 'fixed', 'important');
iframe.style.setProperty('top', '0', 'important');
iframe.style.setProperty('right', '0', 'important');
iframe.style.setProperty('left', 'auto', 'important');
iframe.style.setProperty('bottom', 'auto', 'important');
iframe.style.setProperty('z-index', '2147483647', 'important');
iframe.style.setProperty('transform', 'translateX(calc(100% + 20px))', 'important'); // 470px
iframe.style.setProperty('transition', 'all .4s', 'important');
iframe.style.setProperty('box-shadow', '0 0 15px 2px rgba(0,0,0,0.12)', 'important');
iframe.frameBorder = "none";
// iframe.src = "http://localhost:4001/";
iframe.src = chrome.runtime.getURL("html/iframePage/dist/index.html");
if (document.body) document.body.appendChild(iframe);
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('【content】【ajax-tools-iframe-show】receive message', request);
const {type, iframeVisible, iframeZoom} = request;
if (type === 'iframeToggle') {
iframe.style.setProperty('transform', iframeVisible ? 'translateX(0)' : 'translateX(calc(100% + 20px))', 'important');
sendResponse({nextIframeVisible: !iframeVisible}); // 返回信息到popup.js / App.jsx
}
if (type === 'iframeZoom') {
if (iframeZoom === 'out') { // 缩小
iframe.style.setProperty('height', '40px', 'important');
let timer = setTimeout(() => {
iframe.style.setProperty('width', '170px', 'important');
clearTimeout(timer);
}, 400);
} else if (iframeZoom === 'in') { // 放大
iframe.style.setProperty('width', '580px', 'important');
let timer = setTimeout(() => {
iframe.style.setProperty('height', '100%', 'important');
clearTimeout(timer);
}, 400);
}
sendResponse();
}
return true;
});
}
}
}
chrome.storage.onChanged.addListener(function (changes, namespace) {
for (let [key, {oldValue, newValue}] of Object.entries(changes)) {
if (
key === 'ajaxDataList'
|| key === 'ajaxToolsSwitchOn'
|| key === 'ajaxToolsSwitchOnNot200'
) {
// 发送到pageScript/index
postMessage({
type: 'ajaxTools',
to: 'pageScript',
key,
value: newValue,
});
}
}
});
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zkboys/ajax-tools.git
git@gitee.com:zkboys/ajax-tools.git
zkboys
ajax-tools
ajax-tools
master

搜索帮助